您好,登录后才能下订单哦!
# Linux中如何使用nmcli命令配置网络
## 一、nmcli简介
`nmcli`(NetworkManager Command-Line Interface)是NetworkManager提供的命令行工具,用于管理和配置Linux系统中的网络连接。作为现代Linux发行版的默认网络管理工具,它相比传统ifconfig/route等命令具有以下优势:
1. 统一管理有线、无线、VPN等各种网络接口
2. 支持动态配置和持久化配置
3. 提供更人性化的交互方式
4. 与GUI工具配置实时同步
## 二、基本命令结构
nmcli命令采用"对象-动作"语法结构:
```bash
nmcli [OPTIONS] OBJECT { COMMAND | help }
常用对象类型包括:
- general
:NetworkManager常规状态
- networking
:整体网络功能
- radio
:无线射频开关
- connection
:连接配置
- device
:网络接口设备
nmcli general status
输出示例:
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled enabled enabled
nmcli device status
输出示例:
DEVICE TYPE STATE CONNECTION
eth0 ethernet connected eth0
wlan0 wifi connected MyWiFi
lo loopback unmanaged --
nmcli connection show
nmcli connection add type ethernet con-name eth0-static ifname eth0 \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8 8.8.4.4" \
ipv4.method manual
参数说明:
- con-name
:连接配置名称
- ifname
:物理接口名
- ipv4.method manual
:表示静态配置
nmcli connection add type ethernet con-name eth0-dhcp ifname eth0 \
ipv4.method auto
nmcli connection modify eth0-static \
ipv4.addresses "192.168.1.150/24" \
ipv4.dns "192.168.1.1"
nmcli connection up eth0-static
nmcli connection down eth0-static
nmcli device wifi list
nmcli device wifi connect SSID名称
nmcli device wifi connect SSID名称 password 密码
nmcli connection add type wifi con-name home-wifi ifname wlan0 \
ssid HomeWiFi \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "mypassword"
nmcli networking on
nmcli networking off
nmcli radio wifi on
nmcli radio wifi off
nmcli connection delete eth0-static
nmcli connection reload
nmcli connection modify eth0-static +ipv4.addresses "10.0.0.100/24"
nmcli connection modify eth0-static \
+ipv4.routes "192.168.2.0/24 10.0.0.1"
nmcli connection modify eth0-static \
ipv4.dns "8.8.8.8 8.8.4.4 1.1.1.1"
nmcli connection add type ethernet \
con-name corp-net ifname eth0 \
802-1x.eap peap \
802-1x.identity username \
802-1x.password password \
802-1x.ca-cert /path/to/ca.pem
journalctl -u NetworkManager -f
nmcli device show eth0
nmcli --ask connection edit eth0-static
nmcli的配置最终会保存在以下位置:
- /etc/NetworkManager/system-connections/
:持久化连接配置
- /etc/NetworkManager/NetworkManager.conf
:主配置文件
手动编辑后需要执行:
nmcli connection reload
nmcli connection add type ethernet con-name server-lan ifname eth0 \
ipv4.addresses "10.10.10.2/24" \
ipv4.gateway "10.10.10.1" \
ipv4.dns "10.10.10.1" \
ipv4.method manual \
autoconnect yes
# 有线连接(优先)
nmcli connection add type ethernet con-name office-lan ifname eth0 \
ipv4.method auto \
connection.autoconnect-priority 100
# WiFi连接(备用)
nmcli connection add type wifi con-name home-wifi ifname wlan0 \
ssid HomeNetwork \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "homepassword" \
connection.autoconnect-priority 50
nmcli作为现代Linux网络管理工具,提供了强大而灵活的命令行配置能力。通过本文介绍,您应该已经掌握:
相比传统网络配置方式,nmcli具有配置持久化、操作直观、与GUI工具兼容等优势,是Linux系统管理员和高级用户必备的网络管理工具。
提示:在生产环境中修改网络配置前,建议先通过
--offline
参数测试配置有效性,或保留SSH等远程连接的备用访问方式。 “`
注:本文实际约1800字,内容涵盖了nmcli的主要功能和使用场景。如需扩展,可以增加更多实际案例或特定发行版的注意事项。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。