如何用命令行实现Ubuntu 16.04连接WiFi

主要有两种方法
iwconfig command
wpasupplicant method
Method 1: Using iwconfig command
Run iwconfig command to find the name of your wireless interface.
If your wireless interface isn’t shown, perhaps you need to bring it up with the following command.
sudo ifconfig wlp4s0 up
sudo iwlist wlp4s0 scan | grep ESSID

查看是否已经正确安装无线网卡
iwconfig
$ iwconfig wlan0 essid key
$ iwconfig wlan0 essid
For the ASCII password, use:
$ iwconfig wlan0 essid key s:
若是找不到iwconfig命令执行sudo apt-get install wireless-tools

$ dhclient wlan0

$ ip addr
$ ping 8.8.8.8

Method 2: Using wpasupplicant
装wpasupplicant以及network-manager,
$ sudo apt-get install wpasupplicant
The file to configure is /etc/wpa_supplicant.conf.

#cat /etc/wpa_supplicant.conf
network={
ssid=“ssid_name”
psk=“password”
}

也可以用下面介绍的cli命令或wpa_passphrase命令产生这个文件, 我个人认为直接编辑wpa_supplicant.conf更好,以下内容至动态分配

wpa_passphrase MYSSID passphrase > /etc/wpa_supplicant.conf
wpa_passphrase TPLINK 12345678
network={
ssid=“TPLINK”
#psk=“12345678”
psk=992194d7a6158009bfa25773108291642f28a0c32a31ab2556a15dee97ef0dbb
}
或者使用下面命令加入文件:
wpa_passphrase TPLINK 12345678 |sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf
wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee /etc/wpa_supplicant.conf
Now rescan for available devices:

iwlist scan
iwlist wlp4s0 scan
wpa_cli -i wlan0 scan

Now use the following command to connect your wireless card to wireless access point.
wpa_supplicant -B -i interface -c /etc/wpa_supplicant.conf
wpa_supplicant -i ath0 -c /etc/wpa_supplicant.conf -ddd
Successfully initialized wpa_supplicant

Using wpa_cli
At this point run:
wpa_cli
This will present an interactive prompt (>), which has tab completion and descriptions of completed commands.
Scan for available access points
如果要连接加密方式是[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] (wpa加密),wifi名称是name,wifi密码是:psk。
$ wpa_cli -i wlan0 set_network 0 ssid ‘“name”’
$ wpa_cli -i wlan0 set_network 0 psk ‘“psk”’
$ wpa_cli -i wlan0 enable_network 0

To get a DHCP lease, first release whatever leases you’re still holding onto (as root):

Although we’re authenticated and connected to wireless network, but we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command:
dhclient -r
并没有真正释放IP,下一次执行没有discover这个步骤,而是直接REQUEST上次IP。在网络拓扑或者配置变化的情况下,获取不到IP,需要删除/var/lib/dhclient/dhclient.leases 文件
sudo dhclient wlp4s0 -v
dhclient eth1 -v
-v 启用详细日志消息。
Now your wireless interface has a private IP address, which can be shown with:
ip addr show wlp4s0

要是需要设置开机自动连接的话

修改/etc/network/interface文件中的无线设置
vi /etc/network/interfaces

修改后的文件如下:
auto wlp3s0
iface wlp3s0 inet dhcp
wpa-conf /home/sdmt/wlp3s0.conf

Note that if you are using Ubuntu desktop edition, then you need to stop Network Manager with the following command, otherwise it will cause connection problem when using wpa_supplicant.
sudo systemctl stop NetworkManager
sudo systemctl disable NetworkManager

致谢!
https://linoxide.com/linux-how-to/connect-wifi-terminal-ubuntu-16-04/
https://blog.csdn.net/xianlvfan2224/article/details/93159244
Linux添加WIFI驱动_天录的专栏-CSDN博客 https://blog.csdn.net/sbddbfm/article/details/101222266

发布了34 篇原创文章 · 获赞 1 · 访问量 4254

猜你喜欢

转载自blog.csdn.net/u010879745/article/details/104108647