0

I have a server without access to the Internet but every now and then I need to update some of its packages. In order to make my life easier, I am using my laptop which is also running Linux to share the laptop's WiFi connection over the Ethernet interface of the laptop and connect the server to the laptop with normal Ethernet cable. Of course, I am also changing the GW on the server to match the IP address of the Ethernet interface of my laptop.

I know how I can do that from the network manager GUI but I was wondering how I can do the same using the command line. I want to find a solution which preferably doesn't involve writing IPTables custom rules. The idea is that I want to write a script and an alias, to do exactly that.

As soon as I am done I will share the source code with you guys, here and also on Github, that's the beauty of open-source after all.

4
  • Not sure i understood, you mean forward traffic from ethernet to wifi? – realpclaudio Feb 4 '20 at 15:48
  • changed the first paragraph to explain what exactly I want to do. I hope now my question is clearer. – Georgi Stoyanov Feb 4 '20 at 15:53
  • Ok, now it's clearer. I suspect you'll still need iptables to enable forward across interfaces and SNAT/MASQUERADE outbound packets. Also your server IP must be in same class of laptop (last one should be the gateway). Last but not least, routing must be enabled in your laptop kernel. Then you can save everything as a script and the opposite to undo. But my solution involves iptables, so i'm just commenting and not answering. – realpclaudio Feb 4 '20 at 16:03
  • Okay, I am curious to know your solution then. Regarding the traffic forwarding isn't it possible to use: sudo sysctl -w net.ipv4.ip_forward=1 command? – Georgi Stoyanov Feb 4 '20 at 16:09
3

A bit in a hurry, but basically... If enp0s3 is your LAN and enp0s8 your WLAN:

Laptop side:

#ENABLE PACKET FORWARD
echo 1 > /proc/sys/net/ipv4/ip_forward

#YOU COULD JUST ENABLE FORWARD FOR EVERYTHING WITHOUT LIMITATIONS, USING INTERFACES JUST TO BE MORE SPECIFIC
iptables -A FORWARD -i enp0s3 -o enp0s8 -j ACCEPT
iptables -A FORWARD -i enp0s8 -o enp0s3 -j ACCEPT

#MASQUERADE OUTBOUND CONNECTIONS
iptables -t nat -A POSTROUTING -o enp0s8 -j MASQUERADE

This can be easily scripted, and all conf is cleaned up at reboot.

Server side:

ASSIGN VALID STATIC NETWORK CONFIGURATION WITH LAPTOP IP AS GATEWAY
ASSIGN VALID DNS (VIA /etc/resolv.conf IF LINUX), OR YOU CAN DO ADDITIONAL IPTABLES RULES TO DNAT DNS TRAFFIC LAPTOP SIDE
1

When you use the GUI, that means you basically activate a connection profile in NetworkManager. You can do just the same from command line.

First, find the name of the profile in question, by looking at nmcli connection output. Then activate the profile with nmcli connection up "$PROFILE_NAME".

And of course you can create and modify your profile via command line only, eg by using nmcli. See the manual pages. The relevant setting in the profile is ipv4.method=shared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.