Ubuntu18.04 配置靜態IP
在 Ubuntu18.04 中已經使用netplan
替換ifupdown
,所有需要通過配置netplan
來配置靜態IP,netplan
使用的配置文件爲yaml
,感興趣的朋友可以自行上網搜索yaml
相關資料,再次不做過多介紹,推薦幾篇博文:
1、修改配置
打開/etc/netplan
路徑下的yaml
文件,博主的文件爲/etc/netplan/50-cloud-init.yaml
,不同服務器,yaml文件可能不同,根據實際情況而定,使用cat /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:
eno1:
dhcp4: true
version: 2
備份原文件,可根據實際情況修改文件名稱:
sudo cp /etc/netplan/50-cloud-init.yam /etc/netplan/50-cloud-init.yam.bak
使用sudo nano /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:
eno1: #配置的網卡名稱
dhcp4: no #dhcp4關閉
dhcp6: no #dhcp6關閉
addresses: [10.5.0.223/23] #設置本機IP及掩碼
gateway4: 10.5.1.254 #設置網關
nameservers:
addresses: [114.114.114.114, 8.8.8.8] #設置DNS
version: 2
通過Ctrl+X
退出,輸入y
保存。
2、應用配置
使用如下指令應用配置:
sudo netplan apply
如果使用ssh登錄,ssh會掉線,需要修改IP後重新登錄。