USB Internet

From postmarketOS

You can enable Internet through the USB cable when WiFi doesn't work yet on your device. For this to work you set your host machine as gateway for your phone and let your host machine do network address translation (NAT) for the phone or a guest machine.

On your phone or guest machine

Note Note: and are the DNS servers, feel free to change it if you wish.


Note Note: It works in both sides: to share Internet from your phone to PC (aka USB tethering), just swap host and phone below and use
# ip route add default via dev usb0
# echo nameserver > /etc/resolv.conf

If neither of those work or run correctly, try route add default gw

To make the configuration persistent after reboot do:

# echo 'ip route add default via dev usb0' > /etc/local.d/usb_internet.start
# chmod +x /etc/local.d/usb_internet.start
# rc-update add local

Disable it if you plug the USB cable out and want to use WiFi, because it can interfere with routing (packets would try to use the USB networking, even though it's disconnected)


See Windows FAQ and reconsider your choice of operating system.

On the host machine

First, enable IP forwarding:

# sysctl net.ipv4.ip_forward=1

Then follow the instructions according to the distribution or firewall you use.

iptables (Ubuntu/Arch/Alpine)

# iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# iptables -A FORWARD -s -j ACCEPT
# iptables -A POSTROUTING -t nat -j MASQUERADE -s
# iptables-save #Save changes

This will enable IPv4 forwarding on your host machine which basically lets it function as a router. The postrouting command will make the kernel on the host translate the packets between the phone network and your normal network on the host machine.

firewalld (RHEL/CentOS/Fedora/Rocky/SLE/openSUSE)

# firewall-cmd --get-active-zone 
# nmcli connection modify ethX_other external
# nmcli connection modify ethX_to_phone internal
# firewall-cmd --get-active-zone 
# firewall-cmd --zone=external --add-masquerade
# firewall-cmd --zone=external --list-all
# firewall-cmd --zone=internal --list-all

Use the --permanent and --reload options for a persistent configuration.[1]

nftables (postmarketOS)

# nft add table inet nat
# nft 'add chain inet nat postrouting { type nat hook postrouting priority 100 ; }'
# nft add rule inet nat postrouting iifname "usb*" masquerade

postmarketOS has chain forward configured for USB interfaces by default.[2] If using other distributions or the following lines are missing, add them manually.

# nft add rule inet filter forward ct state { established, related } accept
# nft add rule inet filter forward iifname "usb*" accept

You can now verify the Internet connection on your phone by pinging something. If this does not work it means you need to compile the kernel with CONFIG_USB_ETH setting enabled in menuconfig.


In the file /etc/default/ufw, change DEFAULT_FORWARD_POLICY to ACCEPT.


In the file /etc/ufw/sysctl.conf, uncomment net/ipv4/ip_forward=1.


Add the following rule to /etc/ufw/before.rules BEFORE the *filter table rules.

# NAT table rules

# Forward traffic for

# don't delete the 'COMMIT' line or these rules won't be processed

Restart ufw to make changes take effect.

# ufw disable && ufw enable

See also