业精于勤
而荒于嬉

Ubuntu Server 20.04 配置静态IP


Ubuntu Server 20.04 LTS 使用 Netplan 作为默认的网络配置。

1. 查看需配置静态IP的网络接口名称

ubuntu@ubuntu-server:~$ ip a
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 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:94:5e:cb brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.102/24 brd 192.168.1.255 scope global dynamic ens160
       valid_lft 6961sec preferred_lft 6961sec
    inet6 fc00:e9c4:a8ca:1::a4d/128 scope global dynamic noprefixroute 
       valid_lft 6963sec preferred_lft 6963sec
    inet6 fc00:e9c4:a8ca:1:250:56ff:fe94:5ecb/64 scope global mngtmpaddr noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe94:5ecb/64 scope link 
       valid_lft forever preferred_lft forever

可以看到,网络接口名称为 ens160 ,其IP为192.168.1.102

2. 确认关闭 CloudInit

打开配置文件 /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg,确认文件包含下行内容:

network: {config: disabled}

3. 编辑 Netplan 配置文件

Netplan 默认的配置文件为/etc/netplan/00-installer-config.yaml

默认内容如下:

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      dhcp4: true
      nameservers:
        addresses:
        - 210.72.131.131
        - 114.114.114.114
        search:
        - 192.168.1.1
  version: 2

修改内容如下,确保配置文件没有任何语法错误

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      dhcp4: no
      addresses:
        - 192.168.1.201/24
      gateway4: 192.168.1.2
      nameservers:
        addresses:
        - 210.72.131.131
        - 114.114.114.114
        search:
        - 192.168.1.1
  version: 2

4. 永久 Netplan 配置

执行命令 sudo netplan try ,如果没有报错,将会看到下面内容,按 Enter 继续。

ubuntu@ubuntu-server:~$ sudo netplan try
Warning: Stopping systemd-networkd.service, but it can still be activated by:
  systemd-networkd.socket
Do you want to keep these settings?

Press ENTER before the timeout to accept the new configuration

Changes will revert in 119 seconds

永久配置生效,执行命令: sudo netplan apply

sudo netplan apply

5. 确认IP已更改

再次执行命令 ip a

ubuntu@ubuntu-server:~$ ip a
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 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:94:5e:cb brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.201/24 brd 192.168.1.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fc00:e9c4:a8ca:1::a4d/128 scope global dynamic noprefixroute 
       valid_lft 7134sec preferred_lft 7134sec
    inet6 fc00:e9c4:a8ca:1:250:56ff:fe94:5ecb/64 scope global mngtmpaddr noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe94:5ecb/64 scope link 
       valid_lft forever preferred_lft forever

可以看到IP已经更新为192.168.1.201,无需重启系统。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Ubuntu Server 20.04 配置静态IP》
文章链接:https://ithuang.net/ubuntu-server-20-04-static-ip.html
本站资源仅供个人学习交流,本人版权所有。欢迎转载,转载请注明作者及来源链接,谢谢。