USB Internet: Difference between revisions
m Reverted edits by Doomfan345 (talk) to last revision by WonderfulShrineMaidenOfParadise Tag: Rollback |
WhyNotHugo (talk | contribs) Clarify which host we're talking about |
||
(11 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
A phone running postmarketOS can connect to the Internet through a USB cable plugged onto another Internet-connected device. For this to work you set your host machine as gateway for your phone and let your host machine do {{wikipedia|Network_address_translation|network address translation (NAT)}} for the phone or a guest machine. | |||
== On your phone or guest machine == | |||
{{note|'''Note:''' <code>1.1.1.1</code> and <code>1.0.0.1</code> are the DNS servers, feel free to change it if you wish.</code>}} | {{note|'''Note:''' <code>1.1.1.1</code> and <code>1.0.0.1</code> are the DNS servers, feel free to change it if you wish.</code>}} | ||
{{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 <code>172.16.42.1</code>.}} | {{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 <code>172.16.42.1</code>.}} | ||
These instructions assume the relevant interface is named <code>usb0</code>. On some devices (those running [android-based] downstream kernels?), it may be <code>rndis0</code>. Check first with e.g., <code>ifconfig</code>, and use the interface that has the line <code>inet addr:172.16.42.1</code>. | |||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
# ip route add default via 172.16.42.2 dev usb0 | # ip route add default via 172.16.42.2 dev usb0 | ||
Line 23: | Line 21: | ||
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) | 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) | ||
==== | == On the host machine == | ||
{{note|This section assumes the host machine is running Linux; for Windows, see [[Windows FAQ#Configuring USB Internet with a Windows host]].}} | |||
First, enable IP forwarding: | First, enable IP forwarding: | ||
Line 36: | Line 31: | ||
Then follow the instructions according to the distribution or firewall you use. | Then follow the instructions according to the distribution or firewall you use. | ||
=== iptables (Ubuntu/Arch/Alpine) === | |||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 48: | Line 43: | ||
and your normal network on the host machine. | and your normal network on the host machine. | ||
=== firewalld (RHEL/CentOS/Fedora/Rocky/SLE/openSUSE) === | |||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 62: | Line 57: | ||
Use the <code>--permanent</code> and <code>--reload</code> options for a persistent configuration.<ref>https://www.server-world.info/en/note?os=CentOS_7&p=firewalld&f=2</ref> | Use the <code>--permanent</code> and <code>--reload</code> options for a persistent configuration.<ref>https://www.server-world.info/en/note?os=CentOS_7&p=firewalld&f=2</ref> | ||
=== nftables (postmarketOS) === | |||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 70: | Line 65: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
postmarketOS has chain <code>forward</code> configured for USB interfaces by default.<ref>https://gitlab. | postmarketOS has chain <code>forward</code> configured for USB interfaces by default.<ref>https://gitlab.postmarketos.org/postmarketOS/pmaports/-/blob/master/main/postmarketos-config-nftables/rules/51_usb_inet.nft</ref> | ||
If using other distributions or the following lines are missing, add them manually. | If using other distributions or the following lines are missing, add them manually. | ||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 79: | Line 74: | ||
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 <code>[[Kernel_configuration#CONFIG_USB_ETH|CONFIG_USB_ETH]]</code> setting enabled in menuconfig. | 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 <code>[[Kernel_configuration#CONFIG_USB_ETH|CONFIG_USB_ETH]]</code> setting enabled in menuconfig. | ||
=== ufw === | |||
In the file <code>/etc/default/ufw</code>, change <code>DEFAULT_FORWARD_POLICY</code> to <code>ACCEPT</code>. | In the file <code>/etc/default/ufw</code>, change <code>DEFAULT_FORWARD_POLICY</code> to <code>ACCEPT</code>. | ||
Line 110: | Line 105: | ||
# ufw disable && ufw enable | # ufw disable && ufw enable | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== See also == | |||
* [[USB Tethering]] | |||
== Notes == | == Notes == |
Latest revision as of 17:59, 15 February 2025
A phone running postmarketOS can connect to the Internet through a USB cable plugged onto another Internet-connected 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: 1.1.1.1 and 1.0.0.1 are the DNS servers, feel free to change it if you wish. |
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 172.16.42.1 . |
These instructions assume the relevant interface is named usb0
. On some devices (those running [android-based] downstream kernels?), it may be rndis0
. Check first with e.g., ifconfig
, and use the interface that has the line inet addr:172.16.42.1
.
# ip route add default via 172.16.42.2 dev usb0
# echo nameserver 1.1.1.1 > /etc/resolv.conf
If neither of those work or run correctly, try route add default gw 172.16.42.2
To make the configuration persistent after reboot do:
# echo 'ip route add default via 172.16.42.2 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)
On the host machine
This section assumes the host machine is running Linux; for Windows, see Windows FAQ#Configuring USB Internet with a Windows host. |
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 172.16.42.0/24 -j ACCEPT
# iptables -A POSTROUTING -t nat -j MASQUERADE -s 172.16.42.0/24
# 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 connection.zone external
# nmcli connection modify ethX_to_phone connection.zone 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.
ufw
In the file /etc/default/ufw
, change DEFAULT_FORWARD_POLICY
to ACCEPT
.
DEFAULT_FORWARD_POLICY="ACCEPT"
In the file /etc/ufw/sysctl.conf
, uncomment net/ipv4/ip_forward=1
.
net/ipv4/ip_forward=1
#net/ipv6/conf/default/forwarding=1
#net/ipv6/conf/all/forwarding=1
Add the following rule to /etc/ufw/before.rules
BEFORE the *filter
table rules.
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Forward traffic for 172.16.42.0/24
-A POSTROUTING -j MASQUERADE -s 172.16.42.0/24
# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT
Restart ufw
to make changes take effect.
# ufw disable && ufw enable