Chowhowse.com

Home

Handle Smartphone Tethering on GNU/Linux

January 26, 2019


I own a Samsung Galaxy S7 phone. I have a contract with Verizon, and my phone is the only source of internet I have at my home. I use it both as a mobile hotspot, and as a tethered USB internet device. With the hotspot, I have never had any issues with it on a GNU/Linux system. With the tethering feature, however, I have had all sorts of problems, across several distributions. The most consistent (and troublesome) issue that I run into is that the network interface that is created inside Linux when I tether my phone will simply not be able to connect to the internet at all, even though it appears in, say, the output of 'ip link'. I mentioned in the first part of my Arch Linux Re-installation series that I had to deal with this issue during an OS install. I decided I would cover the issues I have had here, and present my workarounds.


(NOTE: You may or may not experience these same issues when trying to tether your phone to your Linux system. I have experienced them consistently, and have seen reports of the problem around the interet. As I said, I have a Verizon Samsung Galaxy S7, and I have had the problem across multiple phone OS updates, including 2 full phone OS upgrades. If your phone is different from mine, however, you may not experience these exact issues.)


For starters, I should explain what the problem actually is. Yes, it is true that I am not able to access the internet immediately when I tether my phone, but that is really only a symptom of the real issue. The real problem is that, for whatever reason, the phone reports an invalid MAC address to Linux, which causes the resulting network interface to be unusable. You can see your own tethered network device's MAC address by running the 'ip link' command:


ip link


Identify your tethered device from the list of network interfaces that is printed out. If the MAC address (the hexedecimal numbers seperated by colons immediatly following 'link/ether') is all zeroes, you are likely experiencing the same issue I was.


So what do you do about it? I have, over time, found three solutions, each appropriate for different situations. I will describe each one below.


Solution 1: 'macchanger'

The first solution is to use the program 'macchanger'. This program is a simple utility to change the MAC address of a network interface from the terminal. It is available in both the default Ubuntu and Arch Linux repositories, and can be configured to run automatically. If you don't configure it to do so, simply passing macchanger the network interface who's MAC address you want changed will cause it to do so immediately:


macchanger [INTERFACE NAME]


This will result in the network interface having a valid, random MAC address.


So what are the pros and cons of this solution? The obvious pro is the simplicity. I was successful using 'macchanger' the first time, and, as I mentioned, it can be configured to run automatically. The main con, however, is that it is a third-party program that is not included by default on any Linux distributions that I am aware of. If you don't have Internet access at all because of the tethering issue, you will not be able to install 'macchanger', so it is a moot point. If you have some other source of internet access, it could be the solution for you, however.


Solution 2: Manually Changing MAC With 'ip'

The next solution that I used (and the one that I mentioned in the Arch Linux blog post I linked at the start of this article) is to use the built-in 'ip' program to manually set the MAC address of your network interface. 'ip' is the standard GNU/Linux program for showing and manipulating network interfaces (among other things). One of it's myriad uses is to change the MAC address of a given network interface to whatever the user wants. The first step to accomplish this is to use 'ip' to bring the network interface down:


ip link set dev [INTERFACE NAME] down


Next, actually change the MAC address (you can choose any MAC address you like, but it must be 6 2-digit hexedecimal numbers seperated by colons and cannot be all 00's):


ip link set dev [INTERFACE NAME] address [VALID MAC ADDRESS]


The final step is to bring the interface back up, again with 'ip':


ip link det dev [INTERFACE NAME] up


And with that, your device should have a valid MAC address and should be able to get on the Internet!


Again, regarding the good and bad points of this solution: The good (indeed, best) aspect of using 'ip' to change the MAC address is that the 'ip' command is included by default on every Linux distro that is at least semi-recent (think, last 6 or 7 years). The worst con is that it is much more complicated than the preceding solution. This solution is the sort of thing that is best kept in your back pocket to use if no other options are available. It is not the most elegant or least complex option available, however, even on a base Linux system with no network access.


Solution 3: Configuring Systemd to Change MAC Automatically

The final solution I will discuss involves using Systemd, the great big init system that most distributions are using nowadays, to automatically run a network daemon that will change the MAC address of our tethering device's Linux network interface each time it is connected and tethering is turned on. This is actually not too complicated to do, and is the most consistent and quick method I have found (and the one that I am using on my Arch PC currently) once it is configured.


The first step is to identify the name that is given to the tethered device each time it is connected. To find this out, first plug in the phone and turn tethering on, then run the 'ip link' command and identify the interface in the output:


ip link


Remember the name that is associated with that device (it likely begins with 'enp', but it may also be something like 'eth1'). The next step is to create a file in your systemd network directory (on Arch, '/etc/systemd/network') that is named in the format '[NAME].network', where 'NAME' is the name associated with your tethered device's Linux network interface (so, if your interface is named 'en01', you would name the file 'en01.network').


Open this file with the text editor of your choice and add the following lines, replacing the items in curly braces with your information:


[Match]

Name={INTERFACE NAME}

[Link]

MACAddress={VALID MAC ADDRESS}

[Network]

DHCP=yes


Save and close the file, then restart your system. The next time you plug in and tether your phone, it should get as its MAC address the address you specified in the above .network file, and work automatically!


The best aspect of this solution is that it offers a permanent fix for the MAC problem, and once it is up and running, it can simply be forgotten about. Plus, it works on any system that has Systemd, which most do by default. The bad part is that it involves messing with system files, which might be unnerving for new users. However, this is Linux, and you won't get the most you can out of it without getting your hands dirty!


I hope that some of the above information was useful to the reader. I would have loved to have found a guide like this a year or so ago when I first encountered this problem. If anyone has any questions about this article, email me at 'erik (at) chowhowse (dot) com' and I will do my best to render assistance. Thanks for reading!