4

Before "burning" the Raspbian.img file to a SD card, I would like to edit it so the Pi uses a static IP address. (So I can run it in headless mode, without DHCP). I found some articles describing edits I could do in /etc/network/interfaces:

In file $SDCARD_ROOT/etc/network/interfaces, find line:

iface eth0 inet dhcp

and change it to something like:

iface eth0 inet static
address 192.168.1.145
netmask 255.255.255.0
gateway 192.168.1.1

(obviously, these are example IP addresses, replace them according to your needs).

But how can I do the edits in the image file when I'm on Windows?

3 Answers 3

6

Adding to the existing answer. I use a simpler solution to assign static IP for eth0 in Raspbian (Jessie) from Windows.

Assuming you want to use use 255.255.255.0 as subnet mask

Open your SD card installed with Raspbian with Windows Explorer through card reader. You'd find cmdline.txt. Open cmdline.txtand add ip = x.x.x.x after rootwait (x.x.x.x being the intended IP address).

Save it (with admin rights if needed)

It's ready to use.

7
  • what do you mean by cmdline.txt?
    – akavel
    Jan 2, 2017 at 18:48
  • Open your SD card installed with Raspbian with Windows Explorer through card reader. You'd find cmdline.txt. I used this method to give IP address to RPi's eth0. Jan 2, 2017 at 23:23
  • @WidiWidiyanto This is very interesting thanks. This seems to set my eth0 ip address to the value I give it but what about wlan0? Is there a way to update that instead? Or both? I am using wifi and it still uses dhcp for wlan0 with your solution. Oct 27, 2020 at 1:20
  • @Eradicatore If I remember correctly, I edited eth0 first, then I use P2P connection & SSH to my RPi. Or just VNC into it. Then I configure wlan0 from there. A bit too simplistic, but hey it worked. LoL Oct 28, 2020 at 3:26
  • 1
    @WidiWidiyanto One correction on this by the way: netmask does NOT have to be 255.255.255.0. See the help at kr15h.github.io/RPi-Setup/index.html#titles.3. You can use something like ip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off at the end of cmdline.txt to set the other parts of your connection. I just confirmed netmask with this just now. Not sure yet what the "off" at the end is for yet. Nov 3, 2020 at 14:09
4

After you have written a fresh Raspbian image to your SD card, mount the SD card. If it isn't visible in File Explorer, just remove the card from the slot and reinsert it after 10 seconds or so.

Open the SD card, then find and open the file cmdline.txt in Notepad. Save a copy of it, name it something like cmdline-original.txt.

It will look something like this:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh

Change it to this (assuming your IP will be 192.168.1.145 as in your example):

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh ip=192.168.1.145

If you've already booted the Raspberry Pi off of this SD card once, you may not have the section init=/usr/lib/raspi-config/init_resize.sh but just put your ip=192.168.1.145 bit on the end of the line anyway. It should still have only the one line. Don't let it break across multiple lines.

This is not the preferred way of setting your IP, and the use of /etc/network/interfaces has been depreciated in favor of /etc/dhcpcd.conf, so after you succeed in connecting to the Pi you need to open the file /etc/dhcpcd.conf and put your IP address in it. If you don't feel comfortable using console-based text editors yet, you can use the following command in your console to insert the IP address info you specified:

sudo tee -a << EOF /etc/dhcpcd.conf > /dev/null
interface eth0
static ip_address=192.168.1.145/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
EOF

This is an expanded version of Widi Widiyanto's answer. I am not able to comment directly, so I posted it like this. Also, don't forget to restore your cmdline.txt file to it's original state.

2

(might also be possible with "Raspberry Pi emulation for Windows" project instead of Levinux, but below is how I did it before I found out about the former)

The following steps seemed to work for me:

  1. Download Raspbian ISO (torrent)
  2. Download Levinux (~20MB), unzip into Levinux folder.
  3. Add raspbian img file as new virtual disk in Levinux:
    • go to Levinux\Levinux on Mac.app\Contents\MacOS\;
    • copy RunOnWindows.bat to new file RunWithRaspbian.bat in the same directory;
    • right-click RunWithRaspbian.bat and choose Edit (to open in Notepad);
    • below line with:
      -hdc tce.qcow ^
      add a line like:
      -hdd 2014-01-07-wheezy-raspbian.img ^;
    • move the Raspbian .img file 2014-01-07-wheezy-raspbian.img to the same folder where your edited .bat file exists;
  4. Start Levinux: double-click RunWithRaspbian.bat;
    • if it crashes when doing some installation stuff, try starting again (worked OK for me on next start);
    • Login: user: "tc", password: "foo" (source: tinyapps.org);
    • sudo su ;
    • mount /dev/sdd2 # (mount partition 2 of raspbian img);
    • cd /mnt/sdd2;

Now, after typing ls you should see contents of your SD-card. You can edit files with vi. (NOTE: vi is a very user-unfriendly text editor, unfortunately; you must google for some tutorials how to edit anything in it, sorry! The most important is to know how to exit from vi without saving: ESC ESC ESC :wq!.)
UPDATE: if Internet connection will work for you from Levinux console, you can try installing the "nano" editor, which is more user-friendly than "vi"; the following steps should make it work:

tce-load -wo nano.tcz
tce-run nano
# or just:
nano

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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