针对RHEL中双网卡IP不能同时被访问的解决方法

简介:

环境简述:

服务器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



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

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

可以根据需要将静态路由命令添加至相关配置文件,重启之后仍然生效。











本文转自 暗黑魔君 51CTO博客,原文链接:http://blog.51cto.com/clovemfong/1272075,如需转载请自行联系原作者
目录
相关文章
|
18天前
|
安全 Linux 数据库连接
Linux网卡IP地址配置错误的影响🐧🔧
在Linux系统中,网络配置是保持系统顺畅运行的关键一环。正确配置网卡的IP地址对于确保网络通信的准确性和效率至关重要。然而,如果在这个过程中发生错误,可能会带来一系列问题。让我们一起探讨一下,如果Linux网卡的IP地址配置错误,会有什么影响。
Linux网卡IP地址配置错误的影响🐧🔧
|
9月前
|
网络协议 Linux 开发工具
Centos7虚拟机修改IP地址改为静态IP
Centos7虚拟机修改IP地址改为静态IP
727 0
|
Linux 虚拟化 Windows
linux主机模式(Host-Only)的网络配置
linux主机模式(Host-Only)的网络配置
linux主机模式(Host-Only)的网络配置
|
Linux
Linux中一个网卡含有多个IP,将从IP升级为主IP的方法
Linux中一个网卡含有多个IP,将从IP升级为主IP的方法
612 0
|
Linux
linux永久更改eth0的ip地址
linux中永久修改ip和子网掩码,可以用命令也可以直接到文件里修改。这里推荐直接到文件中修改。1、进入网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ethN (ifcfg-ethN是你要修改的网卡,可以用ifconfig查看你要修改的网卡)2、进入文件进行编辑,更改其中的ip 和子网掩码。
2365 0
|
Linux 开发工具
centos7下设置使用NAT模式下的静态ip的步骤
centos7下设置使用NAT模式下的静态ip的步骤
120 0
centos7下设置使用NAT模式下的静态ip的步骤
|
Linux 运维 安全
linux内网机器访问外网代理设置squid
公司一般出于安全考虑, 在同一局域网中只有一台机器可以访问外网,运维进行了整体的限制, 但是在后面的工作中,需要在机器上安装一些软件,及命令,所以其他的机器需要访问外网来简化工作, 但又不能打乱原有运维的设置,所以需要在能访问外网的机器上做个代理。
2330 0