Linux – How to join an ad-hoc wireless network in Linux

ad hoc networklinuxUbuntuwireless-networking

How do you join two or more Linux machines to the same ad-hoc wireless network?

I have two Ubuntu 12.04 laptops that I'd like to connect. Following the command line instructions in Ubuntu's wiki docs, I configured them to both connect to "my network". However, running iwconfig shows they're using different cells, which apparently means that even though they're using the same ESSID, they're separate networks.

e.g. on machine 1, I ran:

user@localhost:~$ sudo service network-manager stop
user@localhost:~$ sudo ip link set eth1 down
user@localhost:~$ sudo iwconfig eth1 mode ad-hoc
user@localhost:~$ sudo iwconfig eth1 channel 4
user@localhost:~$ sudo iwconfig eth1 essid 'my network'
user@localhost:~$ sudo iwconfig eth1 key 1234567890
user@localhost:~$ sudo ip link set eth1 up
user@localhost:~$ sudo ip addr add 192.168.1.1/16 dev eth1
user@localhost:~$ iwconfig
lo        no wireless extensions.

eth2      IEEE 802.11abg  ESSID:"my network"  
          Mode:Ad-Hoc  Frequency:2.412 GHz  Cell: 52:9D:A0:90:28:02   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on

and on machine 2, I ran:

user@localhost:~$ sudo service network-manager stop
user@localhost:~$ sudo ip link set eth1 down
user@localhost:~$ sudo iwconfig eth1 mode ad-hoc
user@localhost:~$ sudo iwconfig eth1 channel 4
user@localhost:~$ sudo iwconfig eth1 essid 'my network'
user@localhost:~$ sudo iwconfig eth1 key 1234567890
user@localhost:~$ sudo ip link set eth1 up
user@localhost:~$ sudo ip addr add 192.168.1.2/16 dev eth1
user@localhost:~$ iwconfig
lo        no wireless extensions.

eth2      IEEE 802.11abg  ESSID:"my network"  
          Mode:Ad-Hoc  Frequency:2.412 GHz  Cell: B6:D6:92:5D:E5:E4
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on

Why didn't they resolve to the same cell number? How do I get them to use the same cell number?

Edit: Even if the use iwconfig's ap parameter to explicitly set cell number on both machines, I still don't seem to "connect" to the ad-hoc network. Attempting to ping either IP results in the error connect: Network is unreachable.

Best Answer

you useiwconfig correctly, as you said the problem is that they don't get the same cell ID. On ad-hoc mode, MAC address is assigned as cell ID. You can force wifi devide to use a predefined cell ID by executing sudo iwconfig eth1 ap B6:D6:92:5D:E5:E4 ( e.g., after sudo iwconfig eth1 essid 'my wlan').

When two device creates an ad-hoc WLAN, they broadcast bacons, so that a node gets aware of if there is another ad-hoc wlan with the same essid, so that they can join (by changing the cell ID). Normally your decides should have joined already. But it might be because of a driver or the chipset of your wireless device. In my opinion, Atheros chipsets has the less problem on wireless networking.

If it would still not work with that command, then the problem is not iwconfig related.

Btw, you should also note that iwconfig is depreciated, you should use iw instead. The other good tool is wpa_supplicant to use.

Related Question