当前位置: > CentOS > CentOS故障 >

Centos服务器双网卡IP不能同时被访问的解决方法

时间:2014-06-23 22:21来源:linux.it.net.cn 作者:IT网
环境简述:

服务器A具备双网卡,安装操作系统RHEL6.3

----------------------------------------------------------

网卡显示名称       IP地址                网关  

----------------------------------------------------------

eth0              192.168.153.4    192.168.153.1

eth1              192.168.152.4    192.168.152.1

-----------------------------------------------------------

网卡配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16  [root@clovem ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.168.153.4
NETMASK=255.255.255.0
GATEWAY=192.168.153.1
[root@clovem ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.168.152.4
NETMASK=255.255.255.0
GATEWAY=192.168.152.1 

重启网络服务

[root@clovem ~]# service network restart

查看其路由信息:

[root@clovem ~]# route  

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.153.0   *               255.255.255.0   U     0      0        0 eth0

192.168.152.0   *               255.255.255.0   U     0      0        0 eth1

link-local      *               255.255.0.0     U     1015   0        0 eth0

link-local      *               255.255.0.0     U     1016   0        0 eth1

default        192.168.152.1    255.255.255.0   UG    1017   0        0  eth1

可以发现双网卡的默认网关为192.168.152.1

从其他网段192.168.151.0/24的某台测试机:192.168.150.252

访问192.168.153.4以及192.168.152.4 ,只能通过192.168.152.4进行网络连接,而192.168.153.4却不可以,从上面的路由表中可以看出两个网卡配置文件中的网关参数只有192.168.152.1生效,并被设置为默认网关。

解决方法:

1.将两个网卡配置文件中的GATEWAY参数全部删除,即

1
2
3
4
5
6
7
8
9
10
11
12
13
14  [root@clovem ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.168.153.4
NETMASK=255.255.255.0
[root@clovem ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.168.152.4
NETMASK=255.255.255.0 

2. 重启网络

[root@clovem ~]# service network restart

3.查看路由表

[root@clovem ~]# route  

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.153.0   *               255.255.255.0   U     0      0        0 eth0

192.168.152.0   *               255.255.255.0   U     0      0        0 eth1

link-local      *               255.255.0.0     U     1015   0        0 eth0

link-local      *               255.255.0.0     U     1016   0        0 eth1

发现默认网关没有被设置

4. 添加静态路由表

1
2
3
4
5
6
7
8  [root@clovem ~]# echo "153 net_153" >>  /etc/iproute2/rt_tables
[root@clovem ~]# echo "152 net_152" >>  /etc/iproute2/rt_tables
[root@clovem ~]# ip  route flush table net_153
[root@clovem ~]# ip  route add default via 192.168.153.1 dev  eth0 src 192.168.153.4   table net_153
[root@clovem ~]# ip  rule  add from 192.168.153.4 table net_153
[root@clovem ~]# ip  route flush table net_152
[root@clovem ~]# ip  route add default via 192.168.152.1 dev  eth0 src 192.168.153.4   table net_152
[root@clovem ~]# ip  rule  add from 192.168.152.4 table net_152 




5. 此时再次通过外部测试机访问均可

1
2
3
4
5
6
7
8
9  [root@storage252 ~]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:82:56:55
          inet addr:192.168.150.252  Bcast:192.168.150.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fe82:5655/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25910143 errors:0 dropped:0 overruns:0 frame:0
          TX packets:19888495 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:26258149440 (24.4 GiB)  TX bytes:16323278977 (15.2 GiB) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14  [root@storage252 ~]# ping -W1 -c2 192.168.152.4
PING 192.168.152.4 (192.168.152.4) 56(84) bytes of data.
64 bytes from 192.168.152.4: icmp_seq=1 ttl=63 time=0.324 ms
64 bytes from 192.168.152.4: icmp_seq=2 ttl=63 time=0.349 ms
--- 192.168.152.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.324/0.336/0.349/0.022 ms
[root@storage252 ~]# ping -W1 -c2 192.168.153.4
PING 192.168.153.4 (192.168.153.4) 56(84) bytes of data.
64 bytes from 192.168.153.4: icmp_seq=1 ttl=63 time=0.342 ms
64 bytes from 192.168.153.4: icmp_seq=2 ttl=63 time=0.271 ms
--- 192.168.153.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.271/0.306/0.342/0.039 ms 

--------------------------------------------------------------------------------


最后还需要添加一条默认网关,才能让该系统访问其他网段主机

[root@clovem ~]# route add default gw 192.168.153.1

可以根据需要将静态路由命令添加至相关配置文件,重启之后仍然生效。
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容