1、僅一個網卡的狀況下,可讓該機器能夠經過多個IP被訪問,或隱藏經常使用IP,讓他人訪問其臨時IP。ide
1.若是臨時性的增長一個IP(重啓機器或network服務後,丟失),可使用ifconfig命令
1)先查看目前的網卡信息
[root@test network-scripts]#ifconfig
eth0Link encap:EthernetHWaddr 00:0C:29:13:94:EB
inet addr:192.168.1.88Bcast:192.168.1.255Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe13:94eb/64 Scope:Link
UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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
loLink encap:Local Loopback
inet addr:127.0.0.1Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNINGMTU:16436Metric: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:1Link encap:EthernetHWaddr 00:0C:29:13:94:EB
inet addr:172.16.1.119Bcast:172.16.1.255Mask:255.255.255.0
UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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
DestinationGatewayGenmaskFlagsMSS Windowirtt Iface
192.168.1.00.0.0.0255.255.255.0U0 00 eth0
172.16.1.00.0.0.0255.255.255.0U0 00 eth0#增長的
0.0.0.0192.168.1.10.0.0.0UG0 00 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
DestinationGatewayGenmaskFlagsMSS Windowirtt Iface
192.168.1.00.0.0.0255.255.255.0U0 00 eth0
172.16.1.0172.16.1.254255.255.255.0UG0 00 eth0#增長的
172.16.1.00.0.0.0255.255.255.0U0 00 eth0
0.0.0.0192.168.1.10.0.0.0UG0 00 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丟失
二、永久性新增一個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
或直接在ifcfg-eth0增長:
IPADDR_1=172.16.1.119/24
LABEL_1='1'
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
oop