[Ubuntu系统] Ubuntu22.04 配置静态IP

1178 0
Honkers 2025-3-5 21:38:41 | 显示全部楼层 |阅读模式

ubuntu 22.04 配置静态ip

1. 适用系统

本文介绍的静态 ip 的配置方法适用于 Ubuntu 系统的 18.04 及其以上版本。

2. 确认以太网连接的网络接口

一般情况下的个人 PC 只会有一张网卡,但在服务器中可能存在多张网卡的情况,使用 ifconfig 命令查看对应 ip 的网络接口。
若提示未找到 ifconfig 命令则使用如下命令按装该工具。

  1. imaginemiracle:~$ sudo apt-get install net-tools
复制代码

查看需要修改的网卡,如下可以查到当前所有的网络接口信息,由于我的主机只有一块网卡,因此就只对它修改即可。

  1. imaginemiracle:~$ ifconfig
  2. imaginemiracle:~$ ifconfig
  3. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  4. inet 192.168.253.129 netmask 255.255.255.0 broadcast 192.168.253.255
  5. inet6 fe80::bbd9:a68d:6982:fa33 prefixlen 64 scopeid 0x20<link>
  6. ether 00:0c:29:b8:7a:f4 txqueuelen 1000 (Ethernet)
  7. RX packets 11433 bytes 15280828 (15.2 MB)
  8. RX errors 0 dropped 0 overruns 0 frame 0
  9. TX packets 5919 bytes 391957 (391.9 KB)
  10. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  11. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  12. inet 127.0.0.1 netmask 255.0.0.0
  13. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  14. loop txqueuelen 1000 (Local Loopback)
  15. RX packets 911 bytes 68550 (68.5 KB)
  16. RX errors 0 dropped 0 overruns 0 frame 0
  17. TX packets 911 bytes 68550 (68.5 KB)
  18. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
复制代码

3. 修改网卡默认配置文件

其默认配置信息的路径如下。

  1. imaginemiracle:~$ sudo vim /etc/netplan/01-network-manager-all.yaml
复制代码

若为修改过的文件应该会是这样:

  1. # Let NetworkManager manage all devices on this system
  2. network:
  3. version: 2
  4. renderer: NetworkManager
复制代码

将其修改为静态 ip 的模式。

3.1. ubuntu20.04 / 18.04 参考以下配置

  1. network:
  2. ethernets:
  3. ens33:
  4. dhcp4: no
  5. addresses: [192.168.1.10/24]
  6. optional: true
  7. gateway4: 192.168.1.1
  8. nameservers:
  9. addresses: [114.114.114.114,8.8.8.8]
  10. version: 2
复制代码

应用该配置

  1. iamginemiracle:~$ sudo netplan apply
复制代码

3.2. ubuntu22.04 参考以下配置

  1. network:
  2. ethernets:
  3. ens33:
  4. dhcp4: no
  5. dhcp6: no
  6. addresses:
  7. - 192.168.0.10/24
  8. routes:
  9. - to: default
  10. via: 192.168.0.1
  11. nameservers:
  12. addresses:
  13. - 114.114.114.114
  14. - 8.8.8.8
  15. version: 2
  16. renderer: networkd
复制代码

应用该配置

  1. iamginemiracle:~$ sudo netplan apply
复制代码

3.3. 注意

若 Ubuntu22.04 应用 ubuntu20.04 /18.04 的配置来设置静态 IP,当使用 netplan apply 则会得到如下警告。

  1. imaginemiracle:~$ sudo netplan apply
  2. ** (generate:3591): WARNING **: 11:29:00.676: `gateway4` has been deprecated, use default routes instead.
  3. See the 'Default routes' section of the documentation for more details.
  4. ** (process:3589): WARNING **: 11:29:00.788: `gateway4` has been deprecated, use default routes instead.
  5. See the 'Default routes' section of the documentation for more details.
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行