linux同一機器設置多個虛擬IP

一.僅一個網卡的情況下,這種情況可以讓該機器可以通過多個IP被訪問,或隱藏常用IP,讓他人訪問其臨時IP。

 

1.如果臨時性的增加一個IP(重啓機器或networ服務後,丟失),可以使用ifconfig命令

 

1)先查看目前的網卡信息

 

[root@test network-scripts]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:13:94:EB
          inet addr:192.168.1.88  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe13:94eb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3412 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1544 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:437408 (427.1 KiB)  TX bytes:189062 (184.6 KiB)
          Base address:0x2040 Memory:e8920000-e8940000

 

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:16436  Metric:1
          RX packets:44 errors:0 dropped:0 overruns:0 frame:0
          TX packets:44 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4546 (4.4 KiB)  TX bytes:4546 (4.4 KiB)

 

表明現在機器上只有一個網卡,端口爲eth0

 

2)新增一個虛擬端口,並配置IP地址

[root@test network-scripts]# ifconfig eth0:1 172.16.1.222 netmask 255.255.255.0 up    

 

                        #up表示當即生效,另外,如果想關閉個端口。可以ifconfig eth0:1 down

 

執行命令後,ifconfig多出一個端口信息


eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:13:94:EB
          inet addr:172.16.1.119  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Base address:0x2040 Memory:e8920000-e8940000

且能夠ping通新增的IP

[root@test network-scripts]# ping 172.16.1.222
PING 172.16.1.222 (172.16.1.222) 56(84) bytes of data.
64 bytes from 172.16.1.222: icmp_seq=0 ttl=64 time=3.29 ms

查看當前路由

[root@test ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
172.16.1.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0      #增加的
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0

 

3)設置路由

對應新IP,新增一個網段,使這個網段能夠訪問

route add -net 172.16.1.0 netmask 255.255.255.0 gw 172.16.1.254 eth0:1

 

查看當前路由

[root@test ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
172.16.1.0      172.16.1.254    255.255.255.0   UG        0 0          0 eth0       #增加的
172.16.1.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0

此時ping 172.16.1.118這臺機器,ping通,表示臨時新增IP完成

[root@test ~]# ping 172.16.1.118
PING 172.16.1.118 (172.16.1.118) 56(84) bytes of data.
64 bytes from 172.16.1.118: icmp_seq=0 ttl=64 time=0.147 ms

注:這是臨時使用的辦法,如重啓network或重啓機器。則新增的IP丟失

 

2.永久性新增一個IP

1)仿照/etc/sysconfig/network-scripts/ifcfg-eth0文件,增加一個新增虛擬端口的文件

如ifcfg-eth0:1

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:1

vi /etc/sysconfig/network-scripts/ifcfg-eth0:1

修改成

DEVICE=eth0:1
#BOOTPROTO=dhcp
BOOTPROTO=static
HWADDR=00:0C:29:13:94:EB
ONBOOT=yes
IPADDR=172.16.1.119
NETMASK=255.255.255.0
TYPE=Ethernet
GATEWAY=172.16.1.254

2)永久性增加對應的路由

[root@test sysconfig]# vi /etc/sysconfig/static-routes
增加一條路由

any net 172.16.1.0 gw 172.16.1.254 netmask 255.255.255.0

[root@test ~]# vi /etc/sysconfig/network       #這個操作如果沒做,也能連接上去,暫不知道影響什麼的
增加一條

GATEWAY=172.16.1.254

 

3)service network restart

發佈了25 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章