Difference between revisions of "Ad-hoc networking"

From ArchWiki
Jump to: navigation, search
m (Other Resources)
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:Networking]]
 
[[Category:Networking]]
 
[[Category:Wireless Networking]]
 
[[Category:Wireless Networking]]
{{i18n|Ad-hoc networking}}
+
[[zh-CN:Ad-hoc networking]]
{{stub}}
+
 
You can setup ad-hoc [[network]]ing to make two computers communicate or share an internet connection. The following scripts serve as an example. '''You will have to change the interface names for this to work on your system'''.
 
You can setup ad-hoc [[network]]ing to make two computers communicate or share an internet connection. The following scripts serve as an example. '''You will have to change the interface names for this to work on your system'''.
  
Line 19: Line 18:
 
   
 
   
 
  #starts the ad hoc server
 
  #starts the ad hoc server
  ifconfig ath0 down
+
  ip link set dev eth0 down
 
  iwconfig ath0 mode ad-hoc
 
  iwconfig ath0 mode ad-hoc
 
  iwconfig ath0 channel 4
 
  iwconfig ath0 channel 4
 
  iwconfig ath0 essid 'mine'
 
  iwconfig ath0 essid 'mine'
  ifconfig ath0 10.0.0.1 up
+
  ip addr add 10.0.0.1 dev ath0
 +
ip link set dev ath0 up
 
   
 
   
 
  #forwards the ad hoc network to the router
 
  #forwards the ad hoc network to the router
Line 51: Line 51:
 
  iwconfig eth1 essid mine
 
  iwconfig eth1 essid mine
 
  iwconfig eth1 mode Ad-Hoc
 
  iwconfig eth1 mode Ad-Hoc
  ifconfig eth1 10.0.0.2 up
+
  ip addr add 10.0.0.2 dev eth1
  route add default gw 10.0.0.1
+
ip link set dev eth1 up
 +
  ip route add default via 10.0.0.1
  
 
To share an internet connection, you need to tell the clients about the DNS servers. See the /etc/resolv.conf file on the server.
 
To share an internet connection, you need to tell the clients about the DNS servers. See the /etc/resolv.conf file on the server.
Line 87: Line 88:
 
== Other Resources ==
 
== Other Resources ==
 
* [http://www.ubuntugeek.com/creating-an-adhoc-host-with-ubuntu.html Creating an Ad-Hoc Host with Ubuntu]
 
* [http://www.ubuntugeek.com/creating-an-adhoc-host-with-ubuntu.html Creating an Ad-Hoc Host with Ubuntu]
 +
* [http://go2linux.garron.me/linux/2011/03/share-your-3g-internet-connection-over-wifi-linux-ipod-touch-925 Share your 3G Internet connection over wifi]

Revision as of 23:31, 9 June 2013

You can setup ad-hoc networking to make two computers communicate or share an internet connection. The following scripts serve as an example. You will have to change the interface names for this to work on your system.

On the server

The server script works for a wireless card with an atheros driver. To make it work with a different card is trivial. More info there: https://help.ubuntu.com/community/WifiDocs/Adhoc. If you just want to connect two computers with no sharing of internet connection, you can leave out the iptables bit in the server script.

#!/bin/bash
####### this part should change or simply be removed depending on the network card
#recreates the ath0 interface in ad hoc mode
modprobe -r ath_pci
modprobe ath_pci autocreate=adhoc
wlanconfig ath0 destroy
wlanconfig ath create wlandev wifi0 wlanmode adhoc
#######

#starts the ad hoc server
ip link set dev eth0 down
iwconfig ath0 mode ad-hoc
iwconfig ath0 channel 4
iwconfig ath0 essid 'mine'
ip addr add 10.0.0.1 dev ath0
ip link set dev ath0 up

#forwards the ad hoc network to the router
iptables -A FORWARD -i ath0 -o eth0 -s 10.0.0.0/24 -m state --state NEW -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Here is a WPA Supplicant config file WPA based adhoc network.

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
ap_scan=2
network={
   ssid="my-wifi"
   mode=1
   frequency=2412
   proto=WPA
   key_mgmt=NONE
   pairwise=NONE
   group=TKIP
   psk="s0m3k3y$"
}

On the client

Run the following on the client. If you want multiple clients, you will have to change the IP address to be unique.

#!/bin/bash
iwconfig eth1 essid mine
iwconfig eth1 mode Ad-Hoc
ip addr add 10.0.0.2 dev eth1
ip link set dev eth1 up
ip route add default via 10.0.0.1

To share an internet connection, you need to tell the clients about the DNS servers. See the /etc/resolv.conf file on the server.

Using NetworkManager

Install and set-up NetworkManager on both the server and the client.

On the Server

Plug the wireless adapter into the computer which you wish to use as the server. Click on the nm-applet in the dock and choose "Create New Wireless Network".

With the dialog box, type in your SSID and choose your security type and password, then click "Create".

Afterwards, right-click on the nm-applet in the dock and choose "Edit Connections". Click on the "Wireless" tab and select the SSID for the network you just created. Click on "Edit".

With the dialog box which pops up, make sure "Mode" is set to "Ad-hoc".

In "IP4 Settings", change "Method" to "Manual" and click on "Add" next to the "Addresses" box. It then should highlight the box to the left where you have to enter three IP addresses.

For "Address" choose a value between 192.168.0.0 to 192.168.255.255; "Netmask", 255.255.255.0; "Gateway", 1.1.1.1. then click save.

Finally, click on the nm-applet in the tray again and select "Connect to Hidden Wireless Network"; then in the "Connection" drop-down box select the network which you just created. It should then connect.

On the Client

On the nm-applet in the tray, click on the wireless network you created and connect to it; afterwards, right-click and choose "Edit Connections". Again, click on the "Wireless" tab and select the SSID for your network and choose "Edit".

In the "IP4 Settings" again change the "Method" to "Manual" and click on "Add" next to the "Addresses" box.

For the "Address" choose a value between 192.168.0.0 to 192.168.255.255 but make sure it is different to the one you selected for the server; afterwards fill in "Netmask", 255.255.255.0; "Gateway", 1.1.1.1. then click save.

It should then allow you to connect to the server (try pinging the server to ensure this).

Other Resources