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 8.8.8.8' >> /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 8.8.8.8
Worked like a charm... :)
ReplyDeletePerfect!
ReplyDeleteThanks for the tip!
What if I don't want to use an IP, instead share only the internet? I used NetworkManager and use share to other computers on IPv4 settings (no IP entered) and manage to share the connection with my other PC.
ReplyDeleteBut I wanna try the command line for that. Can you please help me?
there's no such thing as not using IP, maybe the network manager uses dhcp to assign IP. please follow tutorial at http://lartc.org/ for commandline details.
ReplyDeletesorry for the confusion, what I meant is dchp. I am just new to linux :(
ReplyDeletehi there,I used NetworkManager and use share to other computers on IPv4 settings (no IP entered) and manage to share the connection with my other PC.
ReplyDeleteMichael
security+ test
USA
hey nice blog, remember to visit my blog at
ReplyDeleteInternet Connection Setup Melbourne
Good write up man.. help for all beginners of linux as well as networking and ad-hoc
ReplyDeleteKeep writing. All the very best.
R@J
Appreicate your thoughts, Im not always in agreement, but you do cause a peron to think keep blogging!carpet cleaning adelaide
ReplyDeleteHow can i share my internet connection over WiFi to connect my Android device through WiFi ..??
ReplyDeleteThanks, helped getting started with ad-hoc wireless on Linux. For increased convenience we can run the "dnsmasq" daemon on the internet gateway which provides dns and dhcp services. (Set wireless network interface and IP range in /etc/dnsmasq.conf.) On other machines, "dhclient" then does the IP auto-configuration.
ReplyDeletegreat blog ... thanks for sharing....
ReplyDeletei agree with you...
Deletecollaboration technology
Hello
ReplyDelete"sudo iwconfig wlan0 mode ad-hoc" gives error:
satyam@ubuntu:~$ sudo iwconfig wlan0 mode ad-hoc
[sudo] password for satyam:
Error for wireless request "Set Mode" (8B06) :
SET failed on device wlan0 ; Operation not supported.
satyam@ubuntu:~$
Please help
This blog is truly useful to convey overhauled instructive undertakings over web which is truly examination. I discovered one fruitful case of this truth through this blog. I will utilize such data now.HPE ProLiant ML350 Gen9
ReplyDelete