在 Ubuntu 系统中,使用 ifconfig 命令本身并不能直接设置网关地址。要设置网关,你需要使用 route 命令或者修改网络配置文件。以下是两种方法:
方法一:使用 route 命令
首先,找到你的网络接口名称。运行 ifconfig 或 ip addr 命令,查看你的网络接口名称,例如:eth0 或 ens33。
使用 route 命令添加默认网关。将 your_gateway_ip 替换为你的网关 IP 地址,将 your_interface_name 替换为你的网络接口名称:
sudo route add default gw your_gateway_ip your_interface_name
例如:
sudo route add default gw 192.168.1.1 eth0
方法二:修改网络配置文件
/etc/network/interfaces。使用文本编辑器打开它,例如:sudo nano /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
dhcp 更改为 static,然后添加 gateway 行,将 your_gateway_ip 替换为你的网关 IP 地址:auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
保存文件并退出编辑器。
重启网络服务以应用更改:
sudo systemctl restart networking
或者重启计算机。
注意:这些方法适用于传统的 Ubuntu 系统。对于使用 Netplan 的较新版本(例如 Ubuntu 18.04 及更高版本),你需要修改 /etc/netplan 目录下的 YAML 配置文件。