Question
Is there a generic script that shares Wi-Fi over Ethernet from an Ubuntu Server to another (that doesn't have a Wi-Fi card)? I saw this question (and this, and this) solves it for the version with GUI using mouse clicks, and I thought I could prevent re-inventing the wheel if an equivalent script already exists.
Server-side (CLI-working)
A stripped down version of this script found on GitHub, combined with the instructions on this site worked on the server side.
sudo nano /etc/sysctl.conf
Uncomment (could be done with sudo tee
command in script):
net.ipv4.ip_forward=1
Save with ctrl+s
, ctrl+x
. Reload using:
sudo sysctl -p /etc/sysctl.conf
Create the following script named share_wifi.sh
:
lan_device="eth0"
online_device="wlan0"
iptables -t nat -A POSTROUTING -o $online_device -j MASQUERADE
iptables -A FORWARD -i $online_device -o $lan_device -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $lan_device -o $online_device -j ACCEPT
Make it runnable and run it with:
chmod +x share_wifi.sh
sudo ./share_wifi.sh
Next, I stored the local IP address of the "server" by running:
hostname -I
Which returned something like 123.145.16.18 415.141.14.15
, I stored the first IP address that was returned.
Client-side (GUI-only)
Yet, I set the client pc manually using a GUI instead of CLI. First I tried the CLI commands, but these returned errors:
sudo ip route del default
sudo ip route add default via <ip of gateway pc>
sudo nano /etc/resolv.conf
And added: nameserver 8.8.8.8
, saved it with ctrl+s
, ctrl+x
.
This did not provide internet to the device, so I tried a manual/GUI solution, which did work:
I clicked in the top right Wi-Fi/LAN symbol on wired settings
, clicked on tab: IPv4
, clicked on IPv4 Method
and in that section clicked on Manual
. Next in the section Addresses
I filled in:
Address:123.145.16.55
Netmask:255.255.255.0
Gateway:123.145.16.18
- Note in
Address
I entered a random IP address in the same
format/range as the server local IP address. So for example the
server had: 123.145.16.18
and I picked 123.145.16.55
, I think I
got lucky there that that IP was not yet taken by another device.
- Note in
Netmask
I entered a default/standard value.
- Note in
Gateway
I entered the server local IP address that I noted down earlier with the hostname -I
command.
Next, I clicked on apply, and that was it, the client pc was automatically connected to the internet.
So I'm aiming to automate the last step, Ideally by detecting the client pc over ssh automatically, logging in and running a script on it from the host pc, that basically sets up that manual connection to the host. Otherwise by using two separate scripts, one for the host, another for the client.