ubuntu使用nmcli
Ubuntu Server 20.04使用nmcli配置网络连接
预备需求
- Ubuntu Server 20.04
- root权限
- 可以访问互联网
步骤
-
安装
network-manager
sudo apt install network-manager
-
使用
ip addr
查看要配置的网卡cc@ubuntu:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:59:f9:bf brd ff:ff:ff:ff:ff:ff inet 192.168.93.132/24 brd 192.168.93.255 scope global dynamic ens33 valid_lft 1688sec preferred_lft 1688sec inet6 fe80::20c:29ff:fe59:f9bf/64 scope link valid_lft forever preferred_lft forever cc@ubuntu:~$
-
我的网卡为
ens33
-
启用
network-manager
,编辑NetworkManager.conf
文件sudo nano /etc/NetworkManager/NetworkManager.conf
-
将
managed=false
修改为managed=true
然后重启
NetworkManager
服务sudo systemctl restart network-manager.service
-
编辑
/etc/netplan/*.yaml
,在我的机器上为:cc@ubuntu:~$ sudo nano /etc/netplan/00-installer-config.yaml
-
修改内容如下
network: version: 2 renderer: NetworkManager
-
应用配置
cc@ubuntu:~$ sudo netplan apply
此时已经可以通过nmcli
管理网络了
使用nmcli -p device
查看设备状态
cc@ubuntu:~$ nmcli -p device
=====================
Status of devices
=====================
DEVICE TYPE STATE CONNECTION
----------------------------------------------------------------------
ens33 ethernet connected Wired connection 1
lo loopback unmanaged --
cc@ubuntu:~$
例子:使用nmcli配置静态ip地址
-
创建一个连接,语法如下
sudo nmcli connection add con-name <连接名> ifname <网卡名> type <连接类型> ipv4.method manual ipv4.addresses <ipv4地址> ipv4.gateway <ipv4网关地址> ipv4.dns <ipv4 dns服务器>
-
例如:
sudo nmcli connection add con-name test ifname ens33 type ethernet ipv4.method manual ipv4.addresses 192.168.93.4/24 ipv4.gateway 192.168.93.2 ipv4.dns 192.168.93.2
-
激活连接,语法如下
sudo nmcli connection up <连接名> ifname <网卡名>
-
例如:
sudo nmcli connection up test ifname ens33
后续步骤:
查看连接状态
cc@ubuntu:~$ nmcli connection
NAME UUID TYPE DEVICE
test 4bd559fe-191d-460c-a4cd-56ae44c27859 ethernet ens33
Wired connection 1 e3f905c8-ba3f-3a5f-ab41-8397381e6fe6 ethernet --
删除多余连接
cc@ubuntu:~$ sudo nmcli connection delete Wired\ connection\ 1
查看ip地址
cc@ubuntu:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:59:f9:bf brd ff:ff:ff:ff:ff:ff
inet 192.168.93.4/24 brd 192.168.93.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::cfb6:10b:de98:a885/64 scope link noprefixroute
valid_lft forever preferred_lft forever
cc@ubuntu:~$
相关资料
netplan: https://netplan.io/
NetworkManager: https://help.ubuntu.com/community/NetworkManager
Systemd-networkd: http://manpages.ubuntu.com/manpages/bionic/man5/systemd.network.5.html