Network connection from command line

Asked by Alexander Zemerov on 2007-08-16

Hi,
I have wireless card and can connect to Internet using network-admin. It is fine, but I want to do the same from command line. In Ubuntu community I found "WiFiHowto" page which recommends "ifup" and ifdown" commands. When I try
> ifup eth0
I get message
"eth0=eth0 interface doesn't exist"
or something like that (I don't remember exactly). But if I run
> iwconfig
responce looks like
...
* eth0 IEEE 802.11g ESSID:"NETGEAR"
...
(I ommited lines for more details and for loopback interface)

My assumption is these commands require configuration of /etc/network/interfaces file. My file has no interface entry for my adapter, only for loopbak. So I believe network-admin uses different configuration information. Sure, I can configure /etc/network/interfaces but I don't want mess with two different configuration methodology on my box.
So, is there any command-line analogue to network-admin using the same configuration data? Or may be I misunderstood process of network configuration?

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
2007-08-17
Last reply:
2007-08-18
Basilio Kublik (sourcercito) said : #1

Hi Alexander
if you configure your wireless interface in /etc/network/interfaces you'll be able to use the ifup/ifdown command, if the interface isn't configured there, you would have to use a couple o commands to connect here are some examples:

wep encryption with dhcp:
================
user@machine:~$ sudo ifconfig eth0 up
user@machine:~$ sudo iwconfig essid "My-AP-Name" key s:my_secret_wepkey
user@machine:~$ sudo dhclient eth0

wep encryption with static ip:
=================
user@machine:~$ sudo ifconfig eth0 10.10.10.2 netmask 255.255.255.0 up
user@machine:~$ sudo route add -net 10.10.10.0 netmask 255.255.255.0 gw 10.10.10.1 eth0
user@machine:~$ sudo iwconfig essid "My-AP-Name" key s:my_secret_wepkey

dhcp without encryption:
===============
user@machine:~$ sudo iwconfig eth0 essid "My-AP-Name"
user@machine:~$ sudo dhclient eth0

now this examples should look like this in the interfaces file:
wep encryption with dhcp:
=================
auto eth0
iface eth0 inet dhcp
         wireless-essid My-AP-Name
         wireless-key s:my_secret_wepkey

wep encryption with static ip:
=================
auto eth0
iface eth0 inet static
         wireless-essid My-AP-Name
         wireless-key s:my_secret_wepkey
         address 10.10.10.2
         netmask 255.255.255.0
         gateway 10.10.10.1

dhcp without encyption:
==============
auto eth0
iface eth0 inet dhcp
         wireless-essid My-AP-Name

now in my opinion the most interesting way to configure it is using wpa_supplicant, works for open networks, wep encrypted and wpa/2 personal and enterprise.

first we need some examples of wpa_supplicant.conf file, i'll give you one of each option.

#### start of wpa_supplicant.conf file ####
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
eapol_version=1
ap_scan=1
fast_reauth=1

### any open network ###
network={
        ssid=""
        key_mgmt=NONE
}

### wep encrypted network ###
network={
        ssid="MY-AP-Name-wep"
        key_mgmt=NONE
        wep_key0="my_supersecret_key"
        wep_tx_keyidx=0
}

### wpa personal encrypted network ###
network={
        ssid="My-Ap-Name-wpa_psk"
        key_mgmt=WPA-PSK
        proto=WPA
        pairwise=CCMP TKIP
        group=CCMP TKIP
        psk="My super secret paraphrase that could contain spaces"
        priority=3
}

### wpa enterprise encrypted network ###
network={
        ssid="MY-AP-Name-wpa_enterprise"
        key_mgmt=WPA-EAP
        proto=WPA
        pairwise=CCMP TKIP
        group=CCMP TKIP
        eap=TLS
        <email address hidden>"
        ca_cert="/etc/wpa_supplicant/certs/cacert.pem"
        private_key="/etc/wpa_supplicant/certs/cert-clt.p12"
        private_key_passwd="password to load the private key, this doesn't goes through the network"
}

### end of wpa_supplicant.conf ###

now to user wpa2 instead of wpa, you just need to change the proto from WPA to WPA2.
so you want to connect to a network using wpa_supplicant, first you need to start the wpa_supplicant process:

user@machine:~$ sudo ifconfig eth0 up
user@machine:~$ sudo /usr/bin/wpa_supplicant -ieth0 -Dwext -c /etc/wpa_supplicant/wpa_supplicant.conf
user@machine:~$ sudo ifconfig eth0 add 10.10.10.2 netmask 255.255.255.0
user@machine:~$ sudo route add -net 10.10.10.0 netmask 255.255.255.0 gw 10.10.10.0 eth0

of course if you use dhcp in this last case, you could skip the ifconfig/route commands and use dhcpclient after you start the wpa_supplicant process.
user@machine:~$ sudo /usr/bin/wpa_supplicant -ieth0 -Dwext -c /etc/wpa_supplicant/wpa_supplicant.conf
user@machine:~$ sudo dhclient eth0

now, this could be done via interfaces file as well, and here is how:

### WPA with DHCP ###
auto eth0
iface eth0 inet dhcp
        wpa-ssid My-AP-Name
        wpa-driver wext
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

### WPA with static IP ###
auto eth0
iface eth0 inet static
        wpa-ssid My-AP-Name
        wpa-driver wext
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
        address 10.10.10.2
        netmask 255.255.255.0
        gateway 10.10.10.1

note that in those last two examples of the interfaces file, the ssid is mentioned just for set is as preferred network.
with any of the configurations in your interfaces file you can use ifup/ifdown commands.

for more detailed, and maybe more clear information or if you have a more exotic network you could look at:
man 7 wireless
man 5 interfaces
man 8 wpa_supplicant
man 8 route
man 8 ip
man 8 ifconfig

hope this solve your question, instead of boring you or give you a headache.

Alexander Zemerov (azemerov) said : #2

Basilio,
Thank you. It was helpful information. I didn't know about wpa_supplicant.
But actually my question was - can I avoid _any_ manual configuration and use current configuration prepared by network-admin? I understand there are several ways to control network in Linux, each uses it's own set of configuration files. I can easily configure /etc/network/interfaces, for example, it is no problem at all, but my point is - somewhere in the system I already have completed configuration prepared by network-admin. I know it for sure because network-admin on my box works fine without config information in interfaces file. I want to follow the "network-admin" way with the only difference - do it in command line, not in GUI.
Again, it is not actual problem, just curiosity. Right now I use ifupdown + /etc/network/interfaces and everything works fine. But I want to be more familiar with network-admin approach.

Basilio Kublik (sourcercito) said : #3

now i get what you want, unfortunately i only remember one piece of ancient software that i use a couple of times, this is linuxconf, a curses based interface to admin almost anything from users to services, i really don't know if at some point they add wireless networking support, but you could look it anyway just to find out.

the other not so close approach could be wifi-radar, which can be configured using an pygtk interface under X or editing by hand the configuration file, and can be run as daemon, so it should connect to any open or configured network at boot time.

and the last think i could think of at the moment is the wpa_supplicant approach, but you should configure by hand anyway so i don't think that's what you're looking for.

at the moment that's all i can remember/think of that could "satisfy" in some way your need.

but even if you don't find any real solution to this, you can always start a new blueprint with the specs of what you want, and probably more than one person will follow you.

hope that's of any useful help.

Can you help with this problem?

Provide an answer of your own, or ask Alexander Zemerov for more information if necessary.

To post a message you must log in.