Linux:Ubuntu18.04 配置靜態IP

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後重新登錄。

【參考鏈接】https://segmentfault.com/a/1190000019454297

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章