Linux 一个网卡配置多个ip
仅一个网卡的情况下,这种情况可以让该机器可以通过多个IP被访问,或隐藏常用IP,让他人访问其临时IP。
1.如果临时性的增加一个IP(重启机器或networ服务后,丢失),可以使用ifconfig命令
[root@test network-scripts]# ifconfig eth0:1 172.16.1.222 netmask 255.255.255.0 up
#up表示当即生效,另外,如果想关闭个端口。可以ifconfig eth0:1 down
查看当前路由
[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
设置路由
对应新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完成
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
浙公网安备 33010602011771号