正如你在上面输出中看到的,我的网卡名称是 enp0s3,它的 IP 地址是 192.168.1.103。
现在让我们来为网卡添加一个新的 IP 地址,例如说 192.168.1.104。
打开你的终端并运行下面的命令添加额外的 IP。
1 2
sudoipaddradd192.168.1.104/24 dev enp0s3
用命令检查是否启用了新的 IP:
1 2
sudo ip address show enp0s3
样例输出:
1 2 3 4 5 6 7 8 9
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff inet192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet192.168.1.104/24 scope global secondary enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe2a:34e/64 scope link valid_lft forever preferred_lft forever
类似地,你可以添加任意数量的 IP 地址,只要你想要。
让我们 ping 一下这个 IP 地址验证一下。
1 2
sudo ping 192.168.1.104
样例输出
1 2 3 4 5 6
PING 192.168.1.104 (192.168.1.104) 56(84) bytes of data. 64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.901 ms 64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.571 ms 64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.521 ms 64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.524 ms
好极了,它能工作!
要删除 IP,只需要运行:
1 2
sudo ip addr del 192.168.1.104/24 dev enp0s3
再检查一下是否删除了 IP。
1 2
sudo ip address show enp0s3
样例输出:
1 2 3 4 5 6 7
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff inet192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe2a:34e/64 scope link valid_lft forever preferred_lft forever
可以看到已经没有了!!
正如你所知,重启系统后这些设置会失效。那么怎么设置才能永久有效呢?这也很简单。
添加永久 IP 地址
Ubuntu 系统的网卡配置文件是 /etc/network/interfaces。
让我们来看看上面文件的具体内容。
1 2
sudocat /etc/network/interfaces
输出样例:
1 2 3 4 5 6 7 8 9 10
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto enp0s3 iface enp0s3 inet dhcp
正如你在上面输出中看到的,网卡启用了 DHCP。
现在,让我们来分配一个额外的地址,例如 192.168.1.104/24。
编辑 /etc/network/interfaces:
1 2
sudo nano /etc/network/interfaces
如下添加额外的 IP 地址。
1 2 3 4 5 6 7 8 9 10 11 12
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto enp0s3 iface enp0s3 inet dhcp iface enp0s3 inet static address 192.168.1.104/24
Killed old client process Internet Systems Consortium DHCP Client 4.3.1 Copyright2004-2014 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listeningon LPF/enp0s3/08:00:27:2a:03:4e Sendingon LPF/enp0s3/08:00:27:2a:03:4e Sendingon Socket/fallback DHCPRELEASEon enp0s3 to 192.168.1.1 port 67 (xid=0x225f35) Internet Systems Consortium DHCP Client 4.3.1 Copyright2004-2014 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listeningon LPF/enp0s3/08:00:27:2a:03:4e Sendingon LPF/enp0s3/08:00:27:2a:03:4e Sendingon Socket/fallback DHCPDISCOVERon enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0xdfb94764) DHCPREQUEST of 192.168.1.103on enp0s3 to 255.255.255.255 port 67 (xid=0x6447b9df) DHCPOFFER of 192.168.1.103 from 192.168.1.1 DHCPACK of 192.168.1.103 from 192.168.1.1 bound to 192.168.1.103 -- renewal in 35146 seconds.
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff inet192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet192.168.1.104/24 brd 192.168.1.255 scope global secondary enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe2a:34e/64 scope link valid_lft forever preferred_lft forever
很好!我们已经添加了额外的 IP。
再次 ping IP 地址进行验证。
1 2
sudo ping 192.168.1.104
样例输出:
1 2 3 4 5 6
PING 192.168.1.104 (192.168.1.104) 56(84) bytes of data. 64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.137 ms 64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.050 ms 64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.054 ms 64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.067 ms
好极了!它能正常工作。就是这样。
想知道怎么给 CentOS/RHEL/Scientific Linux/Fedora 系统添加额外的 IP 地址,可以点击下面的链接。