• 前言:Ubuntu从18.04 开始不再使用/etc/network/interfaces配置网络,而是使用netplan

    1. 打开/etc/netplan目录,可以看到有对应的网络配置文件

      1. comedian@comedian:~$ cd /etc/netplan/
      2. comedian@comedian:/etc/netplan$ ls
      3. 50-cloud-init.yaml
    2. 修改配置文件,注意yaml语法

      comedian@comedian:/etc/netplan$ sudo vim 50-cloud-init.yaml
      # This file is generated from information provided by                     
      # the datasource.  Changes to it will not persist across an instance.     
      # 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:                                                            
           enp0s3:         # 网卡名                                                  
               dhcp4: no   # 关闭dhcp                                                  
               dhcp6: no                                                  
               addresses: [192.168.1.120/24]        # 设置静态ip                         
               gateway4: 192.168.1.1                # 设置网关                         
               nameservers:                                                  
                       addresses: [114.114.114.114,8.8.8.8]  # 设置dns                
       version: 2
      
    3. 应用配置修改

      comedian@comedian:/etc/netplan$ sudo netplan apply