FROM: NETWORKSHOP, OTHERNET
SKILL LEVEL: BEGINNER
Knowing how to manually configure a network interface, like a wired or wireless network adapter, on Linux is vital if there is no automatic configuration service (like DHCP) offered on the network to which you’re attached. It is of further use knowing how to set up networking hardware on the command line, especially when you are logged into a machine somewhere else.
An example may be you are connected to a machine over a wireless interface and need to configure the wired card so that it can connect to the Internet. At these times a Graphical User Interface is of no use, especially on small embedded computers without connected displays and keyboards.
List available network interfaces
ifconfig (or interface config) is a tool for manipulating network devices (Network Interface Controller, or NIC) on UNIX systems.
The following command will list all available devices to be configured on your system.
sudo ifconfig -a
The -a switch stands for list all, regardless of whether they’re configured or not.
When run on the system on which I’m writing this document, the following output is given:
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:154805 errors:0 dropped:0 overruns:0 frame:0 TX packets:154805 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:73061530 (69.6 MiB) TX bytes:73061530 (69.6 MiB) eth0 Link encap:Ethernet HWaddr 00:24:a5:9c:63:b9 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) wlan0 Link encap:Ethernet HWaddr e8:94:f6:1e:c4:df BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Here we have an Ethernet device eth0 and a wireless wlan0 device. We also have lo or loopback device, used by software services and their clients running on the local machine. It will not be covered here.
Devices are generally numbered sequentially, so if we were we to have two Ethernet devices, one would probably be eth0 and the other eth1. If we were to have three wireless devices they may be named wlan0, wlan1 and *** wlan2***.
Network devices are referred to as NICs or Network Interface Controller.
Configuring network interfaces
Join a network with NIC eth0
Let’s assume I have a network switch to which other devices are connected by cable. The network is a 10.0.0.* network. There is no automatic network configuration service such as DHCP running on this network.
1. CHOOSE AN IP ADDRESS
How we choose an IP address depends on the situation at hand but it cannot be an address used by another device (network host). Let’s assume 10.0.0.11 is free for me to use.
2. SET THE IP ADDRESS
I can assign the IP address 10.0.0.11 like so:
julian@ce:~$ sudo ifconfig eth0 10.0.0.11
Note that I have to use sudo as I am altering the state of hardware, a low level operation. Now I can type the following to check the IP was set:
julian@ce:~$ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:24:a5:9c:63:b9 inet addr:10.0.0.11 Bcast:10.255.255.255 Mask:255.0.0.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
I can see inet addr:10.0.0.11 in the output. So far the IP has been set but I can’t yet be sure I’m connected to other devices.
3. CHECK CABLES ARE CONNECTED
Ensure the Ethernet cable has active link lights (usually green) when the device is connected to the switch or other network hardware.
4. TEST WITH A PING
Let’s assume I know a laptop on the network already has the address 10.0.0.17. I can use the command ping to send it a tiny packet of data. If it is returned to me, I know I am not only on the network but able to converse with that device:
julian@ce:~$ ping 10.0.0.17 PING 10.10.0.17 (10.0.0.17) 56(84) bytes of data. 64 bytes from 10.0.0.17: icmp_req=1 ttl=64 time=15.3 ms 64 bytes from 10.0.0.17: icmp_req=1 ttl=64 time=14.7 ms 64 bytes from 10.0.0.17: icmp_req=1 ttl=64 time=14.8 ms 64 bytes from 10.0.0.17: icmp_req=1 ttl=64 time=15.1 ms 64 bytes from 10.0.0.17: icmp_req=1 ttl=64 time=14.9 ms
Seeing that the data is returning (“64 bytes from 10.0.0.17…”), I can hit CTRL-C to close ping and start interacting with that device over the network.
If I was so see the following in the output, I would know I can’t reach that particular network host:
From 10.0.0.17 icmp_seq=1 Destination Host Unreachable
I would then try to ping other network hosts. If I can reach others but not this device, then I’d know the issue is likely with the device at 10.0.0.17, rather than mine.
Request an IP to be automatically assigned to NIC eth0
Let’s now assume I’m later in a different location and would like to use a local network there.
On this network there is a DHCP service (Dynamic Host Control Protocol) that can automatically assign me an IP, assuming there’s a free slot and/or my device isn’t blocked. This is almost always the case in universities, offices, studios as it’s much more convenient than having to assign an IP oneself.
I don’t need to know anything about the network, which IP range it is operating on or other details.
1. ASK A DHCP SERVICE ON THE NETWORK FOR AN IP
I can ask the DHCP server, usually running on a network router, to grant me an IP on the eth0 ethernet NIC. I use the -v switch for verbose, so that I can see the output of the process.
julian@ce:~$ sudo dhclient -v eth0 Internet Systems Consortium DHCP Client 4.2.2 Copyright 2004-2011 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/eth0/00:24:a5:9c:63:b9 Sending on LPF/eth0/00:24:a5:9c:63:b9 Sending on Socket/fallback DHCPREQUEST on eth0 to 255.255.255.255 port 67 DHCPACK from 192.168.100.1 bound to 192.168.100.152 -- renewal in 37134 seconds.
Reading the above output I can see that the DHCP service at 192.168.100.1 has granted me the address 192.168.100.152. It will be renewed in 37134 seconds.
2. TEST WITH A PING
Just as before, we need to test that we truly can read and write to other NICs on the network. If you believe the network to which you’ve connected should provide you with access to the Internet, you might try pinging a remote machine:
julian@ce:~$ ping criticalengineering.org PING criticalengineering.org (188.8.131.52) 56(84) bytes of data. 64 bytes from 91-64-73-247-dynip.superkabel.de (184.108.40.206): icmp_req=1 ttl=64 time=10.35 ms 64 bytes from 91-64-73-247-dynip.superkabel.de (220.127.116.11): icmp_req=2 ttl=64 time=10.33 ms 64 bytes from 91-64-73-247-dynip.superkabel.de (18.104.22.168): icmp_req=3 ttl=64 time=10.31 ms 64 bytes from 91-64-73-247-dynip.superkabel.de (22.214.171.124): icmp_req=4 ttl=64 time=10.34 ms
Disconnect NIC eth0 from a network
You can disconnect from a network at any moment by ‘taking down’ the NIC. It’s as effective as pulling out the network cable:
julian@ce:~$ sudo ifconfig eth0 down
Connect to a wireless network on NIC wlan0
Wireless NICs like wlan0 have two components: the radio part that connects (associates) to a wireless network on a specific channel and the networking part that operates on it, using just the same technology as a wired NIC like eth0. For this reason we have to use two different tools to configure wireless NICs, iwconfig (wireless configuration) and ifconfig (network configuation).
Here I can look at the wireless state of my NIC wlan0 using iwconfig:
julian@ce:~$ iwconfig wlan0 wlan0 IEEE 802.11bgn ESSID:off/any Mode:Managed Access Point: Not-Associated Tx-Power=0 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off
As the output shows, the NIC is not currently associated to any network.
Here I will associate with a network (with the ESSID) CE. This is no different from selecting a wireless network on a device like a smartphone:
1. BRING THE NIC UP
Unlike a wired NIC like eth0, which is activated the moment it gets an IP, I have to bring the NIC up before I can use it.
julian@ce:~$ sudo ifconfig wlan0 up
2 . SET THE ESSID. ASK TO ASSOCIATE
julian@ce:~$ sudo iwconfig wlan0 essid CE
Looking at iwconfig output now for wlan0, we can see it has changed. The ESSID option shows “CE”. We can also see the quality of the link (almost perfect at 68/70):
wlan0 IEEE 802.11abgn ESSID:"CE" Mode:Managed Frequency:2.472 GHz Access Point: 00:90:4C:D0:0F:A3 Bit Rate=54 Mb/s Tx-Power=15 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off Link Quality=68/70 Signal level=-42 dBm Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:61 Missed beacon:0
3. JOIN THE NETWORK
Just because I’m associated doesn’t mean that I’m on the network. I still need to either set or ask for an IP.
Interestingly enough I can manually configure any IP I like and use any wireless access point (hotspot) I like to build a network. Generally with wireless routers however the goal is to connect to the Internet to which the wireless router itself has access. Being a router, which is in effect a little computer, it probably runs a DHCP service.
We can try and ask for an IP just as we did above with eth0:
julian@ce:~$ sudo dhclient wlan0
Scanning for wireless networks
First, bring up your wireless device, if not already:
julian@ce:~$ sudo ifconfig wlan0 up
Now scan, using the wireless tool iwlist:
julian@ce:~$ sudo iwilist wlan0 scan
Look for ESSID in the output.