天天看點

linux同一機器設定多個虛拟IP

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

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

1)先檢視目前的網卡資訊

[[email protected] 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位址

[[email protected] 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

[[email protected] 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

檢視目前路由

[[email protected] ~]# 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

檢視目前路由

[[email protected] ~]# 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完成

[[email protected] ~]# 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)永久性增加對應的路由

[[email protected] sysconfig]# vi /etc/sysconfig/static-routes

增加一條路由

any net 172.16.1.0 gw 172.16.1.254 netmask 255.255.255.0

[[email protected] ~]# vi /etc/sysconfig/network       #這個操作如果沒做,也能連接配接上去,暫不知道影響什麼的

增加一條

GATEWAY=172.16.1.254

3)service network restart

lvs