目录
- Ubuntu通过Netplan配置网络教程
- 一、Netplan配置流程
- 二、Netplan配置文件详解
Ubuntu通过Netplan配置网络教程
UbuntuthroughNetplanconfigurationnetworktutorial
一、Netplan配置流程
1.Netplanconfigurationprocess
1、Netplan默认配置文件在/etc/netplan目录下。您可以使用以下命令找到:
1.ThedefaultconfigurationfileofNetplanisinthe/etc/netplandirectory.Youcanfinditwiththefollowingcommand:
ls /etc/netplan/
就可以看到配置文件名称。
Youcanseetheconfigurationfilename.
2、查看Netplan网络配置文件的内容,执行以下命令:
2.ViewthecontentsoftheNetplannetworkconfigurationfileandexecutethefollowingcommand:
cat /etc/netplan/*.yaml
3、现在你需要在任何编辑器中打开配置文件:由于我使用 vim 编辑器来编辑配置文件,所以我将运行:
3.Nowyouneedtoopentheconfigurationfileinanyeditor:SinceIusethevimeditortoedittheconfigurationfile,Iwillrun:
vim /etc/netplan/*.yaml
根据您的网络需要更新配置文件。对于静态 IP 寻址,添加 IP 地址、网关、DNS 信息,而对于动态 IP 寻址,无需添加此信息,因为它将从 DHCP 服务器获取此信息。使用以下语法编辑配置文件。
Updatetheconfigurationfileaccordingtoyournetworkneeds.ForstaticIPaddressing,addIPaddress,gateway,DNSinformation,andfordynamicIPaddressing,thereisnoneedtoaddthisinformationbecauseitwillgetthisinformationfromtheDHCPserver.Usethefollowingsyntaxtoedittheconfigurationfile.
4、在应用任何更改之前,我们将测试配置文件。
4.Wewilltesttheconfigurationfilebeforeapplyinganychanges.
sudo netplan try
如果没有问题,它将返回配置接受消息。如果配置文件未通过测试,它将恢复为以前的工作配置。
Ifthereisnoproblem,itwillreturnaconfigurationacceptancemessage.Iftheconfigurationfilefailsthetest,itwillreverttothepreviousworkingconfiguration.
5、运行以下命令来应用新配置:
5.Runthefollowingcommandtoapplythenewconfiguration:
sudo netplan apply
6、成功应用所有配置后,通过运行以下命令重新启动 Network-Manager 服务:
6.Aftersuccessfullyapplyingalltheconfigurations,restarttheNetwork-Managerservicebyrunningthefollowingcommand:
如果是桌面版:
Ifitisthedesktopversion:
sudo systemctl restart system-networkd
如果您使用的是 Ubuntu 服务器,请改用以下命令:
IfyouareusinganUbuntuserver,usethefollowingcommandinstead:
sudo systemctl restart network-manager
7、验证IP地址
7.VerifytheIPaddress
ip a
二、Netplan配置文件详解
DetailedexplanationofNetplanconfigurationfile
1、使用 DHCP:
1.UseDHCP:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: true
2、使用静态 IP:
2.UsestaticIP:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
– 10.0.0.10/8
gateway4: 10.0.0.1
nameservers:
search: [mydomain, otherdomain]
addresses: [10.0.0.5, 1.1.1.1]
3、多个网口 DHCP:
3.MultiplenetworkportsDHCP:
network:
version: 2
ethernets:
enred:
dhcp4: yes
dhcp4-overrides:
route-metric: 100
engreen:
dhcp4: yes
dhcp4-overrides:
route-metric: 200
4、连接开放的 WiFi(无密码):
4.ConnecttoopenWiFi(withoutpassword):
network:
version: 2
wifis:
wl0:
access-points:
opennetwork: {}
dhcp4: yes
5、连接 WPA 加密的 WiFi:
5.ConnecttoWPAencryptedWiFi:
network:
version: 2
renderer: networkd
wifis:
wlp2s0b1:
dhcp4: no
dhcp6: no
addresses: [10.0.0.10/8]
gateway4: 10.0.0.1
nameservers:
addresses: [10.0.0.5, 8.8.8.8]
access-points:
\”network_ssid_name\”:
password: \”**********\”
6、在单网卡上使用多个 IP 地址(同一网段):
6.UsemultipleIPaddressesonasinglenetworkcard(samenetworksegment):
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
– 10.0.0.10/8
– 10.0.0.10/8
gateway4: 10.0.0.1
7、在单网卡使用多个不同网段的 IP 地址:
7.UsemultipleIPaddressesofdifferentnetworksegmentsonasinglenetworkcard:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
– 9.0.0.9/24
– 10.0.0.10/24
– 11.0.0.11/24
#gateway4: # unset, since we configure routes below
routes:
– to: 0.0.0.0/0
via: 9.0.0.1
metric: 100
– to: 0.0.0.0/0
via: 10.0.0.1
metric: 100
– to: 0.0.0.0/0
via: 11.0.0.1
metric: 100
以上就是Ubuntu通过Netplan配置网络教程的详细内容,更多关于Ubuntu Netplan配置网络的资料请关注悠久资源其它相关文章!