2

OS: Debian 8.2.

There are two network cards: eth0 and wlan0. Here is my /etc/network/interfaces file:

iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1

auto wlan0
iface wlan0 inet manual
wpa-driver nl80211
wpa-roam /etc/wpa_supplicant.conf
iface default inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1

It is my target that the wlan0 will be active and eth0 will be inactive when eth0 and wlan0 are all can be connected.
The fact has been tested that eth0 will be active when eth0 and wlan0 are all can be connected and all can be used.
The script can't realize my goal; how should I rewrite it? I rewrote the /etc/network/interfaces as the following:

auto wlan0
iface wlan0 inet manual
wpa-driver nl80211
wpa-roam /etc/wpa_supplicant.conf
iface default inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
if-up ifconfig eth0 down

The info display on the screen when I input ifconfig after rebooting my PC.

eth0      Link encap:Ethernet  HWaddr e0:3f:49:57:4b:d1
          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e23f:49ff:fe57:4bd1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6068 errors:0 dropped:0 overruns:0 frame:0
          TX packets:113 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3780468 (3.6 MiB)  TX bytes:11800 (11.5 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:700 (700.0 B)  TX bytes:700 (700.0 B)

wlan0     Link encap:Ethernet  HWaddr 00:21:5d:26:c1:10
          inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::221:5dff:fe26:c110/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1291 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5146 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:81917 (79.9 KiB)  TX bytes:992000 (968.7 KiB)

Now I am confused that which IP is mine? 192.168.1.3 or 192.168.1.101?
Which one is at work? 192.168.1.3 or 192.168.1.101?

3
  • Can you turn off eth0 with sudo ifdown eth0 ?
    – RobertL
    Nov 21 '15 at 3:34
  • manually it works, you can do it automatically. Have a look at my answer. Nov 21 '15 at 8:52
  • Both 192.168.1.3 and 192.168.1.101 are IP addresses of your machine. For example, you machine may run a web server on port 80 of both IPs. Even more, your machine can serve different web pages for different IPs, so that http:://192.168.1.3 has different contents from http:://192.168.1.101. Any machine tries to access 192.168.1.3 must come from the ethernet line, while any machine tries to reach 192.168.1.101 must come from the wireless connection. This is called multi-home. Check the term on the internet.
    – Robin Hsu
    Jun 22 '16 at 5:17
1

Short clarification : actions can be added to the interface configurations as actions do be done before or after interfaces are up with the directives pre-up and if-up.

Adding to your configuration an instruction to take down eth0 after wlan0 goes up successfully:

auto wlan0
iface wlan0 inet manual
wpa-driver nl80211
wpa-roam /etc/wpa_supplicant.conf
iface default inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
if-up ifconfig eth0 down
if-down ifconfig eth0 up

I recommend ifconfig instead of ifdown as ifconfig does not throw an error back if the interface is not up and is a simpler operation (ifdown is a script)

2
  • which ip is at work now?i am confued when to reboot pc according to your script.
    – showkey
    Nov 21 '15 at 14:38
  • I just fixed it. You do not need to reboot. When wlan0 goes up, eth0 interface is deactivated, and when it goes down, it is activated again Nov 21 '15 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.