公司默认给配置的 2016 年的 MBP,实在忍受不了退而求其次申请了一台台式机作为开发机、工作站使用。安装的 Ubuntu24.04 LTS 系统需要配置为静态 IP 远程连接上去进行工作。

如下所有操作均使用 root 权限。

记录该过程主要是因为想当然的以为网关地址就是同网段的 .1,导致配置后网络不通,通过 ip route 查询后方可。

查看当前 IP

系统默认开启 DHCP,自动获取 IP,且查看 IP 不再默认支持ifconfig命令,需要 ip addr show进行查看。

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 2c:f0:5d:2b:11:31 brd ff:ff:ff:ff:ff:ff
    altname enp0s31f6
    inet 100.100.57.20/23 brd 100.100.57.255 scope global noprefixroute eno1
       valid_lft forever preferred_lft forever

可以看到自己的 IP 地址为100.100.57.20

配置静态 IP

ubuntu 18 以后版本开始使用netplan配置网卡信息,查看当前系统目录/etc/netplan/下具体配置文件名:

$ ls /etc/netplan
$ 50-cloud-init.yaml

编辑前建议先进行备份。

默认配置:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eno1:
            dhcp4: true
    version: 2

查询路由

通过ip route获取当前路由地址:

$ ip route
$ default via 100.100.56.254 dev eno1 proto static metric 100
$ default via 100.100.56.254 dev eno1 proto dhcp src 100.100.57.20 metric 100
$ 100.100.56.0/23 dev eno1 proto kernel scope link src 100.100.57.20 metric 100

可以看到默认路由地址为100.100.56.254

配置文件

vim /etc/netplan/50-cloud-init.yaml进行编辑,最终内容如下:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    ethernets:
        eno1:
            dhcp4: no
            addresses:
             - 100.100.57.20/23
            routes:
              - to: default
                via: 100.100.56.254
            nameservers:
              addresses:
                - 8.8.8.8
                - 8.8.4.4

注意缩进的格式和位置,不能使用tab只能用空格进行缩进。

应用

netplan apply

如果没有格式错误的话,便没有任何输出,否则提示格式错误。 至此配置成功,通过ip addr show查看确认。

版权声明: 如无特别声明,本文版权归 Mr Chen 所有,转载请注明本文链接。

(采用 CC BY-NC-SA 4.0 许可协议进行授权)

本文标题:《 Ubuntu24.04 配置静态 IP 》

本文链接:https://gbcpp.github.io/notes/ubuntu24.04-static-ip.html

本文最后一次更新为 天前,文章中的某些内容可能已过时!