在 CentOS 7 中查看网关信息有以下几种常用方法:
ip route
命令ip route show | grep default
或简写为:
ip r | grep default
输出示例:
default via 192.168.1.1 dev eth0 proto static metric 100
其中 192.168.1.1
就是默认网关地址。
route
命令(需安装 net-tools
)如果系统未安装 net-tools
,先执行:
yum install net-tools -y
然后运行:
route -n
在输出中查找 Destination
为 0.0.0.0
的行,对应的 Gateway
列即为默认网关:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
网关信息通常保存在网卡配置文件中,路径为 /etc/sysconfig/network-scripts/ifcfg-<网卡名>
(如 ifcfg-eth0
):
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep GATEWAY
输出示例:
GATEWAY=192.168.1.1
nmcli
(NetworkManager 工具)nmcli device show <网卡名> | grep IP4.GATEWAY
示例(网卡名为 eth0
时):
nmcli device show eth0 | grep IP4.GATEWAY
输出示例:
IP4.GATEWAY: 192.168.1.1
netstat
命令(需安装 net-tools
)netstat -rn | grep ^0.0.0.0
输出示例:
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
ip route
或 nmcli
查看。default
路由的网关是实际生效的。选择适合你的方法即可快速获取网关信息!