ubuntu server 18.04配置静态IP

标签(空格分隔): ubuntu配置静态IP


介绍以前配置静态IP方法

以前的版本都是编辑 vim /etc/network/interfaces文件修改IP
但是ubuntuserver18.04之后的版本如果还想用此方法就得重新安装一下ifupdown

sudo apt install ifupdown
sudo vim /etc/network/interfaces

然后配置ip

iface enp0s3 inet static
address 192.168.31.88
gateway 192.168.31.1
netmask 255.255.255.0
dns-nameservers 192.168.31.1

重启网络服务使配置生效

sudo services network restrart

这里简单记录一下使用新工具netplan配置静态IP

在/etc/netplan/目录下默认的配置文件名是50-cloud-init.yaml,我们通过VIM修改它:

cyalias@cc:~$ sudo 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.
# 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:
            addresses: []
            dhcp4: true
    version: 2
~ 
~
~

开始配置静态IP

# 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:
            addresses: [192.168.31.88/24]
            gateway4: 192.168.31.1
            nameservers:
                    addresses: [192.168.31.1]
    version: 2
~   
~

然后使用以下命令使配置即时生效

cyalias@cc:~$ sudo netplan apply