Linux usb share internet

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 (NAT) for the phone or a guest machine.

Contents

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.
Linux
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 .
# 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)

Windows

Windows usb internet.png

Configure the IP addresses like the screenshot above. [1]

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 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. [2]

Читайте также:  Куосера 1040 драйвер линукс
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. [3] 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.

Notes

Источник

How to enable internet sharing via USB Ubuntu 20.04 to android 6.0.1 and 10

Internet connection on Ubuntu is by Ethernet cable.
I want to connect two devices and share internet from Ubuntu to Samsung Galaxy Note Edge [SM-N915FY; android: 6.0.1] and Samsung Galaxy Note 9 [SM-N960F; android 10] by USB cable. I have read all previous topics about sharing internet, tired all but I have failed to share data. In Connections — System Settings Module -> tab IPv4 changed method to Shared to other computers, but still not working. Of course I’ve accepted permission to access by PC on phone.
There is no reverse tethering option on both Samsung Notes. Also tired to enable USB debugging.

It is probably quite a bit easier to check your router for DHCP options, and setting it up to provide «Internet» to the phones via wlan.

as i mentioned before, i want to share internet via USB, not wireless. I know how to share internet wireless, but not via USB.

Right, now the big question is; is there a driver that works for Ubuntu or any other linux? Please be more specific.

2 Answers 2

Pretty old question but there is a simple way, thanks to an amazing tool

I have a way but a little bit tricky. You are going to need:

  • Android with the «USB-PC internet share» option (not the classic USB tethering)
  • iptables or nftables (for masquerade/SNAT)
  • tcpdump
  • iproute2
  • a dns server on your linux (dnsmasq is quick to set up)

1/ On you android device: got to Settings >> Network connection >> Tethering and portable hotspot >> USB-PC internet share . Say ok or choose any OS suggested (usually different Windows versions), it does not matter as long as the functionnality is enabled. => now your smartphone is going to immediately try to connect to internet, but your Linux system is not yet ready. You will need to retrieve the folling information: the subnet and the default gateway you smartphone is going to use. This can be done with tcpdump

2/ On your Linux: use tcpdump to capture traffic from the USB network interface (enabled when USB PC internet share is activated from android). You are going to see a lot of whohas XXXX. This ip address is the default gateway you android is trying to reach. add this ip address and the correct subnetmask to the tethering interface (sudo ip address add XXXXX dev XXXX) or use network manager

3/ enable masquerading with iptables:

sudo iptables -A POSTROUTING -o usb_thethering_interface -j MASQUERADE 

4/ make sure you dns server is working on the Linux.

Источник

How do I share an internet connection through USB?

I have two Linux boxes. One is running KNOPPIX and the other Ubuntu. I have only one wifi dongle between them, and only one of them has an Ethernet port. They both have free USB ports however. I need the box with the dongle to share the connection through a male-to-male USB cable. I know it is possible to do a similar setup with a desktop and certain portable devices, but I need it between two normal computers. I cannot buy any additional hardware.

USB doesn’t work that way and can’t be used to network 2 computers directly. Your best bet is to find an USB/Ethernet adapter, or another Wi-Fi dongle.

I am already doing this on a window System.Sharing Internet of PC via USB to a my mobile. now m looking for a way to do the same on my linux box.

@Gagan Your smartphone can act as both a host and device (USB OTG), your laptop doesn’t. It’s easier to insert an Ethernet cable in between.

3 Answers 3

Without any deeper knowledge, I would suggest looking at the Linux USB Project, section USB Host-to-Host Cables, and possibly Easy Transfer Cable (although that seems to be mainly a Windows thingy). In any case you are likely to need additional hardware, because the cable probably is not «just wires».

This is impossible, unfortunately. All USB connections must be between a host device (e.g. a computer) and a normal USB device (mouse, etc.). Desktops and laptops are always hosts and therefore cannot connect to each other. Some newer mobile devices implement the USB On-The-Go standard, and can switch between being hosts and normal devices, but this won’t be the case for your computers.

There is no such thing as a USB male-to-male cable, or at least, if some idiot made one it would explicitly violate the USB standard. The closest thing to it is something like a laplink cable, which is a USB device in the middle that has two male cables coming out of it. As far as I know, these use proprietary software to communicate with the device and ask it to relay to the other computer. As such, it won’t work as a normal network interface in Linux.

Well, I have a male-to-male cable. It came with a cheap digital camera stick thing, which had a full size USB port to connect to the computer.

Источник

Оцените статью
Adblock
detector