Debian-Netwotk
理论知识
NetworkManager
https://computingforgeeks.com/install-and-use-networkmanager-nmcli-on-ubuntu-debian/
Network interfaces are typically initialized in "networking.service
" for the lo
interface and "NetworkManager.service
" for other interfaces on modern Debian desktop system under systemd
.
Debian can manage the network connection via management daemon software such as NetworkManager (NM) (network-manager and associated packages).
- They come with their own GUI and command-line programs as their user interfaces.
- They come with their own daemon as their backend system.
- They allow easy connection of your system to the Internet.
- They allow easy management of wired and wireless network configuration.
- They allow us to configure network independent of the legacy
ifupdown
package.
These modern network configuration tools need to be configured properly to avoid conflicting with the legacy ifupdown
package and its configuration file "/etc/network/interfaces
".
配置语法示例:
- 配置方式:
- 命令行工具:
nmcli
(推荐)、nmtui
(文本界面)。 - 配置文件:
/etc/NetworkManager/NetworkManager.conf
和/etc/NetworkManager/system-connections/*.nmconnection
(Keyfile 格式)。
- 命令行工具:
- 示例配置(Keyfile):
ini
复制[connection] id=eth0-static type=ethernet interface-name=eth0 [ipv4] method=manual addresses=192.168.1.100/24 gateway=192.168.1.1 dns=8.8.8.8;
NetworkManager
sudo systemctl restart NetworkManager
# 查看状态
nmcli general status
nmcli
https://manpages.debian.org/bookworm/network-manager/nmcli.1.en.html
比较Device & Connection
device指的是物理或虚拟的网络接口,比如以太网卡、Wi-Fi适配器或者虚拟的VPN接口。而connection可能是指配置好的网络连接设置,比如一个Wi-Fi网络的配置,包括SSID、密码、IP设置等。这样的话,一个设备(device)可以有多个connection配置,但同一时间可能只有一个处于激活状态?
比如,我的笔记本电脑有一个无线网卡(device),可能保存了家里、办公室、咖啡店等多个Wi-Fi的connection配置。当我在家时,激活家里的connection,连接到家里的Wi-Fi;到办公室后,切换到办公室的connection。所以,device是硬件或内核识别的接口,而connection是NetworkManager管理的配置集。
每个设备可能有多个connection,但同一时间只能有一个激活的connection。比如,一个以太网设备可能有动态IP和静态IP两种配置,保存为两个不同的connection,用户可以根据需要激活其中一个。
那它们之间的关系是怎样的呢?当激活一个connection时,NetworkManager会将其应用到对应的device上。例如,激活一个Wi-Fi连接时,对应的无线设备会被配置并使用该连接的参数。另外,当设备可用时(比如插入网线),NetworkManager可能会自动激活关联的connection,特别是如果设置了自动连接的话。
总结一下,device是物理或虚拟的网络接口,而connection是针对这些接口的配置集。一个设备可以有多个connection,但同一时间只能有一个活动的connection。这样用户可以根据不同场景切换不同的网络配置,而无需重新设置设备参数。
在NetworkManager的 nmcli
工具中,connection
(连接)和 device
(设备)是两个核心概念,它们的作用和区别如下:
1. Device(设备)
- 定义:
设备是物理或虚拟的网络接口,例如:- 物理网卡(如以太网卡
enp0s3
、无线网卡wlp2s0
)。 - 虚拟接口(如VPN隧道
tun0
、桥接接口virbr0
)。
- 物理网卡(如以太网卡
- 作用:
- 表示系统中实际存在的网络硬件或内核模块提供的接口。
- 设备的状态由内核或驱动控制(如连接/断开、信号强度等)。
- 关键特性:
- 设备是“物理层”的实体,能被
ip link
或ifconfig
等工具识别。 - 同一时间一个设备只能激活一个连接。
- 设备可能处于未托管状态(如果NetworkManager不管理它)。
- 设备是“物理层”的实体,能被
常用命令:
bash
复制
# 查看所有设备及其状态
nmcli device status
# 查看某个设备的详细信息
nmcli device show <设备名>
2. Connection(连接)
- 定义:
连接是一组网络配置的集合(如IP地址、DNS、SSID、密码等),用于控制如何通过设备访问网络。 - 作用:
- 保存网络配置,供设备按需使用。
- 支持多种连接类型:以太网、Wi-Fi、VPN、桥接等。
- 关键特性:
- 连接是“逻辑层”的配置,与设备解耦。例如:
- 一个Wi-Fi设备(如
wlp2s0
)可以保存多个连接配置(如家庭Wi-Fi、公司Wi-Fi)。 - 一个连接可以绑定到特定设备,也可以设置为“适用于任何匹配设备”。
- 一个Wi-Fi设备(如
- 同一设备可保存多个连接,但同一时间只能激活一个。
- 连接支持自动激活(如插入网线时自动启用以太网连接)。
- 连接是“逻辑层”的配置,与设备解耦。例如:
常用命令:
bash
复制
# 查看所有保存的连接
nmcli connection show
# 激活某个连接
nmcli connection up <连接名>
# 创建新的Wi-Fi连接
nmcli connection add type wifi ssid <SSID> wifi-sec.key-mgmt wpa-psk wifi-sec.psk <密码>
3. 区别与联系
特性 | Device(设备) | Connection(连接) |
---|---|---|
本质 | 物理/虚拟接口(硬件或内核模块) | 网络配置的集合(逻辑配置) |
多实例支持 | 一个设备对应一个硬件 | 一个设备可关联多个连接(但同一时间只能激活一个) |
依赖关系 | 设备存在与否由系统硬件决定 | 连接可独立存在,无需设备实时在线 |
自动管理 | NetworkManager可能自动创建设备对象 | 连接需手动创建或通过UI/工具自动生成(如扫描Wi-Fi) |
4. 协作示例
- 插入网线:
- 系统检测到以太网设备(如
enp0s3
)。 - NetworkManager自动激活绑定到该设备的连接(如
有线连接1
)。
- 系统检测到以太网设备(如
- 切换Wi-Fi网络:
- 设备
wlp2s0
已存在。 - 用户激活另一个保存的连接(如从
家庭Wi-Fi
切换到公司Wi-Fi
),设备会断开当前连接并应用新配置。
- 设备
- VPN连接:
- 创建VPN连接时,会生成一个虚拟设备(如
tun0
)。 - VPN连接配置(如服务器地址、证书)保存在连接中,激活后绑定到虚拟设备。
- 创建VPN连接时,会生成一个虚拟设备(如
5. 总结
- Device:是“硬”存在,表示网络接口硬件或虚拟接口。
- Connection:是“软”配置,决定设备如何连接到网络。
- 协作逻辑:通过为设备选择合适的连接,实现灵活的网络管理(如多场景切换、冗余配置)。
Connection常见命令
NMCLI is a network utility tool that will allow us to manage network configurations. There are a couple of commands one can use to manage a network.
Listing network connections
nmcli connection show
Sample Output:
AME UUID TYPE DEVICE
Wired connection 1 ac98eab7-1ad3-4cf0-93a0-210bfe3bb7e2 ethernet enp0s3
The above command shows all the connections, but if you want to see the active or up connections you will add the --active<span> </span>
flag to the above command;
nmcli connection show --active
Sample Output:
NAME UUID TYPE DEVICE
Wired connection 1 ac98eab7-1ad3-4cf0-93a0-210bfe3bb7e2 ethernet enp0s3
With the connection profile name, you can view additional information as below.
nmcli connection show "Wired connection 1"
- Activate a connection:
nmcli connection up uuid uuid
- Deactivate a connection:
nmcli connection down uuid uuid
Add a Network connection
nmcli connection add type <network-type> ifname <connection-name>
For me, it’s the Ethernet connection and I will add a connection profile with the name My-New-Connection
nmcli connection add con-name My-New-Connection ifname enp0s3 type ethernet
Sample output:
Connection 'My-New-Connection' (a6b26540-4c06-4a80-a0fa-a2868e4006e5) successfully added.
With the connection profile added, we will make the below adjustments. Remember to set your own IP address, gateway, DNS server e.t.c using a similar syntax to the one I have given.
To Set automatic start of the network connection use:
nmcli connection modify My-New-Connection connection.autoconnect yes
- Create an auto-configured dual stack connection:
nmcli connection add ifname interface_name type ethernet ipv4.method auto ipv6.method auto
- Create a static IPv6-only connection:
nmcli connection add ifname interface_name type ethernet ip6 2001:db8::2/64 gw6 2001:db8::1 ipv6.dns 2001:db8::1 ipv4.method ignore
- Create a static IPv4-only connection:
nmcli connection add ifname interface_name type ethernet ip4 10.0.0.7/8 gw4 10.0.0.1 ipv4.dns 10.0.0.1 ipv6.method ignore
Modify an existing Network connection using NMCLI
With NMCLI, one can make adjustments, configurations to an existing network connection such as switching between static and DHCP configurations using the syntax below.
nmcli connection modify <connection-id> <parameter> <value>
For example, we will modify the existing connection to a Static IP_Address and also change the subnet /24 without this specification, it will assign an IP with the default mask that can cause problems later.
nmcli connection modify My-New-Connection ipv4.address 192.168.100.157/24
Set a static IPv6 address with a /64 subnet mask
nmcli connection modify My-New-Connection ipv6.addresses 2001:db8:1::1/64
Set an IPv4/IPv6 default gateway
##For IPv4
nmcli connection modify My-New-Connection ipv4.gateway 192.168.100.1
##For IPv6
nmcli connection modify My-New-Connection ipv6.gateway 2001:db8:1::fffe
Set an IPv4/IPv6 DNS server
##For IPv4
nmcli connection modify My-New-Connection ipv4.dns "192.168.100.200"
##For IPv6
nmcli connection modify My-New-Connection ipv6.dns "2001:db8:1::ffbb"
Set the IPv4 and IPv6 connection method to automatic
##For IPv4
nmcli connection modify My-New-Connection ipv4.method auto
##For IPv6
nmcli connection modify My-New-Connection ipv6.method auto
For the changes made to take effect, you need to activate the profile.
$ nmcli connection up My-New-Connection
connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
Now check active connections.
nmcli connection show --active
Device常见命令
Displaying device Status
nmcli -p dev status
get the device information.
nmcli device show
modify a device
$ nmcli device modify <interface-name> <parameter> <value>
###OR
$ nmcli dev mod <interface-name> <parameter> <value>
Remember that the above changes are temporal and can be reset to default settings using the command:
nmcli dev reapply interface-name
In case you are stuck when using NMCLI device manager find help using the command:
nmcli device help
实际案例
配置静态ip
安装NetworkManager
apt install network-manager
启用并开机自启动
systemctl status NetworkManager
systemctl start NetworkManager
systemctl enable NetworkManager
root@debian:~# nmcli dev status
DEVICE TYPE STATE CONNECTION
ens33 ethernet unmanaged --
lo loopback unmanaged --
上面显示出,device没有被NetworkManager托管
vim /etc/NetworkManager/NetworkManager.conf
将文件中的 managed = false 改为 true
root@debian:~# cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifupdown,keyfile
[ifupdown]
managed=true
接下来,重启下NetworkManager
systemctl restart NetworkManager
可以看到,现在device已经被NetworkManager托管了
root@debian:~# nmcli dev status
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected (externally) ens33
lo loopback unmanaged --
指定静态ip
接下来,创建一个connection:
nmcli connection add con-name weipeng-con ifname ens33 type ethernet autoconnect yes ip4 192.168.10.119/24 gw4 192.168.10.1 ipv4.dns 218.2.135.1 ipv6.method ignore
如果创建的有问题,可以使用delete命令,进行删除
nmcli connection delete a3a7478c-d060-4739-9c69-aba0269152f9
上面的connection后,我们激活这个connetion
nmcli connection up weipeng-con
激活后,我们执行命令查看
root@debian:~# nmcli connection show
NAME UUID TYPE DEVICE
weipeng-con 12133c3b-69d9-4dcf-9178-e89e1a5ad5f0 ethernet ens33
Ifupdown (ens33) 108b861f-800d-98ce-9ab4-4ed2c57e11f1 ethernet --
root@debian:~#
# 将原来的connection删除
root@debian:~# nmcli connection delete 108b861f-800d-98ce-9ab4-4ed2c57e11f1
root@debian:~# nmcli connection show
NAME UUID TYPE DEVICE
weipeng-con 12133c3b-69d9-4dcf-9178-e89e1a5ad5f0 ethernet ens33
# Attempt to update device with changes to the currently active connection
root@debian:~# nmcli device reapply ens33
Connection successfully reapplied to device 'ens33'.
也可以,使用 ip addr
查看
root@debian:~# 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 pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:42:dd:50 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 192.168.10.119/24 brd 192.168.10.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
文章作者:Administrator
文章链接:http://localhost:8090//archives/debian-netwotk
版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!
评论