우분투에서 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
27-Jan-2016 - update: see at the end of the article.
I'm currently developing a system where an Android smartphone, used as a (sophisticated ) 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)
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:
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):
#!/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
- 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:
[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 . But if you run a more recent version of NetworkManager than mine, you should be able to bypass the MAC address stuff...
음... 근데 잘 안된다.
'Linux > Ubuntu' 카테고리의 다른 글
네트워크 우선 순위 정하기 (0) | 2018.03.19 |
---|---|
Ubuntu Oracle JDK 7 is NOT installed 해결법 (0) | 2017.11.24 |
ubuntu 16.04에서 network interface의 이름이 'eth0'가 아닌경우 (0) | 2017.11.21 |
계정 추가 (0) | 2016.08.11 |
16.04 samba 설치 및 설정 (0) | 2016.08.11 |