Difference between revisions of "USB Internet"

From postmarketOS
Jump to: navigation, search
(Added syntax highlighting)
(Ubuntu)
 
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
USB Internet
+
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 for the phone.
Enable Internet on your device by using a usb cable (when wifi does not work).
 
  
On your phone:
+
==== On your phone: ====
  
X are your pc usb ip address last numbers you will find in your pc's network manager.
 
  
 
<syntaxhighlight lang="shell-session">
 
<syntaxhighlight lang="shell-session">
$ sudo route add default gw 172.16.42.x
+
$ sudo route add default gw 172.16.42.2
$ sudo nano /etc/resolv.conf
+
$ echo nameserver 80.80.80.80 | sudo tee /etc/resolv.conf
 +
Substitute 80.80.80.80 for your own internal DNS server if you want.
 +
</syntaxhighlight>
 +
To make the configuration persistent after reboot do:
 +
<syntaxhighlight lang="shell-session">
 +
$ echo 'route add default gw 172.16.42.2' | sudo tee /etc/local.d/usb_internet.start
 +
$ sudo chmod +x /etc/local.d/usb_internet.start
 +
$ sudo 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)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Delete and write instead your router ip address (nameserver 192.168.1.1) or dns server address (nameserver 8.8.8.8)
+
==== On the host machine: ====
  
On your pc:
+
===== Ubuntu/ Arch =====
  
 
<syntaxhighlight lang="shell-session">
 
<syntaxhighlight lang="shell-session">
$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward > /dev/null
+
$ sudo sysctl net.ipv4.ip_forward=1
 
$ sudo iptables -P FORWARD ACCEPT
 
$ sudo iptables -P FORWARD ACCEPT
 
$ sudo iptables -A POSTROUTING -t nat -j MASQUERADE -s 172.16.42.0/24
 
$ sudo iptables -A POSTROUTING -t nat -j MASQUERADE -s 172.16.42.0/24
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You are done! Now try to ping someone!
+
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.
 +
 
 +
===== RHEL/CentOS/Fedora =====
 +
 
 +
<syntaxhighlight lang="shell-session">
 +
$ sudo sysctl net.ipv4.ip_forward=1
 +
$ sudo firewall-cmd --get-active-zone
 +
$ sudo nmcli connection modify ethX_other connection.zone external
 +
$ sudo nmcli connection modify ethX_to_phone connection.zone internal
 +
$ sudo firewall-cmd --get-active-zone
 +
$ sudo firewall-cmd --zone=external --add-masquerade
 +
$ sudo firewall-cmd --zone=external --list-all
 +
$ sudo firewall-cmd --zone=internal --list-all
 +
</syntaxhighlight>
 +
 
 +
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>
 +
 
 +
 
 +
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 USB_ETH setting enabled in menuconfig.
 +
 
 +
== Notes ==
 +
<References />
 +
 
 +
[[Category:Guide]]

Latest revision as of 10:37, 19 August 2019

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 for the phone.

On your phone:

$ sudo route add default gw 172.16.42.2
$ echo nameserver 80.80.80.80 | sudo tee /etc/resolv.conf
Substitute 80.80.80.80 for your own internal DNS server if you want.

To make the configuration persistent after reboot do:

$ echo 'route add default gw 172.16.42.2' | sudo tee /etc/local.d/usb_internet.start
$ sudo chmod +x /etc/local.d/usb_internet.start
$ sudo 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:

Ubuntu/ Arch
$ sudo sysctl net.ipv4.ip_forward=1
$ sudo iptables -P FORWARD ACCEPT
$ sudo iptables -A POSTROUTING -t nat -j MASQUERADE -s 172.16.42.0/24

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.

RHEL/CentOS/Fedora
$ sudo sysctl net.ipv4.ip_forward=1
$ sudo firewall-cmd --get-active-zone 
$ sudo nmcli connection modify ethX_other connection.zone external
$ sudo nmcli connection modify ethX_to_phone connection.zone internal
$ sudo firewall-cmd --get-active-zone 
$ sudo firewall-cmd --zone=external --add-masquerade
$ sudo firewall-cmd --zone=external --list-all
$ sudo firewall-cmd --zone=internal --list-all

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


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 USB_ETH setting enabled in menuconfig.

Notes