How To Set Up a PC or Linux Nix Computer as a Router / Bridge, Different OS's
See also: /iptables.
Ubuntu 24.04 (nftables) #
1. enable ipv4 forwarding #
- permanent/persistent
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf
sudo sysctl --system
- temp
sudo sysctl -w net.ipv4.ip_forward=1
## or
#echo 1 > /proc/sys/net/ipv4/ip_forward
2. find the outbound interface #
WAN_IF=$(ip route get 1.1.1.1 | awk '{for (i=1; i<=NF; i++) if ($i=="dev") {print $(i+1); exit}}')
echo "$WAN_IF"
such as ens3 or eth0
3. use nat and allow return traffic #
sudo nft add table ip nat 2>/dev/null || true
sudo nft 'add chain ip nat postrouting { type nat hook postrouting priority 100 ; }' 2>/dev/null || true
sudo nft add rule ip nat postrouting oifname "$WAN_IF" masquerade 2>/dev/null || true
sudo nft add table inet filter 2>/dev/null || true
sudo nft 'add chain inet filter forward { type filter hook forward priority 0 ; policy drop ; }' 2>/dev/null || true
sudo nft add rule inet filter forward oifname "$WAN_IF" accept 2>/dev/null || true
sudo nft add rule inet filter forward iifname "$WAN_IF" ct state related,established accept 2>/dev/null || true
ufw and iptables also work, but not 24.04 native anymore.
Ubuntu 10.04~16.04 NAT #
NAT is mandatory for a router to route traffic between its clients and upper internet.
# /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p
and:
# vim /etc/rc.local
iptables --table nat --append POSTROUTING --out-interface <outer-WAN>0 -j MASQUERADE
iptables --append FORWARD --in-interface <eth-inner-lan>1 -j ACCEPT
(here: for improved security) restart networking:
service networking restart
We may want to and setup DHCP server, see here.
Ubuntu 10.04 Bridge #
firstly: sudo apt-get install bridge-utils
type 1: named / ip-ed bridge #
# vim /etc/rc.conf
auto br0
iface br0 inet static
address 192.168.1.2
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
bridge_ports all
type 2: anonymous bridge (bridge without ip) #
# vim /etc/rc.conf
auto br1
iface br1 inet manual
bridge_ports eth1 eth2
#bridge_ports all
bridge_maxwait 0
Openwrt NAT #
Similar to Ubuntu 10.04 NAT, but inserting (instead of appending) iptables rules due to the already existing rules.
sysctl -w net.ipv4.ip_forward=1 # or: echo 1 > /proc/sys/net/ipv4/ip_forward
## confirm this is enabled by default in the file: /etc/sysctl.conf
iptables --table nat --insert POSTROUTING 1 --out-interface eth0 -j MASQUERADE
iptables --insert FORWARD 1 --in-interface wlan0 -j ACCEPT
# iptables rules can be written to file: /etc/rc.local
Maybe it can be done in a better way using wrt zone settings. Already spend a lot of time, will try next chance.
Ubuntu Change NIC (network interface card) name #
vim /etc/udev/rules.d/70-persistent-net.rules
ref
FreeBsd 7 Local Router (Like a Switch ???) #
(local only, no NAT)
vim /etc/rc.conf
ifconfig_em0="inet 10.0.0.12 netmask 255.255.255.0"
ifconfig_em1="inet 10.0.1.13 netmask 255.255.255.0"
notice: will get route problem if using the same net-addr.
- permanent
# vim /etc/sysctl.conf
net.inet.ip.forwarding=1
# reboot
- temp
sysctl net.inet.ip.forwarding=1
# or
echo 1 > /proc/sys/net/ipv4/ip_forward
// problem: ping test: could get reply since icmp packet 113. where are the first 112 packets???
FreeBsd 7 Bridge #
ref: shaper freeBsd-v7 : bridge
# vim /etc/rc.conf
ifconfig_bge0="inet 192.168.1.176 netmask 255.255.255.0"
cloned_interfaces="bridge0"
ifconfig_bridge0="addm em0 addm em1 up"
ifconfig_em0="up"
ifconfig_em1="up"
defaultrouter="192.168.1.168"
nameserver 8.8.8.8
firewall_enable="YES"
firewall_type="open"
- /etc/sysctl.conf
net.inet.ip.forwarding=1
or, using cmd : sysctl net.inet.ip.forwarding=1;
Windows 7 #
netsh wlan set hostednetwork mode=allow ssid="<ssid_name>" key=<passwd_plain_text>
system echo:
The hosted network mode has been set to allow. The SSID of the hosted network has been successfully changed. The user key passphrase of the hosted network has been successfully changed.
netsh wlan start hostednetwork
system echo:
The hosted network started.
After sleep/hibernate, we need to start it again by netsh wlan start hostednetwork.
ref: About the Wireless Hosted Network (Windows) - MSDN - Microsoft How to set up Virtual Wifi in Windows 7
//bit.ly/29q6Bia) How to set up Virtual Wifi in Windows 7