본문 바로가기

Linux/Ubuntu

USB 테터링으로 네트워크 인터페이스를 할당 받을 경우 mac address 고정하기

우분투에서 USB 테더링(mobile phone : android)을 이용하여 network interface를 할당 할 경우

매번 연결할 때마다 mac address가 변경되어 고정 ip로 할당 하기가 애매해 진다.

매번 ifconfig를 이용해서 해당 mac을 지정해주기도 번거롭다.


이에 udev를 이용해서 매번 연결될 때마다 해당 rule를 지정하여 고정되도록 지정할 수 있다.


다음과 같이 할 수 있다.


[출처]: http://www.monblocnotes.com/node/1895

How to assign a fixed IP address to an Ethernet over USB network interface

Tux27-Jan-2016 - update: see at the end of the article. 

I'm currently developing a system where an Android smartphone, used as a (sophisticated Smile) sensor, is connected to an embedded PC via a USB cable. The PC runs Linux (Mint 14). I use USB tethering to transfer information from the smartphone to the PC and from the PC to the smartphone. The problem is that every time USB tethering is activated, a new IP address (in the subnetwork 192.168.42.0/24) is assigned to the usb0 network interface created on the PC. This means that the application code running on the smartphone can't easily initiate a communication with the PC.

I tried first to prevent Network Manager from running. But this was not the right solution, as I need to use some other network interfaces (a fixed one, and a 4G network one), and I didn't want to deeply modify the PC system configuration. Then, looking at Network Manager configuration file, I discovered that it was possible to request it not to manage some network interfaces. But here, a second problem appeared: Network Manager needs a MAC address, for this option. And the rndis_host driver handling USB tethering uses a new MAC address for every activation...

So, I had to find a way to tell this driver to use a fixed MAC address. I didn't find how to implement that exact solution, but, thanks to this article, I was able to set up a working configuration. The idea is to use udev so that as soon as the device is activated, associated network interface is brought down, and configured with the right MAC address, before being brought up again.

Here are the details:

  • connect the smartphone to the PC with the USB cable
  • on the smartphone, activate USB tethering, using the Settings application
  • on the PC, enter the dmesg command. A message similar to this one should be displayed:
rndis_host 2-1.2:1.0: >usb0: register 'rndis_host' at usb-0000:00:1d.0-1.2, RNDIS device, 96:53:6d:9b:5e:a5
  • still on the PC, enter the following command (adapt usb0 if required) 

아래 명령어를 통해서 usb0에 연결된 network interface의 정보를 얻을수 있다.
이때, ATTR{serial}을 찾아야 하는데, 연결된 mobile device의 이름으로 확인할 수 있다.
내 핸드폰의 경우는 다음과 같이 출력되었다.
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{product}=="SAMSUNG_ANDROID"
ATTRS{serial}=="03175273df394"

여기서 사용할 것은 ATTRS{serial}의 값인 "03175273df394"이다. 
밑에서 언급한 rule에 기입할 정보이기도 하다.


udevadm info -a -p /sys/class/net/usb0
  • you will get udev information for usb0 device, and then for its parents. Look for the device with ATTRS{serial} information. 
  • create the file /etc/udev/rules.d/90-local.rules with the following content (on the same line, xxxxbeing the value displayed for ATTRS{serial}), setting ownership to root:root:
$ sudo vi /etc/udev/rules.d/90-local.rules 
파일을 만들고 아래의 내용을 추가한다.
이때, ATTRS{serial}=="xxxx"를 ATTRS{serial}=="03175273df394"로 설정하면 된다.
rule를 작성하고 해당 파일의 권한을 777로 한다.
$ sudo chmod 777 /etc/udev/rules.d/90-local.rules 
ACTION=="add", DRIVERS=="usb", ATTRS{serial}=="xxxx", RUN+="/etc/udev/scripts/android"
  • create the file /etc/udev/scripts/android with the following content (you can choose any MAC address, provided that it is a locally administered address, i.e. the second least significant bit of the most significant byte is set to 1):
해당 device가 연결되면 실행될 script를 아래처럼 작성한다.
$ sudo vi /etc/udev/scripts/android 

#!/bin/bash
ifconfig usb0 down
ifconfig usb0 hw ether 02:11:22:33:44:55 #<--- 원하는 mac으로 지정한다. 난 처음 연결했을때 mac을 지정했다.
ifconfig usb0 192.168.42.1
ifconfig usb0 up

  • make this file executable, and set its ownership to root:root
실행 script를 작성하고 해당 파일의 권한을 777로 한다.
$ sudo chmod 777 /etc/udev/scripts/android
  • reload udev rules:
sudo udevadm control --reload-rules
  • prevent Network Manager from managing usb0, adding into /etc/NetworkManager/NetworkManager.conf file the MAC address you specified in the script:
아래의 파일을 열어서 다음 항목을 추가한다.
sudo vi /etc/NetworkManager/NetworkManager.conf
[keyfile]
unmanaged-devices=mac:02:11:22:33:44:55 #<--- 원하는 mac으로 지정한다. 난 처음 연결했을때 mac을 지정했다.
  • restart Network Manager:
sudo service network-manager restart

That's it! 

여기까지하면 원하는 mac 주소와 ip가 설정 된다.

If the smartphone is replaced by another one, modify the serial attribute in the udev rule.

27-Jan-2016 - update

A few days ago, Marcello Messori kindly informed me about a new keyword, for the keyfile section: interface-name. This means that it's possible to specify the network interface without having to go through the MAC address steps. The keyfile section would be:

unmanaged-devices=interface-name:usb0

But for me, there is a problem. I use Linux Mint 17 MATE, and provided version of NetworkManageris 0.9.8.8. The interface-name keyword has been added in version 0.9.10. So, it's not available to me Frown. But if you run a more recent version of NetworkManager than mine, you should be able to bypass the MAC address stuff...


음... 근데 잘 안된다.