插件窝 干货文章 centos7怎么看网关

centos7怎么看网关

网关 Gateway 示例 690    来源:    2025-04-08

在 CentOS 7 中查看网关信息有以下几种常用方法:


方法 1:使用 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 就是默认网关地址。


方法 2:使用 route 命令(需安装 net-tools

如果系统未安装 net-tools,先执行:

yum install net-tools -y

然后运行:

route -n

在输出中查找 Destination0.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

方法 3:查看网络配置文件

网关信息通常保存在网卡配置文件中,路径为 /etc/sysconfig/network-scripts/ifcfg-<网卡名>(如 ifcfg-eth0):

cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep GATEWAY

输出示例:

GATEWAY=192.168.1.1

方法 4:使用 nmcli(NetworkManager 工具)

nmcli device show <网卡名> | grep IP4.GATEWAY

示例(网卡名为 eth0 时):

nmcli device show eth0 | grep IP4.GATEWAY

输出示例:

IP4.GATEWAY:                         192.168.1.1

方法 5:通过 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

注意事项

  1. 如果系统使用 DHCP 自动获取 IP,网关可能不会直接写在配置文件中,需通过 ip routenmcli 查看。
  2. 多网卡环境下可能有多个网关,但只有 default 路由的网关是实际生效的。

选择适合你的方法即可快速获取网关信息!