PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Internet sharing WIFI to LAN on Linux Easy

Go down

Internet sharing WIFI to LAN on Linux Easy Empty Internet sharing WIFI to LAN on Linux Easy

Post by jamied_uk 1st November 2024, 23:06

Server Side Linux to host internet through LAN Port (even on RPI 5) from wifi connection!

(This will forward inet traffic internet sharing easy asf)!

Step 1 connect your RPI 5 (or linux laptop / pc) to your wireless internet!

Step 2 (The Setup)

Code:
sudo apt update
sudo apt install -y dnsmasq iptables network-manager
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

Step 3 (Starting up the inet server)

Code:
sudo sysctl -w net.ipv4.ip_forward=1

sudo ip addr add 192.168.4.1/24 dev eth0
sudo ip link set dev eth0 up

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

sudo gedit /etc/dnsmasq.conf

sudo systemctl restart dnsmasq

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -j ACCEPT

sudo sh -c "iptables-save > /etc/iptables.rules"


Last Step aka Client Side Commands!

Code:
sudo ifconfig eth0 192.168.4.10 netmask 255.255.255.0 up
sudo route add default gw 192.168.4.1     
          
Test the connection!

Code:
ifconfig


This is temporary and will need to be re run each reboot! (both server and client)!


Heres an updated script version that dont require client side setup!

enable.sh
Code:
#!/bin/bash
# (c) J~Net 2024
# https://jnet.forumotion.com/t2047-internet-sharing-wifi-to-lan-on-linux-easy#3195
#
# ./enable.sh

# Enable IP forwarding
echo "Setting Internet Sharing Up..."

# Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1

# Configure static IP for the LAN interface
sudo ip addr add 192.168.4.1/24 dev eth0
sudo ip link set dev eth0 up

# Backup the existing dnsmasq configuration file (optional)
# sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

# Configure dnsmasq for DHCP and DNS
sudo tee /etc/dnsmasq.conf > /dev/null <<EOL
# Listen on the LAN interface
interface=eth0

# DHCP range for clients
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

# Gateway (default route) for clients
dhcp-option=3,192.168.4.1

# DNS server for clients
dhcp-option=6,192.168.4.1

# Upstream DNS servers
server=8.8.8.8
server=8.8.4.4
EOL

# Restart dnsmasq to apply the new configuration
sudo systemctl restart dnsmasq

# Configure NAT for Internet sharing
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -j ACCEPT

# Save iptables rules for persistence
sudo sh -c "iptables-save > /etc/iptables.rules"

# Display success message
echo "Internet Sharing Enabled On LAN IP: 192.168.4.1"

disable.sh


Code:
#!/bin/bash
# (c) J~Net 2024
# https://jnet.forumotion.com/t2047-internet-sharing-wifi-to-lan-on-linux-easy#3195
#
# ./disable.sh
#
# Stop dnsmasq service
sudo systemctl stop dnsmasq

# Restore original dnsmasq configuration
if [ -f /etc/dnsmasq.conf.bak ]; then
    sudo mv /etc/dnsmasq.conf.bak /etc/dnsmasq.conf
    echo "Restored original dnsmasq configuration."
else
    echo "Backup dnsmasq configuration not found; skipping restore."
fi

# Remove static IP configuration for eth0
sudo ip addr flush dev eth0
sudo ip link set dev eth0 down

# Disable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=0
sudo sed -i '/net.ipv4.ip_forward=1/d' /etc/sysctl.conf

# Clear iptables rules
sudo iptables -t nat -F
sudo iptables -F
sudo iptables -t nat -X
sudo iptables -X

# Remove saved iptables rules if they exist
if [ -f /etc/iptables.rules ]; then
    sudo rm /etc/iptables.rules
    echo "Removed saved iptables rules."
else
    echo "No saved iptables rules found; skipping removal."
fi

# Restart dnsmasq service to apply clean configuration
sudo systemctl restart dnsmasq

echo "Internet sharing disabled and settings reverted to normal."


Last edited by jamied_uk on 17th November 2024, 09:02; edited 1 time in total
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Internet sharing WIFI to LAN on Linux Easy Empty Re: Internet sharing WIFI to LAN on Linux Easy

Post by jamied_uk 16th November 2024, 15:31

Setup Script for IS



Code:
#!/bin/bash
# (c) J~Net 2024
# https://jnet.forumotion.com/t2047-internet-sharing-wifi-to-lan-on-linux-easy#3195
#
# ./enable.sh

# Enable IP forwarding
echo "Setting Internet Sharing Up..."

# Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1

# Configure static IP for the LAN interface
sudo ip addr add 192.168.4.1/24 dev eth0
sudo ip link set dev eth0 up

# Backup the existing dnsmasq configuration file (optional)
# sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

# Configure dnsmasq for DHCP and DNS
sudo tee /etc/dnsmasq.conf > /dev/null <<EOL
# Listen on the LAN interface
interface=eth0

# DHCP range for clients
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

# Gateway (default route) for clients
dhcp-option=3,192.168.4.1

# DNS server for clients
dhcp-option=6,192.168.4.1

# Upstream DNS servers
server=8.8.8.8
server=8.8.4.4
EOL

# Restart dnsmasq to apply the new configuration
sudo systemctl restart dnsmasq

# Configure NAT for Internet sharing
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -j ACCEPT

# Save iptables rules for persistence
sudo sh -c "iptables-save > /etc/iptables.rules"

# Display success message
echo "Internet sharing enabled. LAN IP: 192.168.4.1"


No Client Side Setup Required!


Reset script to disable IS

Code:
#!/bin/bash
# (c) J~Net 2024
# https://jnet.forumotion.com/t2047-internet-sharing-wifi-to-lan-on-linux-easy#3195
#
# ./disable.sh
#
# Stop dnsmasq service
sudo systemctl stop dnsmasq

# Restore original dnsmasq configuration
if [ -f /etc/dnsmasq.conf.bak ]; then
    sudo mv /etc/dnsmasq.conf.bak /etc/dnsmasq.conf
    echo "Restored original dnsmasq configuration."
else
    echo "Backup dnsmasq configuration not found; skipping restore."
fi

# Remove static IP configuration for eth0
sudo ip addr flush dev eth0
sudo ip link set dev eth0 down

# Disable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=0
sudo sed -i '/net.ipv4.ip_forward=1/d' /etc/sysctl.conf

# Clear iptables rules
sudo iptables -t nat -F
sudo iptables -F
sudo iptables -t nat -X
sudo iptables -X

# Remove saved iptables rules if they exist
if [ -f /etc/iptables.rules ]; then
    sudo rm /etc/iptables.rules
    echo "Removed saved iptables rules."
else
    echo "No saved iptables rules found; skipping removal."
fi

# Restart dnsmasq service to apply clean configuration
sudo systemctl restart dnsmasq

echo "Internet sharing disabled and settings reverted to normal."
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum