“模板:使用nmcli命令配置网络”的版本间的差异

来自ARM Wiki
跳转至: 导航搜索
(创建页面,内容为“===使用nmcli命令配置网络=== 查看已创建的连接: <syntaxhighlight lang="bash"> nmcli connection show </syntaxhighlight> 查看网络接口状态:...”)
 
使用nmcli命令配置网络
第1行: 第1行:
 
===使用nmcli命令配置网络===
 
===使用nmcli命令配置网络===
 +
 +
====设置静态IP示例====
  
 
查看已创建的连接:
 
查看已创建的连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
nmcli connection show
 
nmcli connection show
 +
...
 +
有线连接 1  36ff5fe1-088a-3562-a9f1-70e9552ce8b6  ethernet  eth0
 +
...
 
</syntaxhighlight>
 
</syntaxhighlight>
  
查看网络接口状态:
+
若已存在关于网络接口eth0的连接,需先删除:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli device status
+
nmcli connection delete '有线连接 1'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
查看网络接口网络信息:
+
添加名为''my_static_eth0''的静态IP连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli device show
+
nmcli connection add con-name my_static_eth0 ifname eth0 type ethernet ip4 10.168.1.157/24 gw4 10.168.1.1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
查看总体状态:
+
设置DNS:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli general status
+
nmcli connection modify my_static_eth0 ipv4.dns "10.168.1.1 119.29.29.29"
 
</syntaxhighlight>
 
</syntaxhighlight>
  
添加连接:
+
====设置动态IP示例====
<syntaxhighlight lang="bash">
 
nmcli connection add con-name "my_connection" type ethernet ifname eth0
 
</syntaxhighlight>
 
  
使用连接:
+
查看已创建的连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection up my_connection
+
nmcli connection show
 +
...
 +
有线连接 1  36ff5fe1-088a-3562-a9f1-70e9552ce8b6  ethernet  eth0
 +
...
 
</syntaxhighlight>
 
</syntaxhighlight>
  
不使用连接:
+
若已存在关于网络接口eth0的连接,需先删除:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection down my_connection
+
nmcli connection delete '有线连接 1'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
删除连接:
+
添加名为''my_dynamic_eth0''的动态IP连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection delete my_connection
+
nmcli connection add con-name my_dynamic_eth0 ifname eth0 type ethernet
 
</syntaxhighlight>
 
</syntaxhighlight>
  
查看连接详细信息:
+
====连接WIFI示例====
<syntaxhighlight lang="bash">
 
nmcli connection show id my_connection
 
</syntaxhighlight>
 
  
设置静态IP命令参考如下,添加名为"my_static_ip"的静态IP连接:
+
查看WIFI网络列表:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection add con-name my_static_ip ifname eth0 type ethernet ip4 192.168.0.10/24 gw4 192.168.0.1
+
nmcli device wifi list
 +
...
 +
IN-USE  SSID            MODE  CHAN  RATE        SIGNAL  BARS  SECURITY 
 +
        OrayBox-3D28    红外  9    130 Mbit/s  65      ▂▄▆_  WPA2     
 +
        NORCO-5G        红外  1    130 Mbit/s  59      ▂▄▆_  WPA2     
 +
...
 
</syntaxhighlight>
 
</syntaxhighlight>
  
设置DNS:
+
连接名为''OrayBox-3D28''密码为''norco85258506''的WIFI:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection modify my_static_ip ipv4.dns "1.2.4.8 8.8.8.8"
+
nmcli device wifi connect OrayBox-3D28 password norco85258506
 
</syntaxhighlight>
 
</syntaxhighlight>
  
使用连接:
+
====其他命令====
<syntaxhighlight lang="bash">
 
nmcli connection up my_static_ip
 
</syntaxhighlight>
 
  
 +
查看网络接口状态:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ nmcli connection down my_static_ip
+
nmcli device status
  # 不使用连接
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
查看网络接口网络信息:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ nmcli connection delete my_static_ip
+
nmcli device show eth0
  # 删除连接
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
设置动态IP命令参考如下,添加名为"my_dynamic_ip"的动态IP连接:
+
使用连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli connection add con-name my_dynamic_ip ifname eth0 type ethernet
+
nmcli connection up my_static_eth0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
不使用连接:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ nmcli connection up my_dynamic_ip
+
nmcli connection down my_static_eth0
  # 使用连接
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
连接wifi命令参考如下:
+
查看连接详细信息:
查看wifi列表:
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli device wifi list
+
nmcli connection show my_static_eth0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
连接wifi:
+
关闭WIFI:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli device wifi connect norco_2.4G password norco85258506
+
nmcli radio wifi off
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
打开WIFI:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ nmcli radio wifi off
+
nmcli radio wifi on
  # 关闭wifi
 
$ nmcli radio wifi on
 
  # 打开wifi
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
连接wifi并设置静态IP:
+
连接WIFI并设置静态IP:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli con add con-name my_wifi type wifi ifname wlan0 ssid "norco_2.4G" -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk "norco85258506" ipv4.method manual ipv4.address 192.168.0.10/24 ipv4.dns 1.2.4.8 ipv4.gateway 192.168.0.1
+
nmcli con add con-name "my_wifi" type wifi ifname wlan0 ssid "OrayBox-3D28" -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk "norco85258506" ipv4.method manual ipv4.address 10.168.1.158/24 ipv4.dns 119.29.29.29 ipv4.gateway 10.168.1.1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
创建Wifi热点:创建名为"test"的wifi热点:
+
创建名为''my_hotspot''的WIFI热点:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nmcli dev wifi hotspot ifname wlan0 ssid test password "12345678"
+
nmcli dev wifi hotspot ifname wlan0 ssid "my_hotspot" password "88888888"
 
</syntaxhighlight>
 
</syntaxhighlight>

2023年2月13日 (一) 11:57的版本

使用nmcli命令配置网络

设置静态IP示例

查看已创建的连接:

nmcli connection show
...
有线连接 1  36ff5fe1-088a-3562-a9f1-70e9552ce8b6  ethernet  eth0
...

若已存在关于网络接口eth0的连接,需先删除:

nmcli connection delete '有线连接 1'

添加名为my_static_eth0的静态IP连接:

nmcli connection add con-name my_static_eth0 ifname eth0 type ethernet ip4 10.168.1.157/24 gw4 10.168.1.1

设置DNS:

nmcli connection modify my_static_eth0 ipv4.dns "10.168.1.1 119.29.29.29"

设置动态IP示例

查看已创建的连接:

nmcli connection show
...
有线连接 1  36ff5fe1-088a-3562-a9f1-70e9552ce8b6  ethernet  eth0
...

若已存在关于网络接口eth0的连接,需先删除:

nmcli connection delete '有线连接 1'

添加名为my_dynamic_eth0的动态IP连接:

nmcli connection add con-name my_dynamic_eth0 ifname eth0 type ethernet

连接WIFI示例

查看WIFI网络列表:

nmcli device wifi list
...
IN-USE  SSID             MODE  CHAN  RATE        SIGNAL  BARS  SECURITY  
        OrayBox-3D28     红外  9     130 Mbit/s  65      ▂▄▆_  WPA2      
        NORCO-5G         红外  1     130 Mbit/s  59      ▂▄▆_  WPA2      
...

连接名为OrayBox-3D28密码为norco85258506的WIFI:

nmcli device wifi connect OrayBox-3D28 password norco85258506

其他命令

查看网络接口状态:

nmcli device status

查看网络接口网络信息:

nmcli device show eth0

使用连接:

nmcli connection up my_static_eth0

不使用连接:

nmcli connection down my_static_eth0

查看连接详细信息:

nmcli connection show my_static_eth0

关闭WIFI:

nmcli radio wifi off

打开WIFI:

nmcli radio wifi on

连接WIFI并设置静态IP:

nmcli con add con-name "my_wifi" type wifi ifname wlan0 ssid "OrayBox-3D28" -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk "norco85258506" ipv4.method manual ipv4.address 10.168.1.158/24 ipv4.dns 119.29.29.29 ipv4.gateway 10.168.1.1

创建名为my_hotspot的WIFI热点:

nmcli dev wifi hotspot ifname wlan0 ssid "my_hotspot" password "88888888"