Using the Terminal
This is probably the most intimidating part of learning Linux. The terminal (Powershell on Windows) is where you access the operating system from command-line. You type in the instructions to the computer. There are no buttons, and no icons. If there is a graphical user interface (GUI), it is a grey window with red accents on a blue background. The key is understanding the syntax and knowing a few basic commands to navigate through the system.
Command-line and Linux 101 (optional read)
For beginners, you can use the following terms interchangably: terminal, command-line interface (CLI), bourne again shell (bash), shell, console. There are some historical and technical differences, but don’t worry about that.
Command-line is the process of interacting with the operating system through text. The terminal, shell, console are the ways of accessing the command-line as our modern operating system have a graphical user interface. Bash is a shell application but is also a language as it upgrades the shell experience with more commands. You can use any terminal and install bash.
It helps to understand the Linux file structure, as we are working with Raspberry Pi OS. In Linux, everything is a file. You know those folders on your Windows computer? In Linux, the directory is a file containing information of the contents so in a desktop environment, it looks like a folder.
Let’s take a look at a few important directory.
|Directory||What’s in it|
|/||This is called the root directory as there are nothing else “below” it. Think of the file system as a tree.|
|/boot||Files used for your system to boot.|
|/bin||Binaries – for system applications and utilities. These are considered essential for the system.|
|/sbin||System binaries – for the system administrative functions. I put it up here because it so closely relate to bin.|
|/usr||Read-only user application files, oppose to system application files. It may contain “/bin”, “/sbin”, and “/lib”. These are considered non-essential to the system.|
|/var||The writable version of “/usr” where your logs and some application configuration files are|
|/dev||Devices – where your drivers are – the files that allow the operating system to communicate with your computer hardware and peripherals.|
|/etc||Et cetera – miscellaneous files and configuration files.|
|/home||Home – your user files.|
|/root||The root user, also known as the superuser, is the system administrator and has access to the whole system. This is the home account for the root user.|
|/lib||Libraries that are used by your binaries (applications).|
|/media||This is where your additional drives and/or partitions are mounted. The system mounts these drives.|
|/mnt||This is where traditionally you, as the system administrator, mount a drive but these days, you can mount them anywhere.|
|/opt||Optional packages – only some applications use these.|
|/proc||Processes – remember how everything is a file? Processes are a file too.|
|/run||The temporary files for running applications and processes. These files are removed when the applications and processes are terminated.|
|/tmp||Temporary files that may be deleted during system restarts, or after a certain amount of time – which is why the directory “/run” exists; you don’t want to remove files used by running applications.|
Linux does not need file extensions for it to know what a file is as long as you have an application that knows what to do with the file. You may see what that means when we come across some configuration files later if you choose to go through those parts of the tutorial.
Now let’s learn some basic commands for you to navigate through the file system.
# Change directory (CD) $ cd <directory your want to go to> # Relative path from the current directory $ cd ./<directory you want to go to> # Absolute path from "/", which is the root directory $ cd /<directory you want to go to> # Absolute path from "~", which is the home directory $ cd ~/<directory you want to go to> # The absolute path for home, from root $ cd /home/<user>/ # Go back one directory $ cd ../
Sometimes, you are navigating through the file system and are not sure what is in the directory you are in.
# See what is in the current directory $ ls # This is the output directory1 directory2 file1 file2 file3 # You can see all of the directories and files, including the hidden ones $ ls -a
The “-” means to apply a condition, argument, or option. In the above case, the “a” means all. “ls” is the function name, a command. Think of a command as a verb. The condition, argument, or option are like indirect objects and direct objects. Not every verb requires one, and not every verb can accept one. Same with commands.
Computers speak a language, and languages have syntax. If you know what the computer is expecting from your inputs, you can understand how to communicate with the computer.
When you don’t know the command, and want to know what are some options, use the manual for system applications, or try “help” for other applications.
# For system applications "man" is the command to open up the manual for another command $ man <command you want to look up> # Most other applications you install will have a help file, accessed like this $ <name of the program> -h # Or $ <name of the program> --help
Okay, let’s continue learning a few more commands.
# To move (mv) a file $ mv <directory and file in you want to move> <directory and file you want to move to> # Example, move file1 from current directory to /home/user/Documents/ $ mv file1 ~/Documents/file1 # You don't have to be in the same directory as the file you want to move, as long as you know where it is $ mv ~/folderX/file1 ~/folderY/file1 # Note that Linux is case sensitive. File1 is not the same as file1. Both can exist in the same directory. # We also use the mv command to rename a file $ mv file1 file2 # file1 no longer exist, but file2 has the exact same contents as file1 # To copy (cp) a file is same as moving, except the original file is retained $ cp ~/directoryX/file1 ~/directoryY/file1 # Like the move example, but now you have a file1 in directoryX and a file1 in directoryY # To create a file, use the touch command $ touch ~/directoryY/file2 # A file named file2 is created in directoryY
When we need to edit the contents of a file, we need a text editor. Nano is my preferred text editor an it is built into the Linux operating system. Our Raspberry Pi, therefore, has it too. To use it, simply type “nano ” and the filename:
$ nano file1
We will practice using nano in later parts of this tutorial.
Remember, you can access any file no matter what directory you are in, as long as you type the full path. In the example above, file1 is in the current directory.
If you don’t know the full path of a file, you can type in the beginning of the directory or file name, and press “tab” on your keyboard to autocomplete the name. This makes typing a lot faster too. If there are two directories or two files with the same letters in the beginning, the first tab press won’t auto-complete, but a second tab press will list out all of the files and directories that have the same letters in the beginning. This is like performing “ls” and search while typing a command.
There are some files that are system files, or you may not have permission to. To read, execute, or edit these files, you would need to be the superuser, the system administrator.
# We can execute a command as a superuser with the sudo command like this $ sudo <command to execute> # Or we can become the root user like this: $ sudo su
If you use the latter method, becoming the root user means that anything you do will be under the account of the root user. So if you were to install an application, for example, it would install as the root user, and your user account will not have permissions. Worse yet, the application when running with the root user account will now have access to your entire system. If this application has a back-door or is a virus, it can do some damage do your system. One of the features that make Linux so safe is the user permissions for every file and directory. Anyways, I recommend only using the “sudo” command.
With this basic knowledge, you should be able to follow the rest of this tutorial. I will also explain more commands as we come across them.
The terminal you will be using will depend on your operating system. Mac users can find the terminal in “Applications”>”Utilities” or search it in the Launchpad. Linux users can find it in “Applications”. Windows user can use PowerShell by going to “Start” and typing it in search. From now on, I will only be referring to the console window as the terminal.
After we have the terminal open, this is where the rest of the tutorial will converge and your desktop operating system won’t matter.
The first line of code we will type to get into your Pi is:
$ ssh pi@raspberrypi
“ssh” is the command. “pi@raspberrypi” is the argument with two inputs combined: “pi”, the default username, and “raspberrypi”, the default hostname.
The first time we use login with a username to a hostname, your terminal will prompt you to allow the new connection. Type “yes” and “enter” to continue. This is a security feature as it will let you know that the key on the device is different than what you have logged onto before. If you had previously logged into the Pi, and you get this message, it means that another device may be impersonating your Pi by using the same hostname.
If you cannot connect to your Pi with the code above, try one of the methods in the optional read below.
Look for my Pi’s IP (optional read)
First, we find out what the Pi’s Internet protocol address, which I just refer to as IP.
$ ping raspberrypi # You should get an output on the screen like below: PING raspberrypi (192.168.x.x) 56(84) bytes of data # Your Pi's IP might look different and be 10.x.x.x or 172.x.x.x
If the you have successfully found your Pi, we are done. Your computer would be pinging your Pi so press “Ctrl” + “C” and on your keyboard to get out.
Let’s continue on for those who were unsuccessful.
Download Fing from App Store or Google Play on your mobile device. This is a scanner to identify all devices on the same network, so of course, make sure your mobile device is connected to the same Wi-Fi as we had in the setup.
Another way to find all of your devices and IP’s on your network is to go to the router. I avoid giving router instructions because every brand and generation of models have a slightly different portal. Please Google this one on your own.
After this point, if you still cannot find your Pi on the network, I would double-check the wpa_supplicant.conf and the previous steps.
Once you find your Pi’s IP, use this code below:
$ ssh pi@<your ip address> # Example: ssh firstname.lastname@example.org
To terminate your ssh session:
Congratulations! You can now skip or proceed ahead to any parts you’d like.