溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在Ubuntu系統(tǒng)中怎么為單個網(wǎng)卡配置多個IP

發(fā)布時間:2021-09-03 11:01:26 來源:億速云 閱讀:264 作者:chen 欄目:系統(tǒng)運維

這篇文章主要講解了“在Ubuntu系統(tǒng)中怎么為單個網(wǎng)卡配置多個IP”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“在Ubuntu系統(tǒng)中怎么為單個網(wǎng)卡配置多個IP”吧!

臨時添加 IP 地址

首先,讓我們找到網(wǎng)卡的 IP 地址。在我的 Ubuntu 15.10 服務(wù)器版中,我只使用了一個網(wǎng)卡。

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

代碼如下:

sudo ip addr

樣例輸出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:2a:03:4b brd ff:ff:ff:ff:ff:ff
inet 192.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


代碼如下:

sudo ifconfig

樣例輸出:

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


正如你在上面輸出中看到的,我的網(wǎng)卡名稱是 enp0s3,它的 IP 地址是 192.168.1.103。

現(xiàn)在讓我們來為網(wǎng)卡添加一個新的 IP 地址,例如說 192.168.1.104。

打開你的終端并運行下面的命令添加額外的 IP。

代碼如下:


sudo ip addr add 192.168.1.104/24 dev enp0s3


用命令檢查是否啟用了新的 IP:

代碼如下:


sudo ip address show enp0s3


樣例輸出:

代碼如下:


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
inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.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

類似地,你可以添加任意數(shù)量的 IP 地址,只要你想要。

讓我們 ping 一下這個 IP 地址驗證一下。

代碼如下:

sudo ping 192.168.1.104

樣例輸出

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,只需要運行:

代碼如下:

sudo ip addr del 192.168.1.104/24 dev enp0s3

再檢查一下是否刪除了 IP。

代碼如下:

sudo ip address show enp0s3

樣例輸出:

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
inet 192.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

可以看到已經(jīng)沒有了!!

正如你所知,重啟系統(tǒng)后這些設(shè)置會失效。那么怎么設(shè)置才能永久有效呢?這也很簡單。

添加永久 IP 地址

Ubuntu 系統(tǒng)的網(wǎng)卡配置文件是 /etc/network/interfaces。

讓我們來看看上面文件的具體內(nèi)容。

代碼如下:

sudo cat /etc/network/interfaces

輸出樣例:

# 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

正如你在上面輸出中看到的,網(wǎng)卡啟用了 DHCP。

現(xiàn)在,讓我們來分配一個額外的地址,例如 192.168.1.104/24。

編輯 /etc/network/interfaces:

代碼如下:

sudo nano /etc/network/interfaces

如下添加額外的 IP 地址。

代碼如下:

# 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

保存并關(guān)閉文件。

運行下面的命令使更改無需重啟即生效。

代碼如下:

sudo ifdown enp0s3 && sudo ifup enp0s3

樣例輸出:

Killed old client process
Internet Systems Consortium DHCP Client 4.3.1
Copyright 2004-2014 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/enp0s3/08:00:27:2a:03:4e
Sending on LPF/enp0s3/08:00:27:2a:03:4e
Sending on Socket/fallback
DHCPRELEASE on enp0s3 to 192.168.1.1 port 67 (xid=0x225f35)
Internet Systems Consortium DHCP Client 4.3.1
Copyright 2004-2014 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/enp0s3/08:00:27:2a:03:4e
Sending on LPF/enp0s3/08:00:27:2a:03:4e
Sending on Socket/fallback
DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0xdfb94764)
DHCPREQUEST of 192.168.1.103 on 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.

注意:如果你從遠程連接到服務(wù)器,把上面的兩個命令放到一行中非常重要,因為第一個命令會斷掉你的連接。而采用這種方式可以保留你的 ssh 會話。

現(xiàn)在,讓我們用下面的命令來檢查一下是否添加了新的 IP:

sudo ip address show enp0s3

輸出樣例:

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
inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.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

很好!我們已經(jīng)添加了額外的 IP。

再次 ping IP 地址進行驗證。

代碼如下:

sudo ping 192.168.1.104

樣例輸出:

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


好極了!它能正常工作。就是這樣。

感謝各位的閱讀,以上就是“在Ubuntu系統(tǒng)中怎么為單個網(wǎng)卡配置多個IP”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對在Ubuntu系統(tǒng)中怎么為單個網(wǎng)卡配置多個IP這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI