Connect Raspberry Pi to WiFi and give static IP from command line

First off, let me tell you I am no NOOB to linux, but finding a configuration that worked for me here was a bit time consuming. It looks as though there are numerous ways to go about doing this, and I think I tried about a dozen before I found one that worked with my RPi in my configuration. So I can’t guarantee this will work for you, all I can say is it works for me and it will hopefully help you out too.


All I really did was edit /etc/network/interfaces


You can let the first few lines I show below be, they have to do with the loopback interface and the wired ethernet port on the RPi.

auto lo

iface lo inet loopback
iface eth0 inet dhcp

After those lines we get into the config for the wireless device. In this case, and probably most, this is named “wlan0”. I changed the “iface” line to say static instead of dhcp, and I commented out the wpa_supplicant line. I was never able to get anything to work using the wpa_supplicant.conf file. Then I added in the lines to tell it what wireless network to connect to and what IP address to use, as well as the rest of the needed network config.


auto wlan0

allow-hotplug wlan0

iface wlan0 inet static

        wpa-ssid [your-wifi-name]

        wpa-psk [your-wifi-password]

        address x.x.x.x


        gateway x.x.x.x

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

Make sure to change the above to use your actual WiFi name and password. Also change the address to what you want your static IP address to be, and change the gateway to the IP address of your router.

Add the “#” to the wpa-roam line to comment it out, or delete that line.

You can then save and close the file. At this point you can reboot or type the following commands to restart the wlan0 interface:

sudo ifdown wlan0

sudo ifup wlan0

You should now have your wifi connection and it should be using the IP address you gave it.

For you security minded people, yes I know I have my wifi password in a plain text file. I’m not that concerned about it because this is just a RPi at my home I have for testing and learning purposes.  If you would like to keep your password more secure look into creating a hash file using wpa_passphrase.

Minimize/reduce/stop corruption on Raspberry Pi SD card

If you have spent any time with a Raspberry Pi you have undoubtedly had at least 1 SD card get corrupted on you. SD cards are susceptible to write fatigue, so running an operating system from one, even a light weight one designed for the RPi, can damage the card relatively quickly.

One of the biggest reasons for disk, or in this case card, activity is logging. Unless you are trying to troubleshoot something specifically you probably won’t spend much time looking at /var/log. This location and /var/run, where lock file, PID files, etc reside are common places for the OS to write data to disk.

One of the greatest strengths of Linux is its flexibility and customization. One of these is the ability to use tmpfs. This is a feature of mounting a location in the filesystem that resides in RAM and so never gets written to the disk. To set this up for the two location mentioned above add the following line in your /etc/fstab file:

none /var/run tmpfs size=1M,noatime 0 0
none /var/log tmpfs size=1M,noatime 0 0

These lines mount /var/log and /var/run using tmpfs at boot time, and give them an upper limit of 1MB. This is a pretty restrictive size, and if you are very active on your RPi, you may try setting /var/log to 2M or 3M. Keep in mind that you are working with a Raspberry Pi and have a limited amount of RAM to work with in the first place. (Although the Raspberry Pi 2 just got announced, and even though I have not been able to get my hands on one yet, the 1GB of RAM on this model should help alleviate the restriction here, with the 1GB you should be able to set this to 5M, which will be plenty of space for logging, and still have plenty available to use elsewhere.)

Another thing to do to help minimize the disk activity is to set /boot to read only. It isn’t very often this needs to get written to, and you can always change it back to “defaults” and reboot if need be. To set this, change the following line in red to the example in green inside /etc/fstab:

/dev/mmcblk0p1 /boot vfat defaults 0 2

/dev/mmcblk0p1 /boot vfat ro,noatime 0 2 

At this point you should already notice less activity with the filesystem light on the RPi, however there is one more item that you can modify to help extend the life of your SD card. You can disable the swapfile. The swapfile is a file on the SD card that is used as memory if you run out of system RAM. Even conventional computers have this feature, and even they have a huge performance hit when they start using swap. With the low specs on the RPi, and the transfer speed limitations of even a class 10 SD card, using the swapfile will bring your RPi to a crawl and render it essentially unusable until you reboot it. With all of this in mind, why even have it, so to permanently disable it run the following commands:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

After running this you should see something similar to the following when you run: free -m

pi@raspberrypi ~ $ free -m
             total       used       free     shared    buffers     cached
Mem:           438         59        378          0          9         27
-/+ buffers/cache:         22        416
Swap:            0          0          0


This should help considerably. Although nothing is going to make an SD card last forever. You could go even farther and once you have your RPi updated, configured, and acting like you need it to, then mount your root ,/, filesystem as readonly. This will make it so nothing writes to the SD card ever. This obviously will come with some drawbacks, like you won’t be able to install any new packages or updates unless you change the setting back and reboot. But in certain situations it could be done and be useful.

Yet another option would be to initiate the boot process from the SD card, then change the root filesystem to be on a USB flash drive (which will have the same drawbacks as the SD card) or a USB external hard drive or SSD. This is a post that I will do later. When I get it done I will come back and link to it here.