ifconfig 命令主要用于配置和显示网络接口的参数,但它本身并不直接支持设置网关地址。要配置网关地址,你需要使用 route 命令或编辑 /etc/network/interfaces 文件(在某些发行版中可能是 /etc/sysconfig/network-scripts/ifcfg-eth0 或其他类似的文件)。
以下是使用 route 命令设置网关地址的方法:
192.168.1.1 替换为你的实际网关地址):sudo route add default gw 192.168.1.1
/etc/network/interfaces 文件(以 Debian/Ubuntu 为例):sudo nano /etc/network/interfaces
eth0),并添加以下行(将 192.168.1.1 替换为你的实际网关地址):gateway 192.168.1.1
文件示例:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
sudo systemctl restart networking
或者,在某些发行版中,你可以使用以下命令:
sudo /etc/init.d/networking restart
现在,你的网关地址应该已经配置好了。你可以使用 route -n 或 ip route 命令查看路由表,确认网关地址已设置。