Connect to WPA2 through Terminal?

Asked by Allen

Hi, I'm using Ubuntu 9.10. I want to connect to my wireless router through the Terminal or xterm(Not because I need to, but because I want to learn how, just in case my GUI stops from some stupid mistake, and for future reference). The problem is I can't find ANY information on connecting through WPA2! Any help with that guys? Thanks!

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Philip Muškovac
Solved:
Last query:
Last reply:
Revision history for this message
Philip Muškovac (yofel) said :
#1

This is indeed a bit hard to find documentation about, I can't find it right now either, here's what I know about it:
First you should get rid of network-manager as that claims the device management with 'sudo service network-manager stop'
then you need to set up the encryption with wpa_supplicant. The exhausting documentation is at 'man wpa_supplicant' and 'man wpa_supplicant.conf'
what you need first is a config file for your network, 'man wpa_supplicant.conf' and /usr/share/doc/wpasupplicant/examples/ has a few examples for that, if you don't have any custom wpa settings the first example from wpa_supplicant.conf should be enough:

          # home network; allow all valid ciphers
          network={
               ssid="home"
               scan_ssid=1
               key_mgmt=WPA-PSK
               psk="very secret passphrase"
          }

where ssid is your network name and psk is your network password. once you have that run
sudo wpa_supplicant -i <your wireless inferface> -c <the config file you just created>
once that's running you should be connected to your router.
Now you need to set up the network, if you're using DHCP that's as simple as running
sudo dhclient
if you have custom settings set them in /etc/network/interfaces (see 'man interfaces') and use ifup and ifdown.

I hope that helps.

Revision history for this message
Allen (allenlitchfield) said :
#2

Thank you for the Answer, but before I try this, to start the Network-manager again, do I just need to do 'sudo service network-manager start'?

Revision history for this message
Philip Muškovac (yofel) said :
#3

On 08/03/2010 03:31 PM, Allen wrote:
> Question #119777 on Ubuntu changed:
> https://answers.edge.launchpad.net/ubuntu/+question/119777
>
> Allen posted a new comment:
> Thank you for the Answer, but before I try this, to start the Network-
> manager again, do I just need to do 'sudo service network-manager
> start'?
>

yes, maybe the applet might not recognise the re-started n-m again so
you might have to restart that too (at least that happened in kde here)
since the change isn't permanent, worst case is that you'll have to
reboot, n-m will be started on boot as usual

Revision history for this message
Allen (allenlitchfield) said :
#4

Hi, I made the conf file and named it House.conf, like this

allen@allen-laptop:/usr/share/doc/wpasupplicant/examples$ sudo nano House.conf

network={
        ssid="linksys"
        key_mgmt=WPA-PSK
        proto=WPA
        pairwise=TKIP
        group=TKIP
        psk="MyPassword"
}

saved it. Then did what you said, but get an unexpected result,

allen@allen-laptop:~$ sudo wpa_supplicant -i <linksys> -c <House.conf>
bash: syntax error near unexpected token `newline'

What Did I do wrong? Was it alright that I saved the .conf file under the 'examples' Folder? Sorry, I'm still a newbie :/

Revision history for this message
Best Philip Muškovac (yofel) said :
#5

On 08/03/2010 06:49 PM, Allen wrote:
> Question #119777 on Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+question/119777
>
> Allen posted a new comment:
> Hi, I made the conf file and named it House.conf, like this
>
>
> allen@allen-laptop:/usr/share/doc/wpasupplicant/examples$ sudo nano House.conf
>
>
> network={
> ssid="linksys"
> key_mgmt=WPA-PSK
> proto=WPA
> pairwise=TKIP
> group=TKIP
> psk="MyPassword"
> }
>
> saved it. Then did what you said, but get an unexpected result,
>
> allen@allen-laptop:~$ sudo wpa_supplicant -i <linksys> -c <House.conf>
> bash: syntax error near unexpected token `newline'
>
>
> What Did I do wrong? Was it alright that I saved the .conf file under the 'examples' Folder? Sorry, I'm still a newbie :/
>
that's not an error in the config file but with the command you're using
(the "bash:" at the beginning shows that it's the shell failing, not
wpa_supplicant itself)
actually you shouldn't use <...> in the command line, that was my way to
mark things that need to be replaced, also 'interface' is the wireless
interface name that is shown when you run 'iwconfig' so for me it would be:

sudo wpa_supplicant -i wlan0 -c House.conf

where wlan0 is my network card. This also assumes that you're running
that command in the folder where Home.conf is, if you're running it
somewhere else you'll need
sudo wpa_supplicant -i wlan0 -c
/usr/share/doc/wpasupplicant/examples/House.conf

that you stored the file in examples doesn't matter, you can store it
anywhere you have access to.

Revision history for this message
Allen (allenlitchfield) said :
#6

Oh ok, Thank you for your help SO much. I've been wanting to do this for months and have never found out how. It worked fine! I was confused because it showed no bars in the corner. But ifconfig said I was connected just fine. One last question, after I connect, it showed

WPA: Key negotiation completed with 00:1e:e6:a9:e9:fd [PTK=TKIP GTK=TKIP]
CTRL-EVENT-CONNECTED - Connection to 00:4e:e4:a4:e9:fd completed (auth) [id=0 id_str=]

But my allen@allen-laptop:~$ doesn't show up, So I'm guessing if I close the terminal, it stops connecting? Is there a way to stop it other than closing the terminal? Thanks again.

Revision history for this message
Allen (allenlitchfield) said :
#7

Thanks Philip Muškovac, that solved my question.

Revision history for this message
Philip Muškovac (yofel) said :
#8

> But my allen@allen-laptop:~$ doesn't show up, So I'm guessing if I close
> the terminal, it stops connecting? Is there a way to stop it other than
> closing the terminal? Thanks again.

Yes, the application stays open until you close it like this, to close
it just press Ctrl+C, that closes most command line applications.

Btw, if you're new to the command line you might want to read
https://help.ubuntu.com/community/UsingTheTerminal

Revision history for this message
Allen (allenlitchfield) said :
#9

Ok, thank you very much, Have a good day!