--- title: Share USB tethered phone to Raspberry Pi 4 LAN port tags: Raspberry Pi --- # Share USB tethered phone to Raspberry Pi 4 LAN port ## Raspberry Pi Setup Follow the instructions on Raspberry Pi: Take into consideration that we imagined the phone virtual interface of the Raspberry Pi is `usb0`. 1. Set a static IP address to the eth0 which would be router's clients' default gateway. To do it, there is plenty of procedure: Put lines below to in `/etc/dhcpcd.conf`: ```sh interface eth0 static ip_address=192.168.100.254/24 ``` 2. Install the DHCP server package: ```bash sudo apt-get -y install dnsmasq ``` 3. Configure the DHCP server. At first, create a backup of the config file: ```bash sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak ``` Put lines below to `/etc/dnsmasq.conf`: ```sh interface=eth0 listen-address=192.168.100.254 # bind-interfaces server=8.8.8.8 domain-needed bogus-priv dhcp-range=192.168.100.10,192.168.100.100,24h ``` 4. Enable IP Forwarding to forward router's clientsi packets: In `/etc/sysctl.conf`, uncomment this line: ```sh net.ipv4.ip_forward=1 ``` 5. Follow these commands for the firewall to allow forwarding between interfaces: ```bash sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT sh -c "iptables-save > /etc/iptables.ipv4.nat" ``` 6. Open `rc.local` to add command for restoring firewall configuration on each RPi startup: In `/etc/rc.local`, add this line before Exit 0: ```sh iptables-restore < /etc/iptables.ipv4.nat ``` 7. Make sure that `usb0` is on DHCP mode to get ip address from the phone. Now, you have configured Raspberry Pi as a bridge for the router to the phone. ## Router configuration In this step, you must disable any static routes and disable the DHCP server of the router because the DHCP requests of the clients must forward to the RPi interface. ## Reference - [Share USB tethered phone to Raspberry Pi 4 LAN port](https://raspberrypi.stackexchange.com/questions/106871/share-usb-tethered-phone-to-raspberry-pi-4-lan-port) - [Dnsmasq fail to start at boot on Raspberry Pi 3 with Stretch OS](https://raspberrypi.stackexchange.com/questions/72487/dnsmasq-fail-to-start-at-boot-on-raspberry-pi-3-with-stretch-os) - [iptables 的安裝與設定](http://120.105.184.250/cswang/thit/Linux/iptables.htm)