温馨提示×

linux ifconfig如何配置ip

小樊
46
2026-09-05 02:56:51
栏目: 智能运维

在 Linux 中,ifconfig 可以用来临时配置 IP 地址(重启网络或系统后失效)。下面是常见用法。


一、查看网络接口

ifconfig

ifconfig -a

二、配置 IP 地址(临时)

1️⃣ 配置 IPv4 地址

ifconfig eth0 192.168.1.100 netmask 255.255.255.0

说明:

  • eth0:网卡名(可能是 ens33enp0s3 等)
  • 192.168.1.100:IP 地址
  • netmask:子网掩码

2️⃣ 配置网关(ifconfig 不能直接配网关)

route add default gw 192.168.1.1

ip route add default via 192.168.1.1

3️⃣ 启用 / 禁用网卡

ifconfig eth0 up
ifconfig eth0 down

4️⃣ 配置并立刻启用

ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

三、配置多个 IP(子接口)

ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0

四、⚠️ 重要说明

  • ifconfig 已逐步被弃用
  • 永久配置 IP 应使用:
    • CentOS / RHEL:/etc/sysconfig/network-scripts/
    • Ubuntu(老):/etc/network/interfaces
    • Ubuntu(新):netplan
  • 推荐命令:ip addrip route

五、推荐替代方式(临时)

ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up

如果你告诉我 Linux 发行版(CentOS / Ubuntu 等)是否要永久生效,我可以给你具体配置方法。

0