在 Linux 里用 ifconfig 本身只能临时配置 IP,不能永久生效(重启或重启网络就会丢失)。下面分情况说明。
假设网卡是 eth0,要配置:
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
route add default gw 192.168.1.1
查看是否生效:
ifconfig eth0
route -n
⚠️ 这只是临时配置,系统重启后失效。
编辑:
vi /etc/network/interfaces
示例:
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
重启网络:
/etc/init.d/networking restart
编辑:
vi /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
DNS1=8.8.8.8
重启网络:
service network restart
nmcli connection modify eth0 \
ipv4.method manual \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns 8.8.8.8
nmcli connection up eth0
编辑:
vi /etc/netplan/01-netcfg.yaml
示例:
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8]
应用:
netplan apply
ifconfig 已逐渐被 ip 命令取代ip addr add 192.168.1.100/24 dev eth0
如果你告诉我 具体 Linux 发行版和版本,我可以给你最精确的配置命令。