欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Fedora30 nmcli连接wifi命令,不再使用wpa_supplicant/wpa_cli

程序员文章站 2022-07-01 11:26:34
...

ENV:

[[email protected] ~]# uname -r
5.1.19-300.fc30.x86_64
[[email protected] ~]# cat /etc/redhat-release 
Fedora release 30 (Thirty)

这两天月底,手机流量还有很多,正好可以把手机当热点,让笔记本可以上网。

在连手机热点的时候突然想到,怎么才能用命令行来连接呢,又是网上找资料又是测试系统里的命令,汗!!!

想到以前也有很多玩wpa_supplicant命令的,开始我的表演(哎呀~不要嫌啰嗦)

为了演示,把网断掉。

一、wpa_supplicant/wpa_cli(一定要root账号,其他账号操作权限不够,sudo命令都不行,不知怎么回事)

查看网络

[[email protected] ~]# ifconfig wlp9s0  //减少信息,只找常用的这个网卡查看
wlp9s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether f2:95:f5:1e:80:d4  txqueuelen 1000  (Ethernet)
        RX packets 16832  bytes 13319654 (12.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16765  bytes 2713438 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

另外的命令:

[[email protected] ~]# iw dev
phy#0
	Interface wlp9s0
		ifindex 3
		wdev 0x1
		addr f2:95:f5:1e:80:d4
		type managed
		txpower 15.00 dBm
		multicast TXQ:
			qsz-byt	qsz-pkt	flows	drops	marks	overlmt	hashcoltx-bytes	tx-packets
			0	0	0	0	0	0	0	00
#如果知道网卡名称,可以直接使用以下命令,和上面命令一样的
[[email protected] ~]# iw dev wlp9s0 info  
Interface wlp9s0
	ifindex 3
	wdev 0x1
	addr 68:94:23:ba:ee:d5
	ssid Honor 8
	type managed
	wiphy 0
	channel 1 (2412 MHz), width: 20 MHz, center1: 2412 MHz
	txpower 15.00 dBm
	multicast TXQ:
		qsz-byt	qsz-pkt	flows	drops	marks	overlmt	hashcol	tx-bytestx-packets
		0	0	0	0	0	0	0	0	0

这里可以看到物理网卡和wlp9s0名称,之后我们要用的

查找网络

[[email protected] ~]# iw wlp9s0 scan |grep SSID
	SSID: Honor 8

要想查看具体信息,直接运行命令 # iw wlp9s0 scan
这样可以看到wifi热点的加密方式(想了解的自行百度、google)

wpa_passphrase命令,这个命令就是生成网络信息的:

                 wpa_passphrase "wifi名称" "密码"
[[email protected] ~]# wpa_passphrase "Honor 8" "12345678"
network={
	ssid="Honor 8"
	#psk="12345678"
	psk=6cda728e4eaa531716ee552ad9f14262ce98f00189fadb4e003f796902ff69f9
}

使用wpa_supplicant命令:

这里就是没有使用root用户,运行时一直出错,还以为命令错了呢,找了很久也没找出问题,后来直接root用户就成功了
[[email protected] ~]$ sudo wpa_supplicant -i wlp9s0 -c<(wpa_passphrase "Honor 8" "12345678")
Successfully initialized wpa_supplicant
Failed to open config file '/dev/fd/63', error: No such file or directory
Failed to read or parse configuration '/dev/fd/63'.
[[email protected] ~]# wpa_supplicant -i wlp9s0 -c<(wpa_passphrase "Honor 8" "12345678")
Successfully initialized wpa_supplicant
wlp9s0: SME: Trying to authenticate with a4:71:74:00:11:43 (SSID='Honor 8' freq=2437 MHz)
wlp9s0: SME: Authentication request to the driver failed
wlp9s0: SME: Trying to authenticate with a4:71:74:00:11:43 (SSID='Honor 8' freq=2437 MHz)
wlp9s0: SME: Authentication request to the driver failed
wlp9s0: SME: Trying to authenticate with a4:71:74:00:11:43 (SSID='Honor 8' freq=2437 MHz)
wlp9s0: SME: Authentication request to the driver failed
wlp9s0: SME: Trying to authenticate with a4:71:74:00:11:43 (SSID='Honor 8' freq=2437 MHz)
wlp9s0: SME: Authentication request to the driver failed
wlp9s0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="Honor 8" auth_failures=1 duration=10 reason=CONN_FAILED
wlp9s0: CTRL-EVENT-SSID-REENABLED id=0 ssid="Honor 8"
wlp9s0: SME: Trying to authenticate with a4:71:74:00:11:43 (SSID='Honor 8' freq=2437 MHz)
wlp9s0: SME: Authentication request to the driver failed
wlp9s0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="Honor 8" auth_failures=2 duration=23 reason=CONN_FAILED
^Cnl80211: deinit ifname=wlp9s0 disabled_11b_rates=0
wlp9s0: CTRL-EVENT-TERMINATING 

这就是成功了,手机上也 显示了连接一个设备,但是这个一起在运行也没有完成,多试了几次也是一样的,到最后才知道这样是行不通的了,没法正常连接啊,也不能上网。

只能再想其他的方法了。

wpa_supplicant和wpa_cli这两个命令是不能成功了。

二、nmcli网络管理命令

记得在去年学习RHEL7的时候,网络管理的命令 行是nmcli命令了,也包括nmtui的GUI命令,这个是不是可行 的呢?

[[email protected] ~]# nmtui
bash: nmtui: command not found...
Install package 'NetworkManager-tui' to provide command 'nmtui'? [N/y] 

看来GNOME的桌面环境是没安装这个GUI了,我们不用这个命令,不是还有个nmcli命令行么,

[[email protected] ~]# nmcli 
agent       device      help        networking  
connection  general     monitor     radio       
[[email protected] ~]# nmcli device 
connect     disconnect  lldp        monitor     set         status
delete      help        modify      reapply     show        wifi
[[email protected] ~]# nmcli device 

运行nmcli一路tab(玩linux不要说不知道tab)

wait,wait,,看到了什么,,wifi啊,这个可是正需要的啊,进去看看

[[email protected] ~]# nmcli device wifi 
IN-USE  SSID     MODE   CHAN  RATE       SIGNAL  BARS  SECURITY 
        Honor 8  Infra  6     65 Mbit/s  100     ▂▄▆█  WPA2     
[[email protected] ~]# 

这个正是我们需要的啊,也就是手机热点的信息。没错啊

之前的一次操作,不能算成功,应该是系统对网络信息有记录才导致的,之后再次使用就不成功了
[[email protected] ~]# nmcli device connect 
enp4s0f2        lo              virbr0          wlp9s0
help            p2p-dev-wlp9s0  virbr0-nic      
[[email protected] ~]# nmcli device connect wlp9s0 
Passwords or encryption keys are required to access the wireless network 'Honor 8'.
Warning: password for '802-11-wireless-security.psk' not given in 'passwd-file' and nmcli cannot ask without '--ask' option.
Error: Connection activation failed: (7) Secrets were required, but not provided.
根据提示,再次运行命令,输入密码就可以了
[[email protected] ~]# nmcli device connect wlp9s0 --ask
Passwords or encryption keys are required to access the wireless network 'Honor 8'.
Password (802-11-wireless-security.psk): ••••••••
Device 'wlp9s0' successfully activated with 'cfdc4bdc-3189-4129-a371-ea2b39825373'.
这就是再次操作不成功了,继续努力吧
[[email protected] ~]# nmcli device connect wlp9s0
Error: Failed to add/activate new connection: A 'wireless' setting is required if no AP path was given.

可能是思路错了,要在wifi的命令来操作

[[email protected] ~]# nmcli device wifi  //tab之后有list和connect
connect  help     hotspot  list     rescan   
[[email protected] ~]# nmcli device wifi list 再tab看到的不再重要了,回去
bssid     hwaddr    ifname    --rescan  
[[email protected] ~]# nmcli device wifi list 
IN-USE  SSID     MODE   CHAN  RATE       SIGNAL  BARS  SECURITY 
        Honor 8  Infra  6     65 Mbit/s  100     ▂▄▆█  WPA2     
[[email protected] ~]# nmcli device wifi connect //tab看到了我们想连接的热点
A4:71:74:00:11:43  Honor\ 8           
[[email protected] ~]# nmcli device wifi connect Honor\ 8 //tab后
bssid         ifname        password      wep-key-type  
hidden        name          private       
#连接网络要用到网卡的,ifname这个都认识吧
#在输入ifname后再tab直接出wlp9s0,这是因为我的电脑就一个网卡      
[[email protected] ~]# nmcli device wifi connect Honor\ 8 ifname wlp9s0 //还差个密码呢
bssid         ifname        password      wep-key-type  
hidden        name          private       
[[email protected] ~]# nmcli device wifi connect Honor\ 8 ifname wlp9s0 password 12345678
Device 'wlp9s0' successfully activated with 'd31829c0-dfed-426f-be98-856892b589b4'.

#这里的12345678是我手机热点的密码,不要搞错哦

提示成功连接了,看看网络信息并测试下吧

[[email protected] ~]# nmcli device show wlp9s0 
GENERAL.DEVICE:                         wlp9s0
GENERAL.TYPE:                           wifi
GENERAL.HWADDR:                         68:94:23:BA:EE:D5
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Honor 8   //这里就是wifi热点名称
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/17
IP4.ADDRESS[1]:                         192.168.43.123/24
IP4.GATEWAY:                            192.168.43.1
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 192.168.43.1, mt = 600
IP4.ROUTE[2]:                           dst = 192.168.43.0/24, nh = 0.0.0.0, mt = 600
IP4.DNS[1]:                             192.168.43.1
IP6.ADDRESS[1]:                         2409:8904:1e70:54a4:37a0:c1cc:d3c:57db/64
IP6.ADDRESS[2]:                         fe80::759b:6abb:6247:e936/64
IP6.GATEWAY:                            fe80::a671:74ff:fe00:1143
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 600
IP6.ROUTE[2]:                           dst = 2409:8904:1e70:54a4::/64, nh = ::, mt = 600
IP6.ROUTE[3]:                           dst = ::/0, nh = fe80::a671:74ff:fe00:1143, mt = 6>
IP6.ROUTE[4]:                           dst = ff00::/8, nh = ::, mt = 256, table=255
IP6.DNS[1]:                             2409:8904:1e70:54a4::a5
lines 1-20/20 (END)

[[email protected] ~]# ifconfig wlp9s0
wlp9s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.123  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::759b:6abb:6247:e936  prefixlen 64  scopeid 0x20<link>
        inet6 2409:8904:1e70:54a4:37a0:c1cc:d3c:57db  prefixlen 64  scopeid 0x0<global>
        ether 68:94:23:ba:ee:d5  txqueuelen 1000  (Ethernet)
        RX packets 16946  bytes 13344794 (12.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16964  bytes 2774702 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# ping www.baidu.com
PING www.baidu.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=1 ttl=50 time=59.6 ms
64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=2 ttl=50 time=144 ms
^C
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 59.625/102.054/144.484/42.430 ms

成功了,高兴下吧,哈哈!!

最后,从这次的经历也是明白了,新的系统更新的新的功能,会相应的整合以前的命令和功能,要想学好linux就要不断的学习和调整知识体系。虽然不知道前一个命令为什么不成功(可能是热点使用的WPA2-PSK加密吧,功能没有跟上)。

网上找了一大堆的wifi连接都是wpa_supplicant,看来还是没有新系统的nmcli命令更好用的啊。

有的时候还是要多在系统中找方法,毕竟网上的水军太多了,吃了很多亏,也浪费了很多时间。