Advertisements

RPi: Static WiFi Addressing

Having configured ssh on the Raspberry Pi for public keys, the next step is to cut the cord by configuring the USB WiFi dongle to automagically come up with a static IP.

[Update: As of 2017, set a static IP by tweaking /etc/dhcpcd.conf instead. Search the blog for that to find recent descriptions. ]

Make /etc/network/interfaces look like this:

auto lo

iface lo inet loopback

#iface eth0 inet dhcp

auto eth0
#iface eth0 inet static
iface eth0 inet manual
 address 192.168.1.209
 gateway 192.168.1.1
 netmask 255.255.255.0
 network 192.168.1.0
 broadcast 192.168.1.255

allow-hotplug wlan0

#iface wlan0 inet manual
iface wlan0 inet static
 address 192.168.1.9
 gateway 192.168.1.1
 netmask 255.255.255.0
 network 192.168.1.0
 broadcast 192.168.1.255

#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

iface default inet dhcp

Then set up /etc/wpa_supplicant/wpa_supplicant.conf thusly:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="whatever it might be"
	psk="choose your own password"
}

You want different IP addresses for the eth0 and wlan0 devices, because you never know when you’ll be forced to use them at the same time.

Using wpa-conf rather than wpa-roam prevents the machinery from automagically doing things when you’re not watching.

The router can hand out IP addresses based on MACs, but that means bottling up all that configuration in a single device that might go toes up. Forcibly configuring each device to a static IP adds a bit of resilience to the network, right up to the point where you must change all of them at once.

Alas, the router seemed to lose track of the Pi after a day. Pinging from my desktop box reported Destination Host Unreachable, even though signing on through the USB keyboard showed the USB WiFi link (a netis WF2123) was still up. Signing on to the router and refreshing the DHCP list (even though the RPi has a static IP) knocked things loose: suddenly the RPi became pingable.

It seems the WiFi link turns itself off after a while, which can be averted by tweaking the options:

sudo nano /etc/modprobe.d/8192cu.conf
# Disable power saving
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0
  

The WordPress sourcecode tag seems to turn underscores into blanks [Update: on the last line of a sourcecode block, which I’ve now forced to be a blank line; the options should read rtw_power_mgmt and rtw_enusbss, respectively.

Anyhow, the rtw_enusbss option prevents the USB interface from going down. It was already zero in the default configuration, but I presume there’s no harm in clearing it again.

Advertisements

  1. #1 by solaandjin on 2015-03-22 - 08:41

    Do you use static IPs for all your machines, or DHCP for some and static for others? If the latter case, you can edit dhclient.conf to request a specific IP address so that the router knows not to hand it out to somebody else.

    • #2 by Ed on 2015-03-22 - 09:35

      All the desktop / machine tool boxes have static IPs scattered through .1 to about .30, mostly so I can ssh into them from the Comfy Chair, and everything else gets a dynamic IP in the range .50 to .99 from the router.

      Yeah, a manually updated hosts file on each box: definitely Old Skool. Fortunately, the IP addresses don’t change often enough to make anything more complex worth the effort.

  2. #3 by Frans on 2015-03-27 - 18:18

    The underscores work for me. Perhaps it’s a font issue?

    • #4 by Ed on 2015-03-28 - 07:16

      Apparently, it has something to do with font spacing, as it only happens on the last line in a block with the sourcecode tag; forcing a blank line at the end makes it work perfectly.

      Just another workaround to remember… [sigh]

  3. #5 by Red County Pete on 2015-03-27 - 19:46

    No problems here with Pale Moon on a Windows box. Turning on solfsolder.com scripting changes the font to sans-serif (from courier) and gives me nice colors in the code, but underscores work fine both ways.

  4. #6 by Nadia on 2015-05-16 - 15:43

    hello can you help me with my wifi config raspberrypi……………

    i want the raspberrypi on a dhcp static address when i’m on the wifi mode
    and if i want the rj45 cable mode ip static adress (dhcp) i want that works too. right now it’s only this config that’s works

    my raspberrypi is set on my DHCP server (enfian fw) the MAC adress and his IP on the dhcp static ip adress (eth0) and also another MAC adress for dhcp static wifi address (wlan0).

    here on the raspberrypi :

    file /etc/network/interfaces:

    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet static
    address 192.168.1.155
    gateway 192.168.1.100
    netmask 255.255.255.0
    network 192.168.1.0

    auto wlan0
    allow-hotplug wlan0
    iface wlan0 inet static
    address 192.168.3.252
    gateway 192.168.3.100
    netmask 255.255.255.0
    network 192.168.3.0

    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

    file /etc/wpa_supplicant/wpa_supplicant.conf :

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1

    network={
    ssid=”name_here_routerwifi”
    psk=”pw”
    proto=RSN
    key_mgmt=CCMP
    auth_alg=OPEN
    }

    when i do ifconfig i see the wlan0 192.168.3.252 and eth0 192.168.1.155

    to test only my wifi config, i unplugged the rj45 cable, to have only my wifi dongle activated:

    if i ping 192.168.3.100 or 8.8.8.8 i get destination host unreachable!!!

    so here are the results when the 2 interfaces are on:

    route -n

    192.168.1.0 0.0.0.0 255.255.255.0 eth0
    192.168.3.0 0.0.0.0 255.255.255.0 wlan0

    arp -n

    192.168.1.100 ether MACaddress C eth0
    192.168.3.100 incomplete C wlan0

    i need help!

    thanks!!!

    • #7 by Ed on 2015-05-16 - 16:07

      The wired subnet (192.168.1.x) is different than the WiFi subnet (192.168.3.x), so the network mask on whatever you’re pinging must look something like 255.255.0.0.

      If you’re pinging from the RPi, then the two NICs can’t see each other, because they’re in different subnets. If you’re pinging from a PC with a suitable mask, then the upstream router(s) must bridge those two subnets for the PC to see RPi’s NICs.

      At least, I think that’s the way it works, but I admit to not being much of a wizard at that stuff.

  1. RPi: Time-lapse Photos | The Smell of Molten Projects in the Morning