本文将说明如何将辅助IP地址添加到RHEL/CentOS 8服务器上的网络接口,其实添加辅助IP地址的方法有很多种,本文介绍的是手动创建网络配置文件和使用Network Manager CLI工具:nmcli。
一、手动添加辅助IP地址 第一种方法涉及创建子接口配置文件并将网络信息增加到其中,在我的RHEL 8服务器中,我的网络接口具有以下详细信息: # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff inet 192.168.122.198/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0 valid_lft 1891sec preferred_lft 1891sec inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute valid_lft forever preferred_lft forever 我将分配辅助IPv4地址的网络接口是enp1s0,此接口配置文件位于/etc/sysconfig/network-scripts目录中: $ cat /etc/sysconfig/network-scripts/ifcfg-enp1s0 TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="dhcp" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" IPV6_ADDR_GEN_MODE="stable-privacy" NAME="enp1s0" UUID="498869bb-0d88-4a4c-a83a-c491d1040b0b" DEVICE="enp1s0" ONBOOT="yes" 让我们创建一个子接口配置文件: sudo yum -y install vim sudo vim /etc/sysconfig/network-scripts/ifcfg-enp1s0:1 不要忽视:1在最后,然后添加IP配置数据: DEVICE=enp1s0:1 Type=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=none IPADDR=192.168.122.11 PREFIX=24 重新启动服务器以确认辅助IP地址: sudo reboot 系统重启后,检查接口的IP信息: # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0 valid_lft 3503sec preferred_lft 3503sec inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0:1 valid_lft forever preferred_lft forever inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute valid_lft forever preferred_lft forever 你可以看到我们有两个分配给接口的IP地址:192.168.122.197/24和我们分配的辅助IP 192.168.122.11/24。 确认你可以通过辅助IP地址访问服务器: $ ssh 192.168.122.11 The authenticity of host '192.168.122.11 (192.168.122.11)' can't be established. ECDSA key fingerprint is SHA256:5RuWUmEmb9WoXB6QSivSfvDj5DeaJrxT7N+GVTl/TA8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.122.11' (ECDSA) to the list of known hosts. Activate the web console with: systemctl enable --now cockpit.socket Last login: Sat Mar 23 11:03:35 2019 from 192.168.122.1 [jmutai@rhel8 ~]$ ip ad | grep enp1s0 2: enp1s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0 inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0:1
二、使用nmcli添加辅助IP地址 第二种方法是使用nmcli命令行工具,参考Linux上使用ifconfig、ip、netstat、nmcli命令查找可用的网络接口一文。 检查现有网络连接配置文件,运行nmcli connection show命令:
标记要添加辅助IP地址的接口,然后继续修改其配置: sudo nmcli con mod enp1s0 +ipv4.addresses "192.168.122.11/24" +ipv4.addresses选项用于分配辅助IP地址。 重新启动服务器以确认辅助IP地址: sudo reboot 确认辅助IP分配: $ ip ad 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0 valid_lft 3546sec preferred_lft 3546sec inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0 valid_lft forever preferred_lft forever inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute valid_lft forever preferred_lft forever 至此,你已成功将辅助IP地址添加到RHEL/CentOS 8服务器上的接口。
相关主题 |