Share Internet via Ad-Hoc

On Computer 1, connected to the internet via cable:
# ifconfig wlan0 down # iwconfig wlan0 mode Ad-Hoc # ifconfig wlan0 192.168.0.1 up # iwconfig wlan0 essid rede # iwconfig wlan0 key 123456789AOn Computer 2:
# ifconfig wlan0 down # iwconfig wlan0 mode Ad-Hoc # ifconfig wlan0 192.168.0.2 up # iwconfig wlan0 essid rede # iwconfig wlan0 key 123456789A # route add default gw 192.168.0.1 # sh -c "echo 'nameserver 8.8.8.8' >> /etc/resolv.conf"
The Ad-Hoc network is already configured. You can now ping between both machines.
To share the internet, we need to add some rules to the firewall.
Install
iptables:# pacman -S iptablesConfigure iptables:
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # iptables -A FORWARD -i eth0 -o wlan0 -m state --state ESTABLISHED,RELATED -j ACCEPT # iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT # sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" # /etc/rc.d/iptables save # /etc/rc.d/iptables restart

I’m a Brazilian computer engineer based in Germany, passionate about tech, science, photography, and languages.
I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.
I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, mentoring, and contributing with code and translations.
On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.
Browse my gallery for some of my photography.
Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.
Always happy to connect! 🙂