https://www.2daygeek.com/how-to-add-additional-ip-secondary-ip-in-ubuntu-debian-system/
作者 | Prakash Subramanian
譯者 | MjSeven ?????共計翻譯:70 篇 貢獻時間:214 天
Linux 管理員應該意識到這一點,因為這是一項例行任務。很多人想知道為什麼我們需要在伺服器中新增多個 IP 地址,以及為什麼我們需要將它新增到單塊網絡卡中?我說的對嗎?
你可能也會有類似的問題:在 Linux 中如何為單塊網絡卡分配多個 IP 地址?在本文中,你可以得到答案。
當我們對一個新伺服器進行設定時,理想情況下它將有一個 IP 地址,即伺服器主 IP 地址,它與伺服器主機名對應。
我們不應在伺服器主 IP 地址上託管任何應用程式,這是不可取的。如果要在伺服器上託管任何應用程式,我們應該為此新增輔助 IP。
這是業界的最佳實踐,它允許使用者安裝 SSL 證書。大多數系統都配有單塊網絡卡,這足以新增額外的 IP 地址。
建議閱讀:
我們可以在同一個介面上新增 IP 地址,或者在同一裝置上建立子介面,然後在其中新增 IP。預設情況下,一直到 Ubuntu 14.04 LTS,介面給名稱為 ethX (eth0)
,但是從 Ubuntu 15.10 之後網路介面名稱已從 ethX
更改為 enXXXXX
(對於伺服器是 ens33,桌面版是 enp0s3)。
在本文中,我們將教你如何在 Ubuntu 上執行此操作,並且衍生到其它發行版(to 校正:這句自己加的)。
註意:別在 DNS 詳細資訊後新增 IP 地址。如果是這樣,DNS 將無法正常工作。
如何在 Ubuntu 14.04 LTS 中新增臨時輔助 IP 地址
在系統中新增 IP 地址之前,執行以下任一命令即可驗證伺服器主 IP 地址:
# ifconfig
或
# ip addr
# ip addr
eth0 Link encap:Ethernet HWaddr 08:00:27:98:b7:36
inet addr:192.168.56.150 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe98:b736/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:902 (902.0 B) TX bytes:16423 (16.4 KB)
eth1 Link encap:Ethernet HWaddr 08:00:27:6a:cf:d3
inet addr:10.0.3.15 Bcast:10.0.3.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe6a:cfd3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:80 errors:0 dropped:0 overruns:0 frame:0
TX packets:146 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8698 (8.6 KB) TX bytes:17047 (17.0 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:25 errors:0 dropped:0 overruns:0 frame:0
TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:1730 (1.7 KB) TX bytes:1730 (1.7 KB)
如我所見,伺服器主 IP 地址是 192.168.56.150
,我將下一個 IP 192.168.56.151
作為輔助 IP,使用以下方法完成:
# ip addr add 192.168.56.151/24 broadcast 192.168.56.255 dev eth0 label eth0:1
輸入以下命令以檢查新新增的 IP 地址。如果你重新啟動伺服器,那麼新新增的 IP 地址會消失,因為我們的 IP 是臨時新增的。
# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
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: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:98:b7:36 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.150/24 brd 192.168.56.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.56.151/24 brd 192.168.56.255 scope global secondary eth0:1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe98:b736/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:6a:cf:d3 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.15/24 brd 10.0.3.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe6a:cfd3/64 scope link
valid_lft forever preferred_lft forever
如何在 Ubuntu 14.04 LTS 中新增永久輔助 IP 地址
要在 Ubuntu 系統上新增永久輔助 IP 地址,只需編輯 /etc/network/interfaces
檔案並新增所需的 IP 詳細資訊。
# vi /etc/network/interfaces
# vi /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.56.150
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
gateway 192.168.56.1
auto eth0:1
iface eth0:1 inet static
address 192.168.56.151
netmask 255.255.255.0
儲存並關閉檔案,然後重啟網路介面服務。
# service networking restart
或
# ifdown eth0:1 && ifup eth0:1
驗證新新增的 IP 地址:
# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:98:b7:36
inet addr:192.168.56.150 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe98:b736/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:84 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:962 (962.0 B) TX bytes:11905 (11.9 KB)
eth0:1 Link encap:Ethernet HWaddr 08:00:27:98:b7:36
inet addr:192.168.56.151 Bcast:192.168.56.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth1 Link encap:Ethernet HWaddr 08:00:27:6a:cf:d3
inet addr:10.0.3.15 Bcast:10.0.3.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe6a:cfd3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4924 errors:0 dropped:0 overruns:0 frame:0
TX packets:3185 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4037636 (4.0 MB) TX bytes:422516 (422.5 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:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
如何在 Ubuntu 16.04 LTS 中臨時新增輔助 IP 地址
正如本文開頭所述,網路介面名稱從 Ubuntu 15.10 就開始從 ‘ethX’ 更改為 ‘enXXXX’ (enp0s3),所以,替換你的介面名稱。
在執行此操作之前,先檢查系統上的 IP 資訊:
# ifconfig
或
# ip addr
enp0s3: flags=4163 mtu 1500
inet 192.168.56.201 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::a00:27ff:fe97:132e prefixlen 64 scopeid 0x20
ether 08:00:27:97:13:2e txqueuelen 1000 (Ethernet)
RX packets 7 bytes 420 (420.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 294 bytes 24747 (24.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163 mtu 1500
inet 10.0.3.15 netmask 255.255.255.0 broadcast 10.0.3.255
inet6 fe80::344b:6259:4dbe:eabb prefixlen 64 scopeid 0x20
ether 08:00:27:12:e8:c1 txqueuelen 1000 (Ethernet)
RX packets 1 bytes 590 (590.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 97 bytes 10209 (10.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 325 bytes 24046 (24.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 325 bytes 24046 (24.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如我所見,伺服器主 IP 地址是 192.168.56.201
,所以,我將下一個 IP 192.168.56.202
作為輔助 IP,使用以下命令完成。
# ip addr add 192.168.56.202/24 broadcast 192.168.56.255 dev enp0s3
執行以下命令來檢查是否已分配了新的 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: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.56.202/24 brd 192.168.56.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe97:132e/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
valid_lft 86353sec preferred_lft 86353sec
inet6 fe80::344b:6259:4dbe:eabb/64 scope link
valid_lft forever preferred_lft forever
如何在 Ubuntu 16.04 LTS 中新增永久輔助 IP 地址
要在 Ubuntu 系統上新增永久輔助 IP 地址,只需編輯 /etc/network/interfaces
檔案並新增所需 IP 的詳細資訊。
我們不應該在 dns-nameservers
行之後新增輔助 IP 地址,因為它不會起作用,應該以下麵的格式新增 IP 詳情。
此外,我們不需要新增子介面(我們之前在 Ubuntu 14.04 LTS 中的做法):
# vi /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
# The primary network interface
auto enp0s3
iface enp0s3 inet static
address 192.168.56.201
netmask 255.255.255.0
iface enp0s3 inet static
address 192.168.56.202
netmask 255.255.255.0
gateway 192.168.56.1
network 192.168.56.0
broadcast 192.168.56.255
dns-nameservers 8.8.8.8 8.8.4.4
dns-search 2daygeek.local
儲存並關閉檔案,然後重啟網路介面服務:
# systemctl restart networking
或
# ifdown enp0s3 && ifup enp0s3
執行以下命令來檢查是否已經分配了新的 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: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.56.202/24 brd 192.168.56.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe97:132e/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
valid_lft 86353sec preferred_lft 86353sec
inet6 fe80::344b:6259:4dbe:eabb/64 scope link
valid_lft forever preferred_lft forever
讓我來 ping 一下新 IP 地址:
# ping 192.168.56.202 -c 4
PING 192.168.56.202 (192.168.56.202) 56(84) bytes of data.
64 bytes from 192.168.56.202: icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from 192.168.56.202: icmp_seq=2 ttl=64 time=0.087 ms
64 bytes from 192.168.56.202: icmp_seq=3 ttl=64 time=0.034 ms
64 bytes from 192.168.56.202: icmp_seq=4 ttl=64 time=0.042 ms
--- 192.168.56.202 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3068ms
rtt min/avg/max/mdev = 0.019/0.045/0.087/0.026 ms
via: https://www.2daygeek.com/how-to-add-additional-ip-secondary-ip-in-ubuntu-debian-system/
作者:Prakash Subramanian[7] 選題:lujun9972 譯者:MjSeven 校對:wxy
本文由 LCTT 原創編譯,Linux中國 榮譽推出