|
|
对于无图形界面的服务器版Server,使用netplan配置网络可以在重启后仍然能够正常启动配置并获取IP地址。
有2个步骤,修改/etc/netplan/目录下的.yaml配置文件,并用netplan apply生效。
配置难点在于配置文件的格式。man netplan有很长的说明,功能强大,也有例子,但不好看懂。
在配置wifi无线网卡之前,首先查看下网络。
xxm@ubuntu20:/etc/netplan# ip ad
用以下命令尝试修改并测试配置文件,只是缩进不同,如出现报错,多半是缩进问题:
xxm@ubuntu20:/etc/netplan# sudo netplan try --debug
xxm@ubuntu20:/etc/netplan# sudo netplan generate
设置固定IP地址并设置自动获取IP地址的无线网络
xxm@ubuntu20:/etc/netplan# sudo nano 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
enp1s0:
addresses: [192.168.1.6/24]
dhcp4: false
optional: true
gateway4: 192.168.1.1
nameservers:
addresses: [114.114.114.114]
version: 2
renderer: NetworkManager
wifis:
wlx1cbfce9fddce:
dhcp4: true
access-points:
"Your wifi name":
password: "Your wifi password"
设置自动获取IP地址的有线和无线网络
xxm@ubuntu20:/etc/netplan# sudo nano 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
enp1s0:
dhcp4: true
optional: true
wifis:
wlx1cbfce9fddce:
dhcp4: true
optional: true
access-points:
"Your wifi name":
password: "Your wifi password"
version: 2
xxm@ubuntu20:/etc/netplan# sudo netplan try --debug
Warning: Stopping systemd-networkd.service, but it can still be activated by:
systemd-networkd.socket
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration //这里按回车接受新的配置
Changes will revert in 117 seconds
Configuration accepted.
配置好之后
xxm@ubuntu20:/etc/netplan# sudo ip ad
可以列出正确配置好的网络
剖析一下netplan:
xxm@ubuntu20:~# sudo which netplan
/usr/sbin/netplan
xxm@ubuntu20:~# ll /usr/sbin/netplan
lrwxrwxrwx 1 root root 31 Jan 8 2021 /usr/sbin/netplan -> ../share/netplan/netplan.script*
xxm@ubuntu20:~# file /usr/share/netplan/netplan.script
/usr/share/netplan/netplan.script: Python script, ASCII text executable
xxm@ubuntu20:~# netplan -h
usage: /usr/sbin/netplan [-h] [--debug] ...
Network configuration in YAML
optional arguments:
-h, --help show this help message and exit
--debug Enable debug messages
Available commands:
help Show this help message
apply Apply current netplan config to running system
generate Generate backend specific configuration files from /etc/netplan/*.yaml
get Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all"
info Show available features
ip Retrieve IP information from the system
set Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true
try Try to apply a new netplan config to running system, with automatic rollback
总结:
ubuntu20.04服务器版(仅命令行shell)的网络服务是systemd-networkd 。
配置文件是/etc/netplan/50-cloud-init.yaml 该文件遵循yaml语法,必须用空格缩进,不能用tab。
netplan命令是一个python脚本。
|
|