找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 341|回复: 3

Linux同一机器设置多个IP

[复制链接]

205

主题

172

回帖

6921

积分

论坛元老

积分
6921
发表于 2013-6-5 09:28:50 | 显示全部楼层 |阅读模式
一.仅一个网卡的情况下,这种情况可以让该机器可以通过多个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 Scopeink
          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 encapocal 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

增加一条
GATEWAY=172.16.1.254

3)service network restart

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-6-5 19:30:35 | 显示全部楼层
好!收下了。

0

主题

1

回帖

0

积分

新手上路

积分
0
发表于 2013-6-9 14:03:06 | 显示全部楼层
,收下了

0

主题

4

回帖

0

积分

新手上路

积分
0
发表于 2013-8-14 16:03:11 | 显示全部楼层
好,不错!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 05:54 , Processed in 0.161189 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表