下面以 Linux 系统 为例,介绍如何使用 ifconfig 配置 IP 地址。
⚠️ 注意:ifconfig 属于 已逐步淘汰 的命令(net-tools),现代 Linux 推荐用 ip 命令,但很多系统仍可使用 ifconfig。
ifconfig
或指定网卡:
ifconfig eth0
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
sudo ifconfig eth0 up
sudo ifconfig eth0 down
ifconfig 不能设置默认网关,需用 route 或 ip:
sudo route add default gw 192.168.1.1
查看路由:
route -n
sudo ifconfig eth0:1 192.168.1.101 netmask 255.255.255.0
删除:
sudo ifconfig eth0:1 down
sudo ifconfig eth0 broadcast 192.168.1.255
ifconfig 配置是 临时的,重启失效。
永久配置取决于发行版:
/etc/network/interfaces
/etc/sysconfig/network-scripts/ifcfg-eth0
使用 NetworkManager 或 systemd-networkd
ip addr
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip link set eth0 up
sudo ip route add default via 192.168.1.1
❓ 看不到 ifconfig?
sudo apt install net-tools # Debian/Ubuntu
sudo yum install net-tools # CentOS 7
❓ 没有 eth0? 可能是:
ens33enp0s3wlan0(无线)用 ip addr 或 ifconfig -a 查看真实网卡名。
如果你愿意,我可以:
ifconfig 和 ip 命令对照表