侧边栏壁纸
  • 累计撰写 27 篇文章
  • 累计创建 4 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Ubuntu 全系列网卡 IP 配置教程:从桌面到服务器一步到位

陳小航
2025-10-25 / 0 评论 / 0 点赞 / 3 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

扫码_搜索联合传播样式-标准色版.png
Ubuntu 系统中,无论是老版本还是最新版本,正确配置网卡 IP 都是保证网络正常的关键。本文涵盖 Ubuntu 10.04~24.04 全系列,详细说明 临时 IP、永久 IP、单 IP、多 IP 的配置方法,以及桌面和服务器环境下的操作。

一、查看网卡信息

# 查看网卡列表及状态
ip link show

# 查看网卡 IP
ip addr show

# 查看路由表
ip route show

常见网卡名称:

  • Ubuntu 10.04~16.04:eth0, eth1

  • Ubuntu 16.04+:ens33, enp0s3


二、临时配置 IP(重启失效)

1. 推荐方式:ip 命令

# 添加单个 IP
sudo ip addr add 192.168.1.100/24 dev eth0

# 添加多个 IP(示例)
sudo ip addr add 192.168.1.101/24 dev eth0
sudo ip addr add 192.168.1.102/24 dev eth0

# 删除 IP
sudo ip addr del 192.168.1.100/24 dev eth0

# 设置默认网关
sudo ip route add default via 192.168.1.1

2. 老版本可用:ifconfig + route

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
sudo ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0 up  # 多 IP
sudo route add default gw 192.168.1.1

Tip:ifconfig 在新版本已废弃,推荐用 ip


三、永久配置 IP(SSH/服务器专用)

1. Ubuntu 10.04~15.10:/etc/network/interfaces

单 IP

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 8.8.4.4

多 IP(别名接口)

# 主 IP
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 8.8.4.4

# 第二个 IP
auto eth0:0
iface eth0:0 inet static
    address 192.168.1.101
    netmask 255.255.255.0

# 第三个 IP
auto eth0:1
iface eth0:1 inet static
    address 192.168.1.102
    netmask 255.255.255.0

应用配置:

sudo service networking restart

2. Ubuntu 16.04~17.10:混合模式

  • 桌面可用 GUI(忽略)

  • 服务器推荐用 Netplan(参考下方 18.04+)

  • 单 IP / 多 IP 配置方式同上


3. Ubuntu 18.04~24.04:Netplan

单 IP 示例

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

多 IP 示例

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
        - 192.168.1.101/24
        - 192.168.1.102/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

应用配置:

sudo netplan apply

Tip:服务器环境推荐 networkd 渲染器,Netplan 数组支持多个 IP。


四、检查配置

# 查看 IP 是否生效
ip addr show eth0

# 测试网络连通性
ping 8.8.8.8

# 查看默认路由
ip route show

五、总结

Ubuntu 版本

配置方式

多 IP 方法

10.04~15.10

/etc/network/interfaces

别名 eth0:0, eth0:1

16.04~17.10

/etc/network/interfaces 或 Netplan

同上 / Netplan 数组

18.04~24.04

Netplan

addresses 数组直接写多个 IP

临时配置

ip addr add

可以多次添加

  • 临时配置:快速生效,重启失效

  • 永久配置:修改配置文件,重启或 netplan apply 生效

  • 多 IP:老版本用别名,新版本直接写数组

0

评论区