In this post I will show you how simple it is to connect two clients (linux, of course) ad-hoc via wifi without needing additional tools.
What you need: root access on the machines!
First, identify your wifi nic:
root@host:~$ iw dev phy#0 Interface wlan0 ifindex 4 type managed
In this case we’ve identyfied wlan0 as our wireless interface. As you see, the type of this interface is managed. We have to change the type to ibss:
root@host:~$ iw dev wlan0 set type ibss
Let’s verify this:
root@host:~$ iw dev phy#0 Interface wlan0 ifindex 4 type ibss
Now let’s join a network. In this case to join means if there’s already a network then join it, otherwise create a new one. The manpage of iw says
ibss join [HT20|HT40+|HT40-|NOHT] [fixed-freq] [ ] [beacon-interval ] [basic-rates ] [mcast-rate ] [key d:0:abcde]
In this example we choose MyNetwork as SSID and frequency 2417 GHz as channel 2.
So, the following command on both machines will let them talk to each other.
root@host:~$ iw wlan0 ibss join MyNetwork 2417 key d:0:f00b4r
But, what do we need for network communication? Alright, ip addresses 😉
First flush the adapter (this step is not mandatory):
root@host:~$ ip address flush wlan0
Next, set up ip addresses (for example 10.10.10.1/24 & 10.10.10.2/24):
root@host:~$ ip address add 10.10.10.1/24 dev wlan0
If not done already, bring up the device:
root@host:~$ ip link set wlan0 up
Now you should be able to ping each other:
root@host:~$ ping 10.10.10.2 PING 10.10.10.2 (10.10.10.2) 56(84) bytes of data. 64 bytes from 10.10.10.2: icmp_req=1 ttl=64 time=0.766 ms 64 bytes from 10.10.10.2: icmp_req=2 ttl=64 time=0.773 ms 64 bytes from 10.10.10.2: icmp_req=3 ttl=64 time=0.694 ms 64 bytes from 10.10.10.2: icmp_req=4 ttl=64 time=0.705 ms --- 10.10.10.2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 2997ms rtt min/avg/max/mdev = 0.694/0.734/0.773/0.044 ms
Et voilà, that’s it!
Take care: The connection is only wep encrypted. Don’t share sensitive data through this channel!!!
Another note: In many linux distribution NetworkManager is responsible for managing network devices. Often this causes in trouble, to avoid this you can tell the NetworkManager to ignore devices. But in the above example it’s not recommended because an ad-hoc connection is only a temporary solution. It’s easier to stop the service performing the following command:
root@host:~$ /etc/init.d/network-manager stop
Killing instances of wpa_supplicant and dhclient is also recommended:
root@host:~$ killall dhclient && killall wpa_supplicant