树莓派3b+ 使用 Ubuntu 20.04 连接WPA2 Enterprise WiFi (命令行)
之前折腾树莓派的时候发现想要连到学校的校园网并不是一件简单的事情,找了很多网页发现之后发现还是我学校之前发过一些文章,但是那些文章也有些问题,所以有些地方比较奇怪。折腾了两天,写了一点小教程。因为一开始就是用英语写的, 现在也有点懒得翻译。如果有写错的或者想要知道怎么整的话,试试戳戳我,有可能出现就是了。希望能帮到人咯。
以下是我写的原文:
This time, I am using Raspberry Pi 3B+ to connect to the IllinoisNet (WiFi name in my school), which is a WPA2 Enterprise public network that requires your netid and your student account password (identity and password) to login and connect. On PC or mac, when users try to connect to this wifi, a window will pop up and ask for netid and password. But since there is no GUI in command-line version of Ubuntu, we need to try to add this information to “some file” in the system.
I managed to finish this based on this post. But since this tutorial is outdated, and there are some new commands that are different from the old ones, I hope this post can help you set up your machine. For the convenience of reading, I will copy original posts at some points.
Configuration:
Step 0: Check if your machine can connect to WiFi. I used the command ip a
to check if I have the “network interface” connected (network interface is wlan0
on my machine). You can ignore this step if you know that your machine can connect to WiFi.
Step 1: Make sure wpa_supplicant is installed.
wpa_supplicant -v
Result:
wpa_supplicant v2.9
Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi> and contributors
Most distros probably come with it. Here’s the command to install it in Ubuntu, what you need to do may be different depending on your package manager. But since you are trying to connect to WiFi at this point, if you don’t have wpa_supplicant installed in your machine at this point, my suggestion is to connect to another WiFi first. It can be your personal hotspot or normal WiFi (WPA2/WPA3 Personal) that requires only WiFi name and password to connect.
sudo apt-get install wpa_supplicant
Step 2: Create a config file for wpa_supplicant named “wpa_supplicant.conf”, you can make it anywhere but it’s usually placed in /etc/
or /etc/wpa_supplicant/
. Here I’m creating it inside /etc/wpa_supplicant/
sudo touch /etc/wpa_supplicant/wpa_supplicant.conf
Step 3: Open up the file in a text editor.
Original post uses:
gksudo gedit /etc/wpa_supplicant/wpa_supplicant.conf
But since my machine couldn’t use gksudo and gedit, I was using nano:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
You can also try vim to add this file if you are familiar with vim.
Tutorial for using Nano.
Step 4: Put the following into the file, replacing with your NetID WITHOUT the “@illinois.edu” and with your password:
network={
ssid="IllinoisNet"
scan_ssid=1
key_mgmt=WPA-EAP
pairwise=CCMP TKIP
group=CCMP TKIP
eap=TTLS
identity= "<your netid>"
password= "<your password>"
# ca_cert="/etc/ssl/certs/USERTrust_RSA_Certification_Authority.pem"
phase2="auth=MSCHAPV2"
}
According to this page, I found that the “CA Certificate” is not mandatory for setting up this WiFi. So I comment down the “ca_cert” part above. If you failed because of this, you can try uncomment the line or try to add the certification manually. You can download the ca_cert here: http://go.illinois.edu/wificert. This is also given by the webpage above.
Note: The original post uses a different pem file. But according to another post, the older pem file has expired. So I put the recommended ca_cert aboved.
Refreshing and starting wpa_supplicant:
Step 5: Stop NetworkManager, existing instances of wpa_supplicant, and anything else that may be using the wireless card.
sudo service network-manager stop
sudo killall wpa_supplicant
Step 6: Bring down the interface and start fresh. Replace with the name of your wireless interface (it’s wlan0 on my machine, brackets are not required).
# sudo ifconfig <interface> down
sudo ip link set <interface> down
sudo dhclient -r <interface>
Since I didn’t have ifconfig on my machine, I ignored the command sudo ifconfig wlan0 down
and it still works fine. But the second line works fine for most Linux systems. Here is a tutorial I found.
Step 7: Connect to IllinoisNet with wpa_supplicant. Once again, replace with the name of your interface. Also, change the file path to the .conf file if yours is in a different location
sudo wpa_supplicant -Dnl80211 -i<interface> -c/etc/wpa_supplicant/wpa_supplicant.conf -B
Step 8: Ask for an IP address. Again, is the name of your interface. Note, your GUI will give no visible indication whether you are connected or not. If this command terminates in a timely manner you probably succeeded.
sudo dhclient <interface>
When you’re done and wish to restore to your original state, kill wpa_supplicant and restart NetworkManager.
sudo killall wpa_supplicant
sudo service network-manager start
Shell Script:
The .conf file will stay on your system after the first time, but you will have to go through steps 5-8 every time you connect to IllinoisNet. Going through this process each and every time you want to connect can get annoying. So instead of doing all this manually, after you create the .conf file, you can copy and paste the following into a shell script and just run the script each time you want to connect.
#!/bin/sh
# Script to set up connection to IllinoisNet
# Run as root
service network-manager stop
killall wpa_supplicant
# ifconfig wlan0 down
ip link set wlan0 down
dhclient -r wlan0
wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -B
dhclient wlan0
The script should be run as root. Make sure execution is allowed:
sudo ./scriptname.sh
If the script fails to run, you can reboot and try again (that’s how I solved it).
If you wish to run this script on startup, you can follow tutorials online. Here is one I found for Ubuntu 20.04: https://linuxconfig.org/how-to-run-script-on-startup-on-ubuntu-20-04-focal-fossa-server-desktop.
References:
-
IllinoisNet Wireless - Command Line (Tested on Ubuntu 12.04 and 13.04): https://wiki.illinois.edu/wiki/pages/viewpage.action?pageId=140609619
-
IllinoisNet Wireless - Ubuntu 10.04, 10.10, 11.04, 11.10, 12.04, 12.10, 15.04: https://wiki.illinois.edu/wiki/x/6RaTAw
-
Technology Services Wi-Fi Help Portal-Illinois Faculty, Staff and Students-Linux Devices: https://answers.uillinois.edu/illinois/page.php?id=90285
-
10 Useful “IP” Commands to Configure Network Interfaces: https://www.tecmint.com/ip-command-examples/
-
How to Use Nano, the Linux Command Line Text Editor: https://linuxize.com/post/how-to-use-nano-text-editor/
-
How to run script on startup on Ubuntu 20.04 Focal Fossa Server/Desktop: https://linuxconfig.org/how-to-run-script-on-startup-on-ubuntu-20-04-focal-fossa-server-desktop