温馨提示×

linux ifconfig如何修改地址

小樊
51
2026-10-10 21:28:31
栏目: 智能运维

在 Linux 中,ifconfig 可以用来临时修改网络接口的 IP 地址(重启网络或系统后会失效)。永久修改需要在发行版对应的网络配置文件里改。

下面分情况说明:


一、使用 ifconfig 临时修改 IP 地址

1️⃣ 查看当前网卡

ifconfig

或

ip addr

常见网卡名:eth0、ens33、enp0s3


2️⃣ 修改 IP 地址(示例)

假设要把 eth0 改成:

  • IP:192.168.1.100
  • 子网掩码:255.255.255.0
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

3️⃣ 添加多个 IP(不覆盖原 IP)

sudo ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0

4️⃣ 修改网关(ifconfig 不能改网关)

需要用 route 或 ip:

sudo route add default gw 192.168.1.1

或

sudo ip route add default via 192.168.1.1

5️⃣ 启用 / 禁用网卡

sudo ifconfig eth0 down
sudo ifconfig eth0 up

二、⚠️ 重要说明

  • ifconfig 只能临时修改
  • 修改后立即生效,但:
    • 重启网络 → 失效
    • 重启系统 → 失效
  • 新版本 Linux 更推荐用 ip 命令:
sudo ip addr add 192.168.1.100/24 dev eth0

三、永久修改(按发行版)

✅ Ubuntu / Debian(旧版)

sudo nano /etc/network/interfaces
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 18.04+(Netplan)

sudo nano /etc/netplan/01-netcfg.yaml
network:
  ethernets:
    eth0:
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8]
sudo netplan apply

✅ CentOS / RHEL

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
sudo systemctl restart network

如果你告诉我:

  • Linux 发行版
  • 网卡名称
  • 是否要永久生效

我可以给你完全对应的命令。

0 踩