NetworkManager——nmcli命令连接WIFI和创建热点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014695839/article/details/84554606

像ubuntu server这样的服务器本版的linux系统是没有提供桌面的,只有终端。因此,我们不能用Setting的选项来配置网络。而在终端下,我们可以使用NetworkManager的nmcli命令对网络进行配置和操作。

nmcli命令的操作又很多,本文只取了建立和删除wifi连接、启用和关闭连接、建立热点以及设置自动连接的操作为参考,更多更详细的描述请参考文档:https://developer.gnome.org/NetworkManager/stable/nmcli.html,文档里面写得非常的详细了。

注:nmcli命令支持使用缩写(别名),譬如说 device——dev、connection——con、……

1、建立和删除一个wifi连接

创建wifi连接(SSID:Samsung-printer,密码:12345678,默认使用设备wlan0)的示例:

$ nmcli device wifi connect Samsung-printer password 12345678

每次命令执行后,会在/etc/NetworkManager/system-connections/目录下创建一个新文件来保存配置,重复执行则创建多个这样的文件。

删除wifi连接的示例:

$ nmcli con del Samsung-printer

在/etc/NetworkManager/system-connections/目录下的Samsung-printer文件将被删除。

2、启动连接和关闭连接

启用wifi连接的示例:

$ nmcli connection up Samsung-printer
或者
$ nmcli device con wlan0

关闭wifi连接的示例:

$ nmcli connection down Samsung-printer
或者
$ nmcli device dis wlan0

3、创建热点

命令格式及参数解释如下:

wifi hotspot [ifnameifname] [con-name name] [ssid SSID] [ band { a | bg } ] [channel channel] [password password]

Create a Wi-Fi hotspot. The command creates a hotspot connection profile according to Wi-Fi device capabilities and activates it on the device. The hotspot is secured with WPA if device/driver supports that, otherwise WEP is used. Use connection down or device disconnect to stop the hotspot.

Parameters of the hotspot can be influenced by the optional parameters:

ifname

使用的网络设备名称

con-name

热点的名称

ssid

热点的ssid

band

wifi的协议标准

channel

信道

password

热点的密码。如果不设置,nmcli会生成一个WPA或者WEP的密码。加入全局选项--show-secrets将会输出热点的密码,这在nmcli生成密码时十分有用。

示例:

$ nmcli device wifi hotspot ifname wlan0 con-name MyHostspot ssid MyHostspotSSID password 12345678

命令执行后,将会创建一个名为MyHotspot的连接。使用上述的命令启动连接,将会开启热点。

4、设置自动连接

示例:

$ nmcli connnection modify MyHotspot autoconnect yes

猜你喜欢

转载自blog.csdn.net/u014695839/article/details/84554606