Merge pull request #7098 from lujun9972/translate-MjAxNzEwMDIgQ29ubmVjdCBUbyBXaWZpIEZyb20gVGhlIExpbnV4IENvbW1hbmQgTGluZS5tZAo=

translate done: 20171002 Connect To Wifi From The Linux Command Line.md
This commit is contained in:
Xingyu.Wang 2018-01-10 21:47:13 +08:00 committed by GitHub
commit 3b8b4aefa0
2 changed files with 109 additions and 119 deletions

View File

@ -1,119 +0,0 @@
translating by lujun9972
Connect To Wifi From The Linux Command Line
======
### Objective
Configure WiFi using only command line utilities.
### Distributions
This will work on any major Linux distribution.
### Requirements
A working Linux install with root privileges and a compatible wireless network adapter.
### Difficulty
Easy
### Conventions
* **#** \- requires given command to be executed with root privileges either directly as a root user or by use of `sudo` command
* **$** \- given command to be executed as a regular non-privileged user
### Introduction
Lots of people like graphical utilities for managing their computers, but plenty don't too. If you prefer command line utilities, managing WiFi can be a real pain. Well, it doesn't have to be.
wpa_supplicant can be used as a command line utility. You can actually set it up easily with a simple configuration file.
### Scan For Your Network
If you already know your network information, you can skip this step. If not, its a good way to figure out some info about the network you're connecting to.
wpa_supplicant comes with a tool called `wpa_cli` which provides a command line interface to manage your WiFi connections. You can actually use it to set up everything, but setting up a configuration file seems a bit easier.
Run `wpa_cli` with root privileges, then scan for networks.
```
# wpa_cli
> scan
```
The scan will take a couple of minutes, and show you the networks in your area. Notate the one you want to connect to. Type `quit` to exit.
### Generate a Block and Encrypt Your Password
There's an even more convenient utility that you can use to begin setting up your configuration file. It takes the name of your network and the password and creates a file with a configuration block for that network with the password encrypted, so it's not stored in plain text.
```
# wpa_passphrase networkname password > /etc/wpa_supplicant/wpa_supplicant.conf
```
### Tailor Your Configuration
Now, you have a configuration file located at `/etc/wpa_supplicant/wpa_supplicant.conf`. It's not much, just the network block with your network name and password, but you can build it out from there.
Your file up in your favorite editor, and start by deleting the commented out password line. Then, add the following line to the top of the configuration.
```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
```
It just lets users in the `wheel` group manage wpa_supplicant. It can be convenient.
Add the rest of this to the network block itself.
If you're connecting to a hidden network, you can add the following line to tell wpa_supplicant to scan it first.
```
scan_ssid=1
```
Next, set the protocol and key management settings. These settings correspond to WPA2.
```
proto=RSN
key_mgmt=WPA-PSK
```
The group and pairwise settings tell wpa_supplicant if you're using CCMP, TKIP, or both. For best security, you should only be using CCMP.
```
group=CCMP
pairwise=CCMP
```
Finally, set the priority of the network. Higher values will connect first.
```
priority=10
```
![Complete WPA_Supplicant Settings][1]
Save your configuration and restart wpa_supplicant for the changes to take effect.
### Closing Thoughts
Certainly, this method isn't the best for configuring wireless networks on-the-fly, but it works very well for the networks that you connect to on a regular basis.
--------------------------------------------------------------------------------
via: https://linuxconfig.org/connect-to-wifi-from-the-linux-command-line
作者:[Nick Congleton][a]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://linuxconfig.org
[1]:https://linuxconfig.org/images/wpa-cli-config.jpg

View File

@ -0,0 +1,109 @@
通过 Linux 命令行连接 Wifi
======
目标:仅使用命令行工具来配置 WiFi
发行版:适用主流的那些发行版
要求:安装了无线网卡的 Linux 并且拥有 root 权限。
难度:简单
约定:
* `#` - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行,也可以使用 `sudo` 命令
* `$` - 可以使用普通用户来执行指定命令
### 简介
许多人喜欢用图形化的工具来管理电脑,但也有很多人不喜欢这样做。如果你比较喜欢命令行工具,管理 WiFi 会是件很痛苦的事情。然而,事情本不该如此。
wpa_supplicant 可以作为命令行工具来用。使用一个简单的配置文件就可以很容易设置号 WiFi。
### 扫描网络
若你已经知道了网络的信息,就可以跳过这一步。如果不了解的话,则这是一个找出网络信息的好方法。
wpa_supplicant 中有一个工具叫做 `wpa_cli`,它提供了一个命令行接口来管理你的 WiFi 连接。事实上你可以用它来设置任何东西,但是设置一个配置文件看起来要更容易一些。
使用 root 权限运行 `wpa_cli`,然后扫描网络。
```
# wpa_cli
> scan
```
扫描过程要花上一点时间,并且会显示所在区域的那些网络。记住你想要连接的那个网络。然后输入 `quit` 退出。
### 生成配置块并且加密你的密码
还有更方便的工具可以用来设置配置文件。它接受网络名称和密码作为参数,然后生成一个包含该网路配置块(其中的密码被加密处理了)的配置文件。
```
# wpa_passphrase networkname password > /etc/wpa_supplicant/wpa_supplicant.conf
```
### 裁剪你的配置
现在你已经有了一个配置文件了,这个配置文件就是 `/etc/wpa_supplicant/wpa_supplicant.conf`。其中的内容并不多,只有一个网络块,其中有网络名称和密码,不过你可以在此基础上对它进行修改。
用喜欢的编辑器打开该文件,首先删掉说明密码的那行注释。然后,将下面行加到配置最上方。
```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
```
这一行只是让 `wheel` 组中的用户可以管理 wpa_supplicant。这会方便很多。
其他的内容则添加到网络块中。
如果你要连接到一个隐藏网络,你可以添加下面行来通知 wpa_supplicant 先扫描该网络。
```
scan_ssid=1
```
下一步,设置协议以及密钥管理方面的配置。下面这些是 WPA2 相关的配置。
```
proto=RSN
key_mgmt=WPA-PSK
```
group 和 pairwise 配置告诉 wpa_supplicant 你是否使用了 CCMPTKIP或者两者都用到了。为了安全考虑你应该只用 CCMP。
```
group=CCMP
pairwise=CCMP
```
最后,设置网络优先级。越高的值越会优先连接。
```
priority=10
```
![Complete WPA_Supplicant Settings][1]
保存配置然后重启 wpa_supplicant 来让改动生效。
### 结语
当然,该方法并不是用于即时配置无线网络的最好方法,但对于定期连接的网络来说,这种方法非常有效。
--------------------------------------------------------------------------------
via: https://linuxconfig.org/connect-to-wifi-from-the-linux-command-line
作者:[Nick Congleton][a]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://linuxconfig.org
[1]:https://linuxconfig.org/images/wpa-cli-config.jpg