您的位置:首页 > 理论基础 > 计算机网络

内网无路由跨网络服务——单网卡双IP配置

2016-09-04 16:35 447 查看
有些时候,在我们内网服务中,如果不想使用路由功能,又想让某些服务器能够跨网络服务,那么此时,就需要在服务器网卡上配置多个IP地址,让它能够提供多个网络的访问,那么在linux下该如何实现了,今天就给大家分享一下。

操作步骤
1、cp eth0 eth0:1将网卡接口配置文件复制一份命名为eth0:1
2、修改网卡的具体配置信息
注意:eth0可以用DHCP,可以静态配置
eth0:1只能用静态配置
既然我们是服务器,当然两个配置都使用静态IP最好了
3、重启网络服务(确保NetworkManager服务处于停止状态)
4、验证最终效果

第一步:cp eth0 eth0:1将网卡接口配置文件复制一份命名为eth0:1
[root@Centos6 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1

第二步:修改网卡的具体配置信息
[root@Centos6 network-scripts]# vim ifcfg-eth0
[root@Centos6 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
IPADDR=10.1.253.253
PREFIX=16
[root@Centos6 network-scripts]# cat ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=none
IPADDR=192.168.253.253
PREFIX=24
[root@Centos6 network-scripts]#

第三步:重启网络服务(确保NetworkManager服务处于停止状态)
注意系统默认会开启NetworkManager服务,要想启用单网卡双IP,最好先关闭,否则会不生效
[root@Centos6 network-scripts]# service NetworkManager status
NetworkManager is stopped

[root@Centos6 network-scripts]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Determining if ip address 10.1.253.253 is already in use for device eth0...
Determining if ip address 192.168.253.253 is already in use for device eth0...
[ OK ]

[root@Centos6 network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:72:26
inet addr:10.1.253.253 Bcast:10.1.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fec8:7226/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1185 errors:0 dropped:0 overruns:0 frame:0
TX packets:366 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:107195 (104.6 KiB) TX bytes:38655 (37.7 KiB)

eth0:1 Link encap:Ethernet HWaddr 00:0C:29:C8:72:26
inet addr:192.168.253.253 Bcast:192.168.253.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

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:65536 Metric:1
RX packets:260 errors:0 dropped:0 overruns:0 frame:0
TX packets:260 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:18160 (17.7 KiB) TX bytes:18160 (17.7 KiB)

[root@Centos6 network-scripts]#

第四步:验证最终效果,从Centos7去pingCentos6上配置的双IP
[root@centos7 network-scripts]# ifconfig
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.1.254.254 netmask 255.255.0.0 broadcast 10.1.255.255
inet6 fe80::20c:29ff:fe06:f987 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:06:f9:87 txqueuelen 1000 (Ethernet)
RX packets 133425 bytes 10462696 (9.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 61400 bytes 6585145 (6.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eno16777728:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.253.254 netmask 255.255.255.0 broadcast 192.168.253.255
ether 00:0c:29:06:f9:87 txqueuelen 1000 (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 2496 bytes 234576 (229.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2496 bytes 234576 (229.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@centos7 network-scripts]# ping -c 4 10.1.253.253
PING 10.1.253.253 (10.1.253.253) 56(84) bytes of data.
64 bytes from 10.1.253.253: icmp_seq=1 ttl=64 time=3.41 ms
64 bytes from 10.1.253.253: icmp_seq=2 ttl=64 time=0.536 ms
64 bytes from 10.1.253.253: icmp_seq=3 ttl=64 time=0.539 ms
64 bytes from 10.1.253.253: icmp_seq=4 ttl=64 time=0.738 ms

--- 10.1.253.253 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3029ms
rtt min/avg/max/mdev = 0.536/1.306/3.413/1.219 ms
[root@centos7 network-scripts]# ping -c 4 192.168.253.254
PING 192.168.253.254 (192.168.253.254) 56(84) bytes of data.
64 bytes from 192.168.253.254: icmp_seq=1 ttl=64 time=0.135 ms
64 bytes from 192.168.253.254: icmp_seq=2 ttl=64 time=0.126 ms
64 bytes from 192.168.253.254: icmp_seq=3 ttl=64 time=0.126 ms
64 bytes from 192.168.253.254: icmp_seq=4 ttl=64 time=0.128 ms

--- 192.168.253.254 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3017ms
rtt min/avg/max/mdev = 0.126/0.128/0.135/0.014 ms
[root@centos7 network-scripts]#
可以看出,两个IP地址都可以ping通,所以如果是跨网络服务时,单网卡双IP的功效就体现出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  路由 内网 跨网