Initial setup – 1: Secure, Re-Partition & Swap

October 2012:

Note: These pages are now rather old and possibly out of date with the new Raspbian release, however I’ll keep them online as they may be of help to someone…


Note: These pages have been prepared for Debian Squeeze with some additions for the Debian testing release; Wheezy. Other Linux distributions may benefit from some of the information here, but you may need to work out some of the differences for yourself.

So first things first.

I obtained the latest Debian (The image from http://www.raspberrypi.org/downloads) and wrote it to my SD card. My SD card is 4GB but the image is 2GB… No matter for now. Put this in the Pi and booted it. All OK. As part of it’s initial setup, it reboots itself, but other than that no real surprises.

Update: I’ve just used the Wheezy install from http://www.raspberrypi.org/archives/1435 so will make some changes for this too.

Before doing anything else – are you in the UK? If so, then good. If not, then you might find that the keyboard layout is somewhat strange… If typing Shitf+3 gives you a £ sign instead of what you were expecting, then login (pi;raspberry) and issue the command:

sudo dpkg-reconfigure keyboard-configuration

Go through the menus for the keyboard layout that you need.

Carrying on… Lets make a little more secure…

If not already, login; pi:raspberry. Change your password with the passwd command. Just do it. If you don’t do it and your Raspberry Pi is ever connected to a LAN, then anyone will be able to login and delete all your files, or worse.

Now that you have a password, enable sshd (so that you can connect in remotely – if you never want to connect in remotely, don’t do this)

sudo /boot/boot_enable_ssh.rc

(You may not need to do this in Wheezy)

Now lets reclaim the unused space on the SD card. 2 ways – one would be to simply create a partition for it, then format and mount it, the other way might be to extend the existing partition. I chose the 2nd way and I’ll regret it when they produce another release, however I’m hoping it’s just a kernel release…

As supplied, the Debian image has 3 partitions. (Wheezy has 2) The first is a FAT32 formatted image which the GPU reads to boot itself and the ARM. The next is an EXT4 partition which is the main bulk of the system, and the last is a small swap partition which isn’t used in the initial setup.

We need to delete the swap and ext4 partition, then re-create the ext4 partition the full size of the device, then extend the filing system.

This is not an operation to be undertaken lightly – however the worst possible scenario is a total loss of all data on the SD card, and as that can be re-written from scratch, as long as you don’t have any valuable data on it, you’ll be fine.

sudo fdisk -uc /dev/mmcblk0

(Minor note here, this was done on the Debian Squeeze install, other Linuxes, and Debian Wheezy no-longer need the -uc flags, so if you get an error, just try again without the -uc)

Type ‘p’ it will print the partition table and look like this:

Disk /dev/mmcblk0: 3999 MB, 3999793152 bytes
4 heads, 32 sectors/track, 61032 cylinders, total 7812096 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          157696     3414015     1628160   83  Linux
/dev/mmcblk0p3         3416064     3807231      195584   82  Linux swap / Solaris

 We need to delete partition 3, then partition 2. At this point, we’re deleting the partitions, not the data! The command is ‘d‘, then it will ask for the partition, 3 then ‘d‘ and 2.

(And again, of using Wheezy, then there is no partition 3, but you still need to delete partition 2 – do remember to take note of the starting sector number – the wheezy install I’ve just done has it set to 122880 and not the 157696 that you see above!)

Now, create a new Primary partition; ‘n‘ and enter 2 as the partition number. It will not prompt for the start and here, you must not use the default, but instead use the number at the start of the /dev/mmcblk0p2 line. In this case it’s 157696 but do check as your may be different!

Let it pick the ending sector number and use the default.

Write it to the SD device ‘w‘ and you’re almost there. Because this partition is still in-use, you can’t do anything at this stage other than reboot the Pi. Exit fdisk and sudo reboot will do it.

You can try to use the partprobe command to force the system to re-read the partition, but rebooting is safer at this point.

Login again and now we have to re-size the filing system.

sudo resize2fs /dev/mmcblk0p2

This may take a minute or the, and then your done. Now the output of df -h / should show the additional space:

Filesystem            Size  Used Avail Use% Mounted on
rootfs                3.6G  1.3G  2.2G  38% /

Now a little bit of swap is good, a lot isn’t, so we should add in a little bit of swap, however if running Wheezy, then note that you don’t need to do this as it is already using the dphys-swapfile’ package.

So to create a small swapfile:

cd /var
sudo dd if=/dev/zero of=swapfile bs=1M count=128
sudo mkswap /var/swapfile
sudo swapon /var/swapfile

The dd command will take a few seconds to a minute depending on SD card speed. To make the swapfile work at boot time, you’ll need to edit the /etc/fstab file. Use the command

sudo nano /etc/fstab

and look at the last line – change:

#/dev/mmcblk0p3  none            swap    sw              0       0

into

/var/swapfile  none            swap    sw              0       0

and the next time you boot it will be configured automatically.

While we’re editing /etc/fstab, we should tidy it up and insert a new line to re-mount the root partition is a slightly more efficient manner. This is what my /etc/fstab looks like:

proc            /proc   proc    defaults             0 0
/dev/mmcblk0p1  /boot   vfat    defaults             0 0
/dev/mmcblk0p2  /       ext4    defaults,noatime     0 0
/var/swapfile   none    swap    sw                   0 0

The addition of the root device (mmcblk0p2) and the extra parameters of noatime will speed things up somewhat.

Now would be a good time to reboot again. Check the swap is enabled with the top command, and check the root mount flags with the mount command.

Comments

Initial setup – 1: Secure, Re-Partition & Swap — 40 Comments

  1. Hi, followed your guide, but at the end I get:
    pi@raspberrypi:~$ sudo resize2fs /dev/mmcblk0p2
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    resize2fs: Permission denied to resize filesystem

    Do you have any idea why?

    • Hi,
      If your sure you’re doing this as root, then I’m not sure what’s going wrong – I can see the sudo command in there, but I can’t see why it’s failing – sorry.
      -Gordon

      • I solved it by “reflashing” the SD card. And then your guide worked perfectly 🙂

        Cant help it but I’m still curious what caused the error 😛

  2. You may also want to change the swappiness value. By default it’s set to 1.

    • Setting it to 1 means that Linux will try to not automatically move unused pages out to swap… 0 should stop it entirely. Bit of a quandry there – my feeling is that moving totally unused stuff to swap will free-up RAM for disk & file buffers, speeding some operations up, but at the same time, may impose a bit of lag if/when one of the swapped-out processes needs to run…

      My thoughts now is that a value of (say) 10 isn’t going to do much harm, so I’ll give that a go myself for a day and see if it feels any different. Hard to really tell with just a “feeling” though!

      -Gordon

  3. I got one too – whoo hoo! Once I have anything entertaining running on it I will let you know.

  4. Thanks Gordon. Awaiting my Pi this weekend. I have bookmarked these pages as an excellent reference.

  5. My knowledge is really unix based, rather than linux, and while I understand the swappiness point, which is well made given that SD cards are slow and prone to fail with frequent overwrites, in unix days, there was a severe risk if the pager could not allocate sufficient VM even if it was not used. The result of this was generally a kernel panic. The rule of thumb was to have at least 2 times as much swap as ram and preferably more.

    Setting up a new machine, the rule (that I used) was to allocate twice as much swap as the amount of ram that the machine could possibly address so that the disk did not need to be reformatted if a ram upgrade was applied.

    So I am surprised to see the RPi with less swap than ram.

    Thoughts/comments?

    I have just re-sized my SD card which is a Class 6 16GB card (and I am still finding it hard to believe that this was otherwise surplus to requirements!) and have left room to put 750MB swap at the end of the main volume although it remains with 191Mb for now.

    By the way, does anyone know whether debian will just use whatever swap is allocated or do I have to tweak the kernel to use additional swap, should I play with gparted again…???

    Ian

    • Ian,

      I too am an old Unix hand, (v6, v7, SunOs…) however Linux is a subtly different beast. The requirement for any swap (let alone 2x) went out of the window many years ago in Linux. These days it will use whatever you give it (if any).

      Also, swapping to a swapfile is almost 100% identical to swapping to a partition and IMO it gives you a lot more flexibility. Just create another file, use the mkswap and swapon commands…

      So my advice is to dump the swap partition to give you more space in the filesystem and create a swapfile instead. Just 64 or 128MB – realistically, swapping to any media on the Pi (especialyl SD) is going to be slow, to best to avoid it in the first place! (Hard, I know, but …) You might want to investigate using the 224 bootloader too – unless you’re running any of the acellerated 3D demos..

      So

      cd /boot
      sudo cp arm224_start.elf start.elf

      and reboot.

      -Gordon

  6. Hi Gordon,

    Many thanks or this great tutorial…

    just for information, when recreating 2nd partition with n, fdisk prompt e for extended and p for principal before choosing 2 😉 so that means e need to type p before choosing 2.

    this stand for a debian squeeze

  7. Hi Gordon….

    Ur initial set up guides are precise/wonderful and helps to get job done.

    I request for small help…which

    #—————————————————————————-
    till 11/07/2012 – I was able to connect to Rpi from netbook having lubuntu using
    command – ssh pi@10.42.0.61

    this ip address I got by using command ip addr…..& netbook is directly connected to Rpi using ethernet cable.
    #—————————————————————————
    on 12/07/2012 – I did normal updation of lubuntu (which is installed in my netbook) from net. and after this I am unable to connect to Raspberry pi using ssh. Then I reinstalled OS latest debian (downloaded from Raspberry pi website)
    and to my surprise I am unable to see the ip address. Below are results I am getting……..
    #—————————————————————————-
    pi@raspberrypi:~$ ip address
    1: lo: mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    2: eth0: mtu 1488 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether b8:27:eb:02:28:2c brd ff:ff:ff:ff:ff:ff
    #—————————————————————————-
    pi@raspberrypi:~$ ifconfig
    eth0 Link encap:Ethernet HWaddr b8:27:eb:02:28:2c
    UP BROADCAST RUNNING MULTICAST MTU:1488 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:8 errors:0 dropped:0 overruns:0 frame:0
    TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:560 (560.0 B) TX bytes:560 (560.0 B)

    #—————————————————————————-
    pi@raspberrypi:~$ ping 127.0.0.1
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.198 ms
    64 bytes from 127.0.0.1: icmp_req=2 ttl=64 time=0.148 ms
    64 bytes from 127.0.0.1: icmp_req=3 ttl=64 time=0.155 ms
    64 bytes from 127.0.0.1: icmp_req=4 ttl=64 time=0.154 ms
    64 bytes from 127.0.0.1: icmp_req=5 ttl=64 time=0.156 ms
    64 bytes from 127.0.0.1: icmp_req=6 ttl=64 time=0.157 ms
    64 bytes from 127.0.0.1: icmp_req=7 ttl=64 time=0.154 ms
    #—————————————————————————-
    root@raspberrypi:/home/pi# netstat -ntlp|grep sshd
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 985/sshd
    #—————————————————————————-
    Tried many places in internet to find the solution…but no success…..can u please help……

    Best Regards,

    Jishnu

    • It looks like the Pi running Debian has not configured its IP address correctly. It needs to have something on your network giving out IP addresses using DHCP, however you can statically set the IP address if needed.

      So rather than connect the Pi to your Laptop directly, can you connect them both to a router that’s giving out IP addresses?

      However if that’s not possible, then you can manually configure both the Pi and the Laptop by giving the commands:

      sudo ifconfig eth0 192.168.0.1 (on the Laptop)
      sudo ifconfig eth0 192.168.0.2 (on the Pi)

      however that will not survive a reboot, so editing the /etc/network/interfaces file may be the way forward.

      Note that the Pi is really designed to be connected to the Internet in some way – that’s the only way it will get it’s clock set correctly.

      -Gordon

      • Thanks “GURU – GORDON”……
        (a) for providing solution quickly….
        (b) …. it worked… I added address both in Net Book as well as raspberry pi …./etc/network/interfaces. BTW I do not have router and connect to internet using 3G modem….. So next time if I connect this net book to LAN in my office…..means again I have to re-configure eth0… am I right….

        BTW what is the progress of Return to Basic… apple soft basic is my favorite…..am looking forward for the same soon….

        Best Regards…. Jishnu… Muscat – Sultanate of Oman.

        • Yes, you’ll likly need to do the manual configurations when you next move it -although connecting to the office LAN ought to be fine, it’s just when you connect the 2 devices directly together.

          Return to BASIC is just waiting for me to have a little bit of time to package it up nicely. Soon!

          -Gordon

          • Good Morning… with the solution u ‘ve provided above… working well. Now i wish to transfer/copy files from net book to Rpi and vice versa…. any idea how????

            With Best Regards,

            Jishnu

          • Since both sides are Linux, then you can easilly use the scp command from one to the other. If you can ssh, then you can scp – or even better for multiple files, then you can use the rsync command. See their man pages for more details.
            -Gordon

  8. Hey teacher…
    Nice tutorial. I wish I had understood more of it. What linux I know comes from years of running a single-user LAMP server in Fedora.

    My question:

    root@raspberrypi#/ df -h
    Filesystem Size Used Avail Use% Mounted on
    rootfs 1.8G 1.6G 46M 98% /
    /dev/root 1.8G 1.6G 46M 98% /
    tmpfs 19M 240K 19M 2% /run
    tmpfs 5.0M 0 5.0M 0% /run/lock
    tmpfs 37M 0 37M 0% /tmp
    tmpfs 10M 0 10M 0% /dev
    tmpfs 37M 0 37M 0% /run/shm
    /dev/mmcblk01pi 56M 36M 21M 64% /boot

    • No question?

      Your df output looks OK for a 2GB device.

      However I probably need to go back and update that page – it was written in the early days of the Pi (How much things change in just 3-4 months!) for the early Debian releases – Raspbian has built-in options for things like resizing, etc…

      -Gordon

  9. Hey teacher…
    Nice tutorial. I wish I had understood more of it. What linux I know comes from years of running a single-user LAMP server in Fedora. Now I’m using wheezy, which appears to be an all-around better distro for newbies.

    My question:

    root@raspberrypi#/ df -h
    Filesystem Size Used Avail Use% Mounted on
    rootfs 1.8G 1.6G 46M 98% /
    /dev/root 1.8G 1.6G 46M 98% /
    tmpfs 19M 240K 19M 2% /run
    tmpfs 5.0M 0 5.0M 0% /run/lock
    tmpfs 37M 0 37M 0% /tmp
    tmpfs 10M 0 10M 0% /dev
    tmpfs 37M 0 37M 0% /run/shm
    /dev/mmcblk01pi 56M 36M 21M 64% /boot

    How much memory do I have left for applications?? Do I have 64% remaining or 98% used? Its a 32 Gig SD card if that matters.

    • OK. You have hardly any left as the system thinks it’s a 2GB SD card. So you need to resize it.

      So if you start at the repartition part of that original page, then you should be able to work through it, although if it’s the Debian Raspbian image you have, then there is a built-in program that would be run the first time you boot it, but if not, then you can re-run it.

      -Gordon

  10. Yeah, I couldn’t edit my own post. Sorry about that.

    You said it looks okay for a 2 gig device. Do you mean for a 2 gig SD card?
    Thats the output from an RPi with a 16 gig SD card.

    Unfortunately, very little of this works for wheezy. Which begs the question, “Why aren’t you people using Wheezy”?

    Read up at raspberryPi.org and you’ll see Wheezy is no longer a “test” OS. Its the recommended OS. Particularly for newbies – which EVERYONE IS –

    So anyway, under Raspian Wheezy (updated on Aug 30th), creating a brand new image on SD boots nicely and isn’t in the least bloated or lacking in features. Immediately it will detect if you’ve ran raspberrypi-config and take you to a GUI where you can do some setup tweaks. The second line is where the SD card is re-sized for you. Upon next reboot, go have lunch while it does its thing. A 32G Class10 card took 40 minutes to reshuffle itself on my system. Now I have more room than I’ll probably ever use.

    Thank you!

    • Glad your sorted now, but I think it is time to update my pages to reflect the Raspbian release (but I’ve no idea what the other istros are now doing – e.g.g Arch).

      Technically speaking, Wheezy is still a “test” release – as fat as the Debian people are concerned anyway, however I understand there’s a code/feature freeze on it now, so it should become the official stable Debian soon. Raspbian is a complete re-build of Debians Wheezy more suited for the ARM processor on the Raspberry Pi.

      And yes, the re-size can take some time on a bigger system! Do remember to keep a backup of anything you put on the Pi though!

      -Gordon

  11. Resizing by command line is alright but using ‘Gparted’ graphical partition manager is so much simpler! Using an Ubuntu install I have on my main machine (live CD/DVD would do it as well) just run Gparted and use it to slide the swap partition to the end of the card and then expand the ext4 partition to make use of the freshly released space. Job done without a terminal box in sight. Even the 2GB images need this, although Raspbian does offer expansion on first boot now as you mention above.

    • Gparted might well be simpler – but in the early days when I wrote that guide, it wasn’t avalable on the Pi itself – also this is the first time for a lot of people using Linux – so they may not have the luxury of a 2nd PC to do the resize…

      And some people like the command-line anyway 😉

      -Gordon

  12. Do you mind if I quote a few of your posts as long as I provide
    credit and sources back to your website? My website is in
    the exact same niche as yours and my users would genuinely benefit from some of the information you provide here.
    Please let me know if this alright with you. Thank you!

  13. Hello
    I have seen the Raspberry in Maplin and would like to learn embedded C programming using it.

    Where would I start?

    Eric

    • The Pi has some limitations when it comes to treating it a an embedded device, however you can certianly learn C on it and use it to control things via the GPIO port. I don’t know what to suggest though in terms of books, etc. as I’ve been programming in C for over 30 years now …

      What sort of programming & hardware experience do you currently have?

      -Gordon