Coding truck

Make a Raspberry Pi Linux Server

A Raspberry Pi is a great little single board computer.  It is a little workhorse of a machine that fits in the palm of your hand. There are lots of great projects for it that let you:

  • Create music
  • Emulate video game consoles
  • Stream video. 

Today we are going to look at another application for the Raspberry Pi. We are going to make it into a little linux server. Yup the ones that you log into with the terminal window. 

It may not be the most appealing use of the $35 computer, but it is super handy.  You can use it as a little web server, a database, or plenty of other things. It's also a great way to get experience with the command line, SSH, and Linux administration. 

Did I mention that this powerful little computer is only $35?

Getting supplies

You will need to get a few more things than the Raspberry Pi board. Here is a list of things that you will need.  You can usually find these in a kit on Amazon, eBay or your electronics shopping site of choice for $60 - $75.

  • Raspberry Pi board - This is the computer itself
  • Raspberry Pi case - A nice little plastic case to keep your pi safe
  • Micro SD card - Memory card that will act as the hard drive for your pi.  You will want at least 32gb. 
  • Micro USB Power Supply - You probably have one of these lying around that you may not be using. You want to make sure it puts out enough power
  • Raspbian Lite OS - A lightweight linux distribution without a GUI
  • Etcher - An open source project that will get the OS onto the micro SD card
  • Another computer - Perhaps even the one you are using right now. The instructions should work as is for Mac and Linux. If you are using Windows it should work fine with a bash terminal such as the one provided with Git Bash.

Needed only during setup

There are a few more things that you will need during setup. Once your pi is setup and 

  • Network cable
  • HDMI cable
  • USB Keyboard
  • USB Mouse (you may not need this)

Flash!

The first thing that we need to do is to get the copy of the Raspbian Lite OS onto the micro SD card. 

Etcher writing image to micro SD card

Assemble!

Now it's time to put our raspberry pi together. Some kits will come with tiny heat sinks to attach to your pi. If you don't have these you should still be ok.

  • Attach heat sinks
  • Put pi in case
  • Insert micro SD card containing a copy of Raspbian Lite OS
  • Attach to network with network cable
  • Attach to monitor with HDMI cable
  • Attach USB keyboard
  • Attach USB mouse (if you like)
  • Plug in power supply and attach to raspberry pi

Boot!

You have plugged in your pi and it will have some lights turn on. On your screen you should see 4 raspberries, a rainbow square and a bunch of text flashing by.

Ultimately you will be faced with a login prompt

raspberrypi login: _

Login with default credentials 

  • username: pi 
  • password: raspberry

You should be greeted by a prompt

pi@raspberrypi:~ $ _

Open the raspberry pi config by typing at the prompt:

sudo raspi-config

Now press enter. You will see a blue screen with a menu of 9 options

1) Change User Password 
2) Network Options 
3) Boot Options 
4) Localisation Options 
5) Interfacing Options 
6) Overclock 
7) Advanced Options 
8) Update 
9) About raspi-config

Config!

We have entered the Raspberry Pi configuration tool. It has a nice little interface for performing common configuration items. You can navigate the menu with the up and down arrow keys. 

  • Esc will return you to the previous menu
  • Enter will execute your current selection
  • Tab will cycle focus to buttons below the menu and back

Change User Password

Press enter to select the first option. You should get a message that says, "You will now be asked to enter a new password for the pi user". 

Press enter and then type in the superuser password. You will have to make one up. It should be something secure. Don't worry, you won't need to be using it very often.

Retype the password when prompted and press enter. You should see the message, "Password changed successfully"

Network Options

Now select the second menu item, "Network Options" and press enter. You should see the following options:

1) Hostname 
2) Wi-fi 
3) Network interface names

Hostname

A hostname is the name that you will call your raspberry pi. It is currently named raspberrypi. Let's change that.

Select Hostname from the menu and press enter. You will see a long message about how to select a host name. 

Basically, you have to use lower case letters a through z or numbers. You can also use a hyphen though a hostname cannot begin or end with a hyphen.

Enter your hostname. For this host, I chose "jester". Press enter.

Wi-fi

Most likely, you will want to access your raspberry pi over wi-fi. To do this, you will need to configure wi-fi. For this step you will need to know the SSID (name) and password to your wi-fi.

After selecting a hostname, the config tool put you back at the main menu. Choose network options and then wi-fi

It will ask you which country you are in, select your country and press enter. If you are in the United States, you will find it near the bottom of the list. You can press a key to scroll to countries beginning with that letter. For instance, the "u" key will scroll to the countries starting with the letter u.

You will get a message stating that your wi-fi country has been updated, followed by a message asking for an SSID. SSID is the name of your wi-fi network. 

After entering the SSID and pressing enter, you will be prompted to enter the wi-fi password. Do this and press enter again.

Localisation Options

We will skip boot options as we will not be modifying the default configuration there. If you accidentally select boot options, you can return to the main menu by pressing the Esc key.

In the localisation menu, you will see the following sub-menu:

1) Change Locale 
2) Change Timezone 
3) Change Keyboard Layout 
4) Change wi-fi Country

We have already set the wi-fi country, but will need to select the other 3 options one at at time.

Change Locale

Selecting change locale and pressing enter will present you with a long list. The list is the possible encodings and locales. You can scroll through this list with the up and down arrows on your keyboard. 

The default is set to en-GB.UTF-8 UTF-8

This is British English with UTF-8 encoding. If you are like me you will want to change this to American English with UTF-8 encoding. To do this you will need to unselect the current setting. Scroll to the row

[*] en_GB.UTF-8 UTF-8

Once selected, press space bar. The asterisk should disappear. Now scroll to the row en_US.UTF-8 UTF-8 and press space bar. It should now look like this

[*] en_US.UTF-8 UTF-8

Once complete, press enter. You will get another prompt about configuring locales, use the arrow keys to scroll to the row, en_US.UTF-8. Press enter. You should see a "Generating locales..." message at the bottom of the screen and return to the main menu.

Change Timezone

Return to the localisation options menu and select change timezone. Choose your timezone and press enter. I am in the US central timezone so I selected US from the first list and Central from the second list.

Change keyboard layout

If you have a special keyboard select it from the list. If not you want the PC generic keyboard. It will then prompt you with a series of options. Unless you know that they are incorrect, you want the default values.

Interfacing Options

Select the interfacing options and you will see the interfacing options menu. We only want to turn on SSH so that we can access the pi from another PC. To do this select the SSH option and press enter.

SSH

Would you like the SSH server to be enabled?

Yes, yes we would. Select yes and press enter.

The SSH server is enabled

Thank you very much

Advanced Options

We are almost done. There is only one more option to set. Select advanced options and press enter to go to the advanced options menu.

Memory Split

We won't be putting much of a strain on the gpu and would rather allocate that memory to the cpu. Select memory split from the menu and press enter

When prompted reduce the memory used by the gpu to 4MB.

Finish

We are all done with the configuration tool! time to use the tab key to select finish and press enter

Would you like to reboot now?

Choose yes and press enter. Your pi will reboot.

Login

We have changed the host name to jester so the login now says

jester login:

The username is still pi, but we have updated the password. Login with 'pi' and the new password. you should see a prompt of your username followed by your pi's hostname

pi@jester:~ $ _

Create your user

We are not going to be using the user pi, let's make our own. I will call my user 'j' but you can use whatever username you want.

sudo adduser j

It should show you something like the following ending with a prompt for a new password. Enter the new secure password and re-enter it.

Adding user 'j' ...
Adding new group 'j' (1001) ...
Adding new user 'j' (1001)  with group 'j' ...
Creating home directory '/home/j' ...
Copying files from '/etc/skel' ...
New password: _

Once entered it will ask you for some information, you can fill it in or leave it blank. When prompted if the information is correct, type 'Y' and press enter. You created a user for yourself.

We will be using this user and want it to be able to use administrative commands (when we want it to). Next we will give it rights to do so by typing: 

sudo usermod -aG sudo j

Increase swap space

Sometimes we need more memory than the raspberry pi can provide. When this happens the pi can us hard drive space to hold the information rather than crashing. Depending on what you use your pi for you may need to increase this space. You can do this with the following commands.

sudo nano /etc/dphys-swapfile

This will open the nano editor with the file /etc/dphys-swapfile in it.

You want to edit the line

CONF_SWAPSIZE=100

Edit it so that it says

CONF_SWAPSIZE=1024

Do not edit anything else. Once complete press CTRL-O,

and CTRL-X to save and exit.

Now type the following commands

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Test wi-fi and config

Next, disconnect the network cable from the raspberry pi and test the wi-fi connection

Unplug the network cable.

Restart the raspberry pi by typing.

sudo shutdown -r now

Press enter and your pi should reboot. At the login use the login of the new user you created.

Once logged in I see the prompt

j@jester:~ $ _

Now let's test the wi-fi by using the ping command to see if it connects.

ping -c 4 codingtruck.com

You should see something that states something like "64 bytes from..." repeated. If it does not stop press CTRL-C to kill the command. If you don't see this output, you have a problem with your wi-fi configuration.

Get your ip address

Type:

ifconfig 

Press enter and you should see results with 3 sections

  • eth0
  • io
  • wlan0

In the wlan section, the second line will say something like

inet 192.168.0.2 netmask 255.255.255.0 broadcast 192.168.0.255

The first set of numbers, 192.168.0.2, may be different in your case. This is the ip address of your raspberry pi. Write this down.

Intermission

We can now shutdown our pi for the next phase of configuration

sudo shutdown -r now

We verified that wi-fi works, now we can make the raspberry pi headless. Unplug the keyboard, mouse and HDMI connection from the raspberry pi. This should only leave the power.

If you want to move the raspberry pi to another location now is a good time. Once moved you can plug it back in.

Because your pi is on wi-fi you should be able to connect to it using SSH when you turn it back on.

In another post we will discuss some more steps that you can take to improve the functionality of your pi. 

For now, you can start playing with your pi.  What do you think? What do you plan on using it for?

Next
Prev