Laptops are common these days and so is wireless networking that comes with it. Almost all of us use Wireless Router for wireless networking and internet sharing. But most of us might not be familiar with ad-hoc mode that works in every wireless card and can eliminate the use of router altogether. Here's how:
To setup ad-hoc wireless, you basically set two or more wireless cards to same ESSID in ad-hoc mode and different IP addresses.
Setting the wireless card in ad-hoc mode
Generally iwconfig is used to set the wireless mode, but if you have madwifi drivers (see this too), the commands are slightly different. Go to terminal and type,
iwconfigIf you see interfaces named wlan0, wlan1, etc iwconfig will work. If you see interface names similar to ath1 and wifi0, then madwifi specific commands are to be used.
For general wifi cards:
sudo iwconfig wlan0 mode ad-hocReplace wlan0 with the interface name as listed by iwconfig
For cards using madwifi driver:
sudo wlanconfig ath1 destroyNow set essid. Pick some name like 'adhocwifi'
sudo wlanconfig ath1 create wlandev wifi0 wlanmode adhoc
sudo iwconfig wlan0 essid adhocwifiIf required, to set encryption key
sudo iwconfig wlan0 key 1234567890Now set IP address:
sudo ifconfig wlan0 192.168.0.1Follow the above steps for another wireless card and set IP address in same subnet, say 192.168.0.2, and ping each other.
The whole ad-hoc mode setting can also be done using GUI network-manager, but since i'm not so much fond of graphical interface i'm not covering it. It should be simple.. :)
Internet Connection Sharing
Now to share the internet over wireless,
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADEwhere ppp0 is the connection you want to share (PPPoE connection in this case)
You also need to enable IP forwarding:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"Or, to enable permanently add the following line to /etc/sysctl.conf
net.ipv4.ip_forward=1Some ISPs might limit the TTL so that you wont be able to share the internet. Fix:
sudo iptables -t mangle -A PREROUTING -j TTL --ttl-inc 1
Now to use the shared internet on another computer, set it to ad-hoc mode and assign IP address in the same subnet as described above and perform the following:
1. Set the IP of computer sharing internet as gateway
sudo route add default gw 192.168.0.12. Set DNS server. We're using Google's DNS.
sudo sh -c "echo 'nameserver 18.104.22.168' >> /etc/resolv.conf"You can also use IP and DNS Masquerading to ease the task.
1. Connect to the Wireless Network (in this case 'adhocwifi')
2. Go to Network Connections
3. Right click the Wireless Connection
4. Select the Internet Protocol (TCP/IP) and click Properties
5. Set IP to 192.168.0.2 (or accordingly in the same subnet as set on Linux box), gateway to 192.168.0.1 (as set on Linux box) and DNS to 22.214.171.124