在 CentOS 系统中,使用 ifconfig 命令本身并不能直接设置网关。ifconfig 主要用于配置和显示网络接口的参数,如 IP 地址、子网掩码等。要设置网关,你需要使用 route 命令或修改 /etc/sysconfig/network-scripts/route-eth0 文件(其中 eth0 是你的网络接口名称)。
以下是使用 route 命令设置网关的方法:
route -n
192.168.1.1,可以使用以下命令添加:route add default gw 192.168.1.1
/etc/sysconfig/network-scripts/route-eth0 文件(将 eth0 替换为你的网络接口名称):vi /etc/sysconfig/network-scripts/route-eth0
在文件中添加以下内容:
192.168.1.1 via 192.168.1.1 dev eth0
将 192.168.1.1 替换为你的默认网关地址,将 eth0 替换为你的网络接口名称。
systemctl restart network
现在,你的 CentOS 系统应该已经设置了正确的默认网关。你可以再次使用 route -n 命令查看路由表,确认默认网关已设置。