如何在Ubuntu 15.10上为单个网卡设置多个IP地址

系统 Linux 系统运维
有时候,你可能想在你的网卡上使用多个IP地址。遇到这种情况,你会怎么办呢?买一个新的网卡并分配一个新的IP?不,没有这个必要。现在我们可以在Ubuntu系统中为一个网卡分配多个IP地址。本文介绍具体方法,也适用于Debian以及它的衍生版本。

   [[161583]]

      有时候,你可能想在你的网卡上使用多个 IP 地址。遇到这种情况,你会怎么办呢?买一个新的网卡并分配一个新的 IP?不,没有这个必要(至少在小型网络中)。现在我们可以在 Ubuntu 系统中为一个网卡分配多个 IP 地址。想知道怎么做到的?跟着我往下看,其实并不难。

  这个方法也适用于 Debian 以及它的衍生版本。

  临时添加 IP 地址

  首先,让我们找到网卡的 IP 地址。在我的 Ubuntu 15.10 服务器版中,我只使用了一个网卡。

  运行下面的命令找到 IP 地址:

  1. sudo ip addr 

  样例输出:

  1. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default  
  2. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
  3. inet 127.0.0.1/8 scope host lo 
  4. valid_lft forever preferred_lft forever 
  5. inet6 ::1/128 scope host  
  6. valid_lft forever preferred_lft forever 
  7. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 
  8. link/ether 08:00:27:2a:03:4b brd ff:ff:ff:ff:ff:ff 
  9. inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 
  10. valid_lft forever preferred_lft forever 
  11. inet6 fe80::a00:27ff:fe2a:34e/64 scope link  
  12. valid_lft forever preferred_lft forever 

  或

  1. sudo ifconfig 

  样例输出:

  1. enp0s3 Link encap:Ethernet HWaddr 08:00:27:2a:03:4b  
  2. inet addr:192.168.1.103 Bcast:192.168.1.255 Mask:255.255.255.0 
  3. inet6 addr: fe80::a00:27ff:fe2a:34e/64 Scope:Link 
  4. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
  5. RX packets:186 errors:0 dropped:0 overruns:0 frame:0 
  6. TX packets:70 errors:0 dropped:0 overruns:0 carrier:0 
  7. collisions:0 txqueuelen:1000  
  8. RX bytes:21872 (21.8 KB) TX bytes:9666 (9.6 KB) 
  9. lo Link encap:Local Loopback  
  10. inet addr:127.0.0.1 Mask:255.0.0.0 
  11. inet6 addr: ::1/128 Scope:Host 
  12. UP LOOPBACK RUNNING MTU:65536 Metric:1 
  13. RX packets:217 errors:0 dropped:0 overruns:0 frame:0 
  14. TX packets:217 errors:0 dropped:0 overruns:0 carrier:0 
  15. collisions:0 txqueuelen:0  
  16. RX bytes:38793 (38.7 KB) TX bytes:38793 (38.7 KB) 

  正如你在上面输出中看到的,我的网卡名称是 enp0s3,它的 IP 地址是 192.168.1.103。

  现在让我们来为网卡添加一个新的 IP 地址,例如说 192.168.1.104。

  打开你的终端并运行下面的命令添加额外的 IP。

  1. sudo ip addr add 192.168.1.104/24 dev enp0s3 

  用命令检查是否启用了新的 IP:  

  1. sudo ip address show enp0s3 

  样例输出:

  1. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 
  2. link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff 
  3. inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 
  4. valid_lft forever preferred_lft forever 
  5. inet 192.168.1.104/24 scope global secondary enp0s3 
  6. valid_lft forever preferred_lft forever 
  7. inet6 fe80::a00:27ff:fe2a:34e/64 scope link  
  8. valid_lft forever preferred_lft forever 

  类似地,你可以添加任意数量的 IP 地址,只要你想要。

  让我们 ping 一下这个 IP 地址验证一下。

  1. sudo ping 192.168.1.104 

  样例输出:

  1. PING 192.168.1.104 (192.168.1.104) 56(84) bytes of data. 
  2. 64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.901 ms 
  3. 64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.571 ms 
  4. 64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.521 ms 
  5. 64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.524 ms 

  好极了,它能工作!

  要删除 IP,只需要运行:

  1. sudo ip addr del 192.168.1.104/24 dev enp0s3 

  再检查一下是否删除了 IP。

  1. sudo ip address show enp0s3 

  样例输出:

  1. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 
  2. link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff 
  3. inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 
  4. valid_lft forever preferred_lft forever 
  5. inet6 fe80::a00:27ff:fe2a:34e/64 scope link  
  6. valid_lft forever preferred_lft forever 

  可以看到已经没有了!

  正如你所知,重启系统后这些设置会失效。那么怎么设置才能***有效呢?这也很简单。

  添加*** IP 地址

  Ubuntu 系统的网卡配置文件是 /etc/network/interfaces。

  让我们来看看上面文件的具体内容。

  1. sudo cat /etc/network/interfaces 

  输出样例:

  1. # This file describes the network interfaces available on your system 
  2. and how to activate them. For more information, see interfaces(5). 
  3. source /etc/network/interfaces.d/* 
  4. # The loopback network interface 
  5. auto lo 
  6. iface lo inet loopback 
  7. # The primary network interface 
  8. auto enp0s3 
  9. iface enp0s3 inet dhcp 

  正如你在上面输出中看到的,网卡启用了 DHCP。

  现在,让我们来分配一个额外的地址,例如 192.168.1.104/24。

  编辑 /etc/network/interfaces:

  1. sudo nano /etc/network/interfaces 

  如下添加额外的 IP 地址。

  1. # This file describes the network interfaces available on your system 
  2. and how to activate them. For more information, see interfaces(5). 
  3. source /etc/network/interfaces.d/* 
  4. # The loopback network interface 
  5. auto lo 
  6. iface lo inet loopback 
  7. # The primary network interface 
  8. auto enp0s3 
  9. iface enp0s3 inet dhcp 
  10. iface enp0s3 inet static 
  11.  address 192.168.1.104/24 

  保存并关闭文件。

  运行下面的命令使更改无需重启即生效。

  1. sudo ifdown enp0s3 && sudo ifup enp0s3 

  样例输出:

  1. Killed old client process 
  2. Internet Systems Consortium DHCP Client 4.3.1 
  3. Copyright 2004-2014 Internet Systems Consortium. 
  4. All rights reserved. 
  5. For info, please visit https://www.isc.org/software/dhcp/ 
  6. Listening on LPF/enp0s3/08:00:27:2a:03:4e 
  7. Sending on LPF/enp0s3/08:00:27:2a:03:4e 
  8. Sending on Socket/fallback 
  9. DHCPRELEASE on enp0s3 to 192.168.1.1 port 67 (xid=0x225f35) 
  10. Internet Systems Consortium DHCP Client 4.3.1 
  11. Copyright 2004-2014 Internet Systems Consortium. 
  12. All rights reserved. 
  13. For info, please visit https://www.isc.org/software/dhcp/ 
  14. Listening on LPF/enp0s3/08:00:27:2a:03:4e 
  15. Sending on LPF/enp0s3/08:00:27:2a:03:4e 
  16. Sending on Socket/fallback 
  17. DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0xdfb94764) 
  18. DHCPREQUEST of 192.168.1.103 on enp0s3 to 255.255.255.255 port 67 (xid=0x6447b9df) 
  19. DHCPOFFER of 192.168.1.103 from 192.168.1.1 
  20. DHCPACK of 192.168.1.103 from 192.168.1.1 
  21. bound to 192.168.1.103 -- renewal in 35146 seconds. 

  注意:如果你从远程连接到服务器,把上面的两个命令放到一行中非常重要,因为***个命令会断掉你的连接。而采用这种方式可以保留你的 ssh 会话。

  现在,让我们用下面的命令来检查一下是否添加了新的 IP:

  1. sudo ip address show enp0s3 

  输出样例:

  1. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 
  2. link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff 
  3. inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3 
  4. valid_lft forever preferred_lft forever 
  5. inet 192.168.1.104/24 brd 192.168.1.255 scope global secondary enp0s3 
  6. valid_lft forever preferred_lft forever 
  7. inet6 fe80::a00:27ff:fe2a:34e/64 scope link  
  8. valid_lft forever preferred_lft forever 

  很好!我们已经添加了额外的 IP。

  再次 ping IP 地址进行验证。

  1. sudo ping 192.168.1.104 

  样例输出:

  1. PING 192.168.1.104 (192.168.1.104) 56(84) bytes of data. 
  2. 64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.137 ms 
  3. 64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.050 ms 
  4. 64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.054 ms 
  5. 64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.067 ms 

  好极了!它能正常工作。就是这样。

  想知道怎么给 CentOS/RHEL/Scientific Linux/Fedora 系统添加额外的 IP 地址,可以点击下面的链接。

  如何在CentOS 7上给一个网卡分配多个IP地址

  工作愉快!

责任编辑:武晓燕 来源: Linux中国
相关推荐

2022-10-27 10:02:58

UbuntuLinux

2015-11-30 14:33:36

Intel GraphLinuxUbuntu

2011-04-14 11:41:41

UbuntuIP

2010-01-07 14:22:17

Ubuntu IP地址

2019-09-08 15:43:19

UbuntuLinuxIP地址

2019-08-13 16:10:38

UbuntuLinux时间同步

2018-12-09 13:50:47

UbuntuLinux IP地址

2019-04-04 11:20:37

Windows 10设置IP地址

2016-01-07 16:12:43

CentOS 7网卡IP地址

2015-12-07 14:47:15

UbuntuGIMP 2.8.16Linux

2015-10-26 15:48:51

安装Ubuntu 15.1Linux

2009-04-08 15:56:31

Linux路由MAC绑定

2015-12-01 10:54:44

Ubuntu 15.1NVIDIA 358.Linux

2020-04-30 13:50:11

Linux网卡信息

2018-09-08 09:05:00

UbuntuLinuxIP地址

2019-02-28 09:30:04

Ubuntusudo命令

2019-08-02 15:30:42

UbuntuMongoDB命令

2019-08-30 11:20:28

UbuntuVirtualBoxLinux

2018-10-15 15:23:50

UbuntupipPython

2024-01-04 11:50:00

UbuntuDocker
点赞
收藏

51CTO技术栈公众号