logo

Practical Computing Advice and Tutorials

Tue: 23 Jul 2019


Site Content

Programming
&
Development


Technical Knowhow


Command Line Interface


Security

Setting up a Raspberry Pi

The motivation here is to set up a Linux Box so that it can be accessed from a PC on your LAN, via SSH, and be a useful addition to your home or office network by providing a central place for shared files (on your LAN), a central connection point for a shared printer, as well as learning platform for the CLI, Programming in C (which I'm using to teach myself about that particular programming language) or Python, as well as many other useful functions that I'll get to as and when.

Why Linux?

Unix based computer operating systems (of which Linux is just one example) are very versatile systems which can be tailored to a specific task. I don't subscribe to using one OS over another, but for some tasks only a Linux OS will do, but for other tasks, only a MS Windows OS will do. As the saying goes: It's horses for courses.

If you choose to use a Linux OS, you'll quickly discover the power of the Command Line Interface (CLI). Learning the commands for the CLI can seem daunting, but operating a headless Linux system (that is a system that does not have any directly connected human interfaces) via a remote terminal is not that difficult, once you have a basic grasp of the most commonly used commands.

There are loads of Linux user guides on the Internet, written by very knowledgeable people. These guides will take-you-by-the-hand, and lead you through the options, which is exactly what I needed when I started with Linux, and to which I still have to refer. Two links to get you started with the CLI...

https://www.howtogeek.com/140679/beginner-geek-how-to-start-using-the-linux-terminal/

http://linuxcommand.org/index.php

For the more intrepid, you have an in-line manual to which you can refer. Try the commands man man or man intro. The manual pages are very well written, from a technical view point, and are where much of the information found in other guides, comes from; the source, if you will.



Let's get going...

I'm going to be using a Raspberry Pi Model B (512MB RAM, UK Model) with a 16GB SD Card. The SD Card is used for the OS, but for the main storage, I'll be connecting a 500GB External Hard Drive, via USB, as 16GB can soon become full if used for file/data storage as well. The Pi also requires an external PSU, for which I'm re-purposing a 5v 850mA phone charger.

Although for the set up, a mouse, keyboard and display device is needed, after that, the Pi can be completely controlled from a PC on the LAN. For these peripheral devices, I'll use the HDMI connection on my TV and a USB dongle as the keyboard/mouse controller. The OS I'll be installing is Raspbian Jessie Lite, which has no DTE (Desk Top Environment) as I'll not need one.

I downloaded the OS from https://www.raspberrypi.org/downloads/

To install the disk image, I'm using Etcher Portable from https://etcher.io/

Put the SD Card into a card reader that's a part of the system on which you'll be running Etcher. The card should mount and you should be able to see it in your list of attached devices, even if you can't access it. Don't be concerned about formatting it as Etcher will take care of that.

Run Etcher, select the Raspbian.img and the SD Card, and then click the 'Flash!' button. Everything else is automated.

Etcher

When Etcher has done it's thing, the SD Card will be ready to put into the Pi, which you can then power-up, having first connected the USB dongle (if that's what you're using) and HDMI display. If all is well, the Pi will boot-up.

What happens next will depend on what .img you're using. For me, as I've said, I'm not using a DTE, so I'm seeing a login prompt, the default user name is pi and the password is raspberry. But, if you've chosen a DTE, you'll be presented with a GUI for the rest of the install.

Connect the Pi to your LAN and then Login and type ifconfig. Look for the IP Address for the eth0 interface, and make a note of the address (look for the line inet addr:), as you'll need it later.

For more information on the ifconfig command, I've written about it here as an introduction to using the CLI

The next thing to do is to make sure that the system is up-to-date. To save having to prefix admin commands with sudo, just enter the command sudo su which will put the system into SuperUser mode and the prompt will change from :~$ to :/home/pi#

Now enter the command apt-get update

You should see some Get requests generated while the Pi connects to the http servers to get all the relevant package lists. Keep an eye on things, checking for errors, which if exist, should be looked into before continuing.

After it's done, enter the command apt-get upgrade

This should give you a list of packages that will be upgraded. Just hit enter to continue and again you should see a series of Get requests; go and make yourself a coffee while the system updates.

Two more house keeping commands to make a note of...

apt-get autoremove
apt-get autoclean

Those four apt-get commands should be used on a regular bases, say, once a month.

You can also use the apt-get dist-upgrade command every once in a while, or if the system indicates that a new distro (Linux OSs are called distributions) is available.

Type exit to exit out of SU mode and then change the default account password with the command passwd

You're now ready to use your Linux Box for whatever you wish.

So that you can login to your Linux Box from a PC on your LAN, you'll need to be running an SSH Server. Check that the service is running with the command service ssh status If it's not running, start the service with service ssh start then systemctl enable ssh so that the service will autostart, should the power be interrupted or the system is rebooted for some reason.

Before we shutdown the system and disconnect the stuff we don't need, use an SSH client on your PC to SSH into your Linux system. With a Windows system, you can use PuTTY or KiTTY, there may be others, but I find KiTTY to be very good.

If you can't login to your Linux Box from your LAN PC (or maybe the update command failed), investigate the issue before continuing.

Some things to check on include...

If all seems to be well on the Linux Box, that is to say, you can ping your NAT Router, then it's likely an issue at your PC. Is your Personal Firewall blocking the SSH Client?

If your Network is any more complex than that, then you shouldn't need my guidance; you should know how to manage the Network that you have.

So, SSH into your Linux system and login with your new password. If your new password doesn't work, try the old one. If the old one works, you should see a warning regarding the default password, when you connect, so change it with the passwd command and then use the reboot command to check all is well before continuing.

If all is well, the Linux system can now be shutdown and the unneeded peripherals disconnected. Enter the command shutdown now then after the Pi has shutdown, remove the power connection, then disconnected the HDMI cable and any other unneeded peripherals. You can then reconnect the power and control the Pi (or whatever the Linux Box is) from your PC.

One of the things that I like about KiTTY is that the window will stay open, so that when the remote system is powered on again, you'll get an auto Logon without having to re-enter the IP Address of the remote; you simply hit the Enter key.

Power the Linux system back up, wait 30 seconds or so and then login over SSH.


Now to set-up the Time Zone...

Use the command sudo dpkg-reconfigure tzdata to get this screen

Time Zone

Select the correct time zone by using the up arrow key and then press the TAB key to select <Ok> and press enter. Now select the correct city with the down arrow and again TAB to the <Ok> and press enter.

You should get a conformation message, but you can also check that all is well with the date command.

If you get stuck, please leave a comment and if I can help I will, but as people have slightly different LAN and PC configurations, it's not easy to offer much advice about a configuration that I don't have direct access to.



Adding an External HDD to the Rpi

Although you can simply use the SD Card that the Rpi boots from as the main file storage medium as well, it can soon become full. So, the motivation here is to add a larger storage device so that we have a more useful capacity for LAN file sharing. It's also more than possible to use the Pi as a media player, but I.M.H.O you'd be better off with a more powerful device for that kind of usage. That said, media files can still be stored on our attached USB Drive and then accessed with an external player, but that's for another project.

I'll be using a 500GB USB connected HDD for this project, you can use whatever you have, but it will need it's own power supply as the power from the Pi USB will not be sufficient, so don't even try it. At best you'll simply crash the Pi, at worse, you break it altogether.

It's also possible to connect a powered external USB Hub, but for me, I don't want stuff hanging off my Pi, so I've not done that.

Formatting The External HDD

If you want the option of being able to plug the drive into your Windows PC (which can be a handy option to have) it'll need to be formatted to .exFAT. You could create a fully partitioned drive with different partitions for different stuff. That's an advanced way of doing things and should be considered, but again, if you want to be able to plug the drive into you Windows PC for any reason, you'll have a problem.

The Linux File System

One of the things that Windows users find a little confusing, at first, is the Linux (or Unix) file system; I did. But, I found that after a while, that the Linux system is in fact less confusing than the Windows system! Here's an overview...

File System Tree

I can't give any credit to the creator of this diagram because I don't know who created it, but I do thank the creator as it's very useful.

For me to fully explain the details of this file system, would be like me reinventing the wheel as there's a lot of information already out there and it would also distract from the focus of this How To, but I do encourage the reader to do some research into the topic.

This guide [https://www.howtogeek.com/] is as good a place to start as any.

Power up your Pi and login. Unless you've changed anything you should be now looking at a prompt...

pi@raspberrypi:~$

That's the account that you're logged into: pi is the user name and raspberrypi is the domain name. If you're not there, enter the command cd If you're lost, then logout and then log back in.

Now enter the command cd /dev then ls You'll see quite a few entries here, but the ones to look out for are prefixed sd I'm hoping that you don't yet see any. Now plug the USB HDD into one of the USB sockets of your Pi and do another ls (up arrow will bring back the last thing you typed). You should now see sda and sda1 which means drive sda has been connected and partition sda1 has been found.

The partition will now need to be mounted so that it is accessible to the file system; we do this using the mount command and a mount point within the file system. The mount point can be any empty directory, but the standard point is the /mnt directory. If you choose to do things your own way, just remember that if you ask for help from someone and things are custom to you, then it'll be harder for you to get help.

I'm going to use a directory within the /mnt directory, to uniquely identify this HDD.

To follow along, enter the command cd /mnt followed by ls I have an empty /mnt directory.

Now, the mkdir command can be used to make a new directory for the mount point for my HDD; sudo mkdir lacie_500_GB is the command I'll use, but you may want a different name, just don't use any spaces in your name. Now I'm going to mount the drive with...

sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/lacie_500_GB

If I now issue the command cd lacie_500_GB I'm now seeing the root section of my HDD.

In keeping with the Unix way, I'm going to set up the HDD file system with the following commands...

mkdir home
cd home
mkdir rob

I now have my HDD with a /home/rob/ structure with which to work.

If you want to populate your HDD with some files from a different computer, you'll need to ummount it before you disconnect it (I'll cover LAN File Transfers soon).

I would use the command sudo umount /mnt/lacie_500_GB but you'll maybe have a different name for your drive.

If you want the drive to automatically mount at boot time, the /etc/fstab file will need be edited, and a line added for each partition and mountpoint. It's maybe a practical thing to do if you're not going to leave everything powered up, or to guard against the power being interrupted. If you get stuck with that, leave me a comment and I'll add the content here.