USB Network

Out of the box, a postmarketOS device uses a static address of 172.16.42.1/24 on any USB network interface and runs the unudhcpd DHCP server, offering one address, 172.16.42.2/24, to the USB network. After plugging a cable between your device and a PC, from the PC you might be able to SSH to the phone with:
$ ssh 172.16.42.1
This article provides details in case this does not work out of the box (no IP assigned, doing a new port, etc.).
= Connecting =
Power on your device and connect it to your USB port. A new network interface should appear, run ip a to list all of them. The new entry should look something like:
$ ip a
1: enp0s19f2u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
 link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
 inet 172.16.42.2/24 brd 172.16.42.255 scope global dynamic noprefixroute enp0s19f2u1
 valid_lft 861920sec preferred_lft 861920sec
 inet6 fe80::aabb:ccdd:eeff:00aa/64 scope link noprefixroute
 valid_lft forever preferred_lft forever
If you instead get something like -
$ ip a
5: enp4s0u1u1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
then you need to give the interface a proper MAC address to be able to get an IP address, run sudo ip link set dev enp4s0u1u1 address 12:12:12:12:12:12. Make sure you specify the right interface name.
You should now have been assigned the IP address 172.16.42.2 (verify with ip a or ifconfig). If it still does not work automatically, you might need to run a DHCP client on your PC (dhclient or dhcpcd).
== SSH ==
When you are past the initramfs, you should be able to connect via ssh and the username and password you set during the install phase:
$ ssh user@172.16.42.1
user@172.16.42.1's password: 
Welcome to postmarketOS!
This distribution is based on Alpine Linux.
Read both our wikis to find a large amount of how-to guides and
general information about administrating and development.
See <https://wiki.postmarketos.org> and <https://wiki.alpinelinux.org>.
You may change this message by editing /etc/motd.
localhost:~$
Use sudo to run commands as root, it is configured.
=== Prevent host key verification clashes when connecting to different devices ===
If you have multiple devices you want to SSH into, you will get issues with host key verification as your system will realize it's talking to different devices on the same IP address. To prevent this, disable host key verification entirely for the IP address used by postmarketOS devices when connected by USB.
Add the following to ~/.ssh/config:
Host pmos
 HostName 172.16.42.1
 UserKnownHostsFile /dev/null
 StrictHostKeyChecking no
Afterwards you can connect to your device by running ssh <username>@pmos. Replace <username> with the proper username on your device.
== Share device TCP/IP ports through the PC ==
To share ports from the postmarketOS device through a computer, do this:
$ socat tcp-listen:[OUTWARD PORT],reuseaddr,fork tcp:[DEVICE IP ADDRESS]:[INWARD DEVICE PORT]
For SSH, [INWARD DEVICE PORT] is 22.
[OUTWARD PORT] can be 90XX when connected to adb (Android Debug Bridge tool) to open the socket port using Android with connection forwarding - adb reverse (or) adb forward - with tcp:[PORT] tcp:[PORT].
== Add IP addresses to the Alpine Linux device's network interfaces ==
To use networks from other computers, editing /etc/network/interfaces can be needed.
To put IP information into that, you can do:
$ echo $'echo -e \'[line of file]\' >> /etc/network/interfaces' | sudo "$SHELL" -s
and use the page Configure Networking - Alpine Linux (IPv4 DHCP Configuration).
= Troubleshooting =
== Try IPv4 only ==
Especially while mainlining, getting an IP often only works with the following command:
$ dhcpcd interface --nohook ipv6
== Making sure the USB is detected ==
In case you don't get an IP address directly, make sure that your PC recognizes the phone after it booted into postmarketOS at all. It should show the following in dmesg.
[ 6284.442088] usb 1-1: new high-speed USB device number 36 using xhci_hcd
[ 6284.627106] usb 1-1: New USB device found, idVendor=18d1, idProduct=d001
[ 6284.627123] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6284.627134] usb 1-1: Product: Android
[ 6284.627143] usb 1-1: Manufacturer: Android
[ 6284.627151] usb 1-1: SerialNumber: 0123456789ABCDEF
[ 6284.632322] rndis_host 1-1:1.0 usb0: register 'rndis_host' at usb-0000:00:14.0-1, RNDIS device, b2:c4:1e:03:64:b9
[ 6285.239048] rndis_host 1-1:1.0 enp0s20f0u1: renamed from usb0
[ 6285.287058] IPv6: ADDRCONF(NETDEV_UP): enp0s20f0u1: link is not ready
It should also show up on lsusb (the name doesn't necessarily match the model):
Bus 001 Device 023: ID 18d1:d001 Google Inc. Nexus 4 (fastboot)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
And ip link should show an additional entry as well (the last one, in the following list):
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
 link/ether e2:88:85:58:0f:99 brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
 link/ether e2:08:85:58:0f:99 brd ff:ff:ff:ff:ff:ff
14: enp0s29f7u3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen 1000
 link/ether 86:11:85:5e:df:fa brd ff:ff:ff:ff:ff:ff
If that's the case and you can't connect, your computer probably just doesn't have an IP address on its side of the link.
If however, nothing shows up (even when Inspecting the initramfs), the most likely reason is a wrong Kernel_configuration. This is highly device-specific, but some hints can be found in #Kernel configuration parameters
== No IP assigned or MAC address is 00:00:00:00:00:00 ==
If USB gets detected, then you probably don't have a network manager that automatically requests IP addresses from new ethernet interfaces, so you'll have to set the interface up manually.
If there's no MAC address assigned(00:00:00:00:00:00), you need to assign one.
# ip link set dev enp0s20f0u1 address 12:12:12:12:12:12
Use the following command to start a DHCP client. Replace the interface name with the one on your machine (see ip a before and after connecting the device via USB). Some Linux distributions have dhcpcd installed instead of dhclient.
$ dhclient -v enp0s20f0u1
Some distribution use other tools that might interfere with dhclient and dhcpcd. In most cases, you should be able to set an IP address with
# ip address add dev enp0s20f0u1 172.16.42.2/24
If after assigning an IP address and ip a shows your device status as "DOWN" instead of "UNKNOWN", bring it up with 
# ip link set enp0s20f0u1 up
P.S. If your device is Samsung with old kernel and CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE, something like usb_gadget-set-random-rndis-host-MAC-address.patch can help with preventing zero MAC.
After the interface is brought up you can SSH to the phone at the phone's ip address:
# ssh user@172.16.42.1
username here is dependent on what you chose in the pmbootstrap init step
== Kernel configuration parameters ==
If you think that the kernel configuration may be at fault, first double check with pmbootstrap kconfig check that the required parameters are all set.
The postmarketOS initramfs is able to configure two different USB systems in the kernel in order to let your PC talk to your phone via networking. One of them has to be enabled (exclusively?):
* CONFIG_USB_ETH
** CONFIG_USB_ETH_RNDIS is usually needed as well, it enables a compatibility interface for microsoft hosts.
** Usually used in mainline kernels (see also: Mainlining FAQ)
* CONFIG_USB_G_ANDROID
** This option is usually enabled in Android kernel forks
You can use grep on your kernel config to check which of these options are enabled or not (option=y means it is enabled). If you want to change the options, don't modify the file directly, but use pmbootstrap kconfig edit (usage instructions). Otherwise the dependencies in that configuration file will not get resolved properly and cause other issues.
Related: linux-postmarketos-qcom: Remove composite usb gadget for networking
== SSH issues ==
If SSH doesn't work for one reason or another, you can try enabling telnetd on your installation. For that, add the debug-shell initfs hook and connect to it. Then run the following commands to mount your root partition (writable):
source /etc/deviceinfo
source init_functions.sh
mount_root_partition
mount -o rw,remount /sysroot
Put the following content into /sysroot/etc/init.d/telnetd:
#!/sbin/openrc-run
command="/usr/sbin/telnetd"
command_args="-F"
pidfile="/run/${RC_SVCNAME}.pid"
command_background="yes"
depend() {
 need net localmount
 after firewall
}
Then make the file executable and enable the service so telnet gets launched by openrc:
chmod +x /sysroot/etc/init.d/telnetd && chroot /sysroot rc-update add telnetd default
Then resume boot by typing pmos_continue_boot. Telnetd should be started once the device boots and you should be able to connect to it.
=== Using Dropbear ===
Alternatively you can try to use dropbear SSH server instead of default openssh. For this you can avoid messing with debug-shell hook and modifying system on running phone, you can tweak rootfs before flashing it in this sequence:
 1. pmbootstrap init
 2. pmbootstrap install
 3. pmbootstrap chroot -r # go inside you phone's future rootfs
 4. # make modifications to rootfs
 # in this case, to workaround an SSH issue:
 apk add dropbear
 rc-update del sshd default
 rc-update add dropbear default
 5. exit
 6. pmbootstrap install # again, to recreate image
 # flash images as usual
 7. pmbootstrap flasher flash_kernel
 8. pmbootstrap flasher flash_rootfs
== Note about HTC devices ==
* HTC devices seem to be tricky in general, we have a few open issues with HTC devices that seem to be not booting
* The HTC desire does work, and it required this line in the initfs-hook to get USB network
* It was hard to discover: drebrez used the tuning sysfs instructions, but he had to write it as a initfs hook and save it in the cache partition of the device, then reboot in TWRP to get that file.
* Without a working display, no USB network and no serial output, you can use the LEDs for debugging and the other partitions to store files.
== Windows driver ==
See Windows_FAQ#USB_networking.
= See also =
* USB Internet: Let your phone access the Internet by sharing your PC's Internet connection via USB.
* Osk-sdl#Troubleshooting
* Troubleshooting:boot
Category:Guide