在 Linux 中,ifconfig 本身不能直接设置网关。ifconfig 只负责配置网络接口(IP 地址、子网掩码等),网关需要通过 route 或 ip 命令来设置。
下面按常见情况说明:
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
route add default gw 192.168.1.1
查看网关:
route -n
删除网关(如需):
route del default gw 192.168.1.1
ifconfig 已逐渐被 ip 取代。
ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
ip route add default via 192.168.1.1
查看:
ip route
以上方法 重启后失效。
编辑:
/etc/sysconfig/network-scripts/ifcfg-eth0
示例:
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
编辑:
/etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
ifconfig:只能设 IProute / ip route如果你告诉我 Linux 发行版 + 版本,我可以给你最准确的方法。