Basic Linux Guide
Summary
Linux is a wonderful and free, open source Operating System which can be run on your computer as a replacement to windows. Due to the development nature of Linux, it is highly configurable and adaptable to many different cases, which is why it is prevalent on many systems such as phones, 3D printers, routers, and the Raspberry Pi.
Below is a simple guide to give you the quick basics to what you need to know in linux in order to get to what you need to do. As a basic starter, there is plenty more information than what is presented here, so we highly encourage you to seek out information and experiment with what you know in order to learn and enable yourself to do much more with the resources that you have.
As the Raspberry Pi will be many people's first experience with linux, the information presented here will be focused on raspberry pi when needed; however a lot of the information here is general in scope and can usually work the same on any linux system.
Check out the Raspberry Pi Important Info section!
Table of Contents
- Getting help and more information
- The boot directory
- Config.txt
- Cmdline.txt
- System drivers
- Editing in the system and recovering from failure
- File structure compared to windows
- Copying files
- Moving and renaming files
- Removing files
- Making directories
- Sudo
- Permission errors and checking ownership
- Installing maintained software
- Installing from a package
- Downloading code from Github
- Archive files
- StdOut StdIn and StdErr
- Redirecting
- Grepping and filtering
- Dmesg
- the list commands: lsusb, lsblk, lspci
- System commands
If you find yourself stuck on any particular command; here are a few ways to get help with your task:
Try the --help or -h or man commands
These are switches that are enabled in many software packages to give you information on how to use it; for instance, if you have trouble with the program apt try using apt --help
Google through the Stack exchange
- A great resource in question/answer format. Most google searches will link back to here. Always search for your question first before asking. If you really have to ask, it is often useful to ask for help with what you are originally trying to do, rather than to ask for help trying a solution you have already determine.
A great resource for a different version of linux. The information contained in here can be immensely helpful in regards to certain programs, but it is important to remember that the information is not a direct 1-to-1 translation for your linux system, unless you're running Arch Linux.
By default, Raspbian (The version of linux for the Raspberry Pi ) comes with the terminal window lxterminal, this is similar to the terminal app on both Mac and Ubuntu, which is yet another version of linux. While all the programs might be different, the underlying command process is the same.
Raspberry Pi Important Infomation
The Raspberry pi has been one of the most popular Single Board Computer chips in the IoT Maker Space, causing Raspbian and Linux ARM to increase in popularity. With the popularity, there has been a number of how-to guides and tutorials that are for many specific cases, that might work for others, but might not work for you. The underlying Linux system runs the same, but with the flexibility of Linux, there are many different ways to do the same task.
It is important that you understand the basics of linux and understand what the overall intent of the guide or tutorial is, rather than trying to blindly follow it and hope for the best.
The Boot directory is a FAT32 partition that can be read on Windows if you ever find the need to plug your Raspberry Pi SD card into a windows machine. It has the core elements of the Raspbian OS and some configuration files; two of which are config.txt and cmdline.txt.
This file emulates a sort of "bios" system for your rpi, and can set many of the system parameters such as:
Output resolution.
Audio
Connection information, including GPIO Access.
Drivers and Memory usage.
There is generally too many options to go through, but the best write up is probably available here, for in-depth options:Â https://elinux.org/RPiconfig
If you are trying to set up a new display device, or something such as our XC9024 make sure to try and read the manual of the product, which will include the correct config.txt options.
These are options that are passed to the Linux Kernel, and have some options including terminals and consoles for you to type into. Generally this is a bit too low-level for any reasonable result in tinkering, but you can read more about it here: https://elinux.org/RPi_cmdline.txt and https://elinux.org/RPI_BCM2708_Parameters#module:_sdhci-bcm2708
Linux drivers are called "modules" and are managed by a "Device tree" in Raspbian; You can read more here, but you'll find in config.txt that you can already see some dtparam and dtoverlay commands.
https://www.raspberrypi.org/documentation/configuration/device-tree.md
Check what modules your devices are using before changing your config.txt - if you use some script or "copy-paste" commands to make config.txt for you, then you might find your old config.txt irreversably deleted!
If you find the graphics or the screen of the Raspberry Pi not quite working, you can use the "Change terminal" hotkey, which is Ctrl+Alt+F2 though to F7 - these can give you a text-based terminal and is a great way to recover your system, even when it seems to be completely frozen!
Whenever you encounter trouble, try Ctrl-Alt-F2 to bring up an alternative terminal, and log in. Here you can run some System commands to try to rectify the issue, or you can modify your /boot/config.txt scripts to reboot the system with a new configuration.
To edit files, there are a few text editors; but nano is known as one of the easier ones. you can edit files with nano /boot/config.txt ( See 'root' if you have permission issues ) and edit your file.
You can then press Ctrl-X to exit; it will ask you to save, where, and whether you want to overwrite the file.
The Command line
The number one difference people notice between linux and windows, is that linux has a heavy preference for the command line. That is not to say that the command line is needed for all cases; but as you'll soon see, the command line is a very simple and very fast tool that not only runs fast, but can sometimes be faster than clicking a button.
Commands are usually written one per line, with the first word being the command, and the rest are arguments to give to the command. After typing in the command, pressing enter will run the command.
Switches or options for the command are usually prefixed with a dash, that can give specific information to the command.
The commands we will be learning about in this document are:
Command | Use and reason for inclusion |
---|---|
File commands: | For moving around files and folders |
Permission commands: | For managing linux permissions |
Install and archive commands: | For installing software |
Piping and redirection: | For managing textual information |
Greping and filtering: | For filtering texual information |
System Diagnostics: | For figuring out what went wrong |
Before we get too far into the commands, let's first go over some of the basic differences between Linux.
If you already have a bit of computer knowledge, you should know about the file structure on a windows PC:
Directory | Contents |
---|---|
C:\ | This is the hard-drive |
C:\Windows | This is the Operating system |
C:\Program Files | This is where Programs are stored |
C:\Users or C:\Documents... | This is where the user's "My Documents" are stored |
The structure in a Linux system is fairly different, with more nuances and less room for error in the linux system:
There is no C:\ - the base of linux starts at / and is not a hard-drive.
There is strict importance on using / instead of \ - Windows will accept either.
Filenames are case sensitive - so a folder named Users is different to users
Programs are split over multiple directories. For instance: all the config files go in one directory, the executable files go in another.
Files that start with a . in their name, are automatically hidden from most commands unless explicitly mentioned. These are usually called 'dotfiles' and are on the home directories.
More often than not, configuration is done by configuration files, there is no registery like on windows.
To see what a typical linux filesystem looks like:
Directory | Contents |
---|---|
/ | This is the Root directory, analogous to Window's C:\ - everything starts from here |
/bin | System binaries, or system programs, generally not advisable to touch anything in here. |
/etc | System configuration files; usually you would have to go in here to play with default settings, but not always easy to reverse if you make a mistake |
/media | USB sticks and other removable media are placed in here. |
/dev | System devices, which is usually not too helpful for terminal use. However, other programs such as arduino will want to know about the /dev/ttyUSB0 or similar to communicate to your hardware. We'll explore in the system information section. |
/usr | Not Users as in you or I, but rather Userspace as in programs that are not part of the kernel or core system |
/usr/bin | Userspace binaries; most installed programs get put in here |
/usr/share | Icon files, images, resources, that can be shared amongst different software packages |
/home | the HOME DIRECTORIES for different users. This is where all your personal files, downloads, documents, customisations and config files go, and is highly worth backing up regularly before things go wrong. It has a shortcut of ~ |
There are more directories in the linux system but this should suffice for an understanding of the underlying system. By way of example, if you were to install the webrowser firefox you will find the executable program itself was stored in /usr/bin and the configuration file was in ~/.mozzila (~ means 'my home directory' in linux)
File commands
Before going on to the various commands, there are a few short-cuts that can help save time:
Remember all filepaths start from the root, / then are built from that point forward.
Your home folder will have the shortcut of ~ as long as you are logged in as your user.
Starting a new terminal window starts you in your home folder, or ~
../ Means 'up one directory' from the current position. ./ means this directory
You can <tab> complete and use the wildcard character *.
By simple way of example, you can run the following commands to navigate around the linux command and understand the layout of the system:
Start by opening a new terminal, and type these commands as way of practise and understanding:
whoamipwdlscd /lspwdcd /usr/binpwdcd ../pwdcd ~pwdls ./
For Raspbian, as the pi user, home would be /home/pi
~/Downloads would be the same as /home/pi/Downloads
The basic command is cp with two parameters, for source and destination:
cp file1 file2#file2 is now a duplicate of file1
You can copy directories by using the -r switch, for 'recursive.'
cp -r directory directory_backup
Of course, you can move files around the filesystem with this method as well:
cp file1 -v directory/two/layers/deep# A copy of file1 is now placed in directory/two/layers/deep
-v is a simple switch to display output (verbose.) you can also -f to force if there is any warnings.
Moving files is the same as the copy command, except the original file will be moved to the new location and you do not need to specify -r. This will allow you to rename files as well:
mv old_filename new_filename#rename filemv file directory#move file into directorymv file directory/new_filename#move and rename as one action.#IF new_filename is a directory, it will move into# that directory as the old filename instead.
The command to remove files is rm but, as a way of security, it will not remove directories unless you specify -r for recursive. You can also remove empty directories with the rmdir command.
rm file1 file2 file3#remove file1 and file2 ..... etc.rm -rv directory#remove the directoryrm -rf directory/*#remove all the files IN directory, not the directory itself, using the wildcard character.
To make a directory, specify one or more directory names to mkdir :
mkdir dir1 dir2mkdir ../dir3mkdir ~/Documents/NewProject
If you try to make a directory such as /yourdirectory you'll get a permission error, read about permissions below.
Permission commands
Linux has a fairly simple permission structure based on 3 groups of people: the owner of the file, the group of the file, and everyone else. Permissions simply permit or deny access to any resource; there is no concept of an access level; If the group is allowed access to a file, and you are a member of that group, then you'd be allowed access to that file.
There is a global, root username, which does have access to the whole system no matter what permissions are set. This is the super user, and allows for many system changes.
Important There is a big warning sign with root access: Anything you tell the Linux system to do, it will try to do provided that you have access. When you have root you will always have access.
To run a command as root, you can use the sudo command. Please make sure that you first, cannot run the command as non-root, as mistakes are common and sudo has the potential to break everything in your system.
If you want to run multiple things as root, you can use the su command, to switch user. By default, this switches to root but you can also specify other usernames.
If you find that you cannot access something, or you cannot save your files where you thought you could, you must check the permissions of the file.
To check the permissions, use the ls -l command, for "list files in long form"
Regarding permissions, there are 3 main factors which you can see above:
The Modeline of the file or directory
The Owner of the file or directory
The Group of the file or directory
Firstly, let's look at our example and what we are trying to do. When we run cat somefile.txt we get a permission denied error, but we don't get that with sudo. The short form of solution is to use sudo for everything, but that will inevitably do something bad if we make a mistake in what we type, and defeats the purpose of protection in the first place.
Observing the modeline for the somefile.txt we can see that it has w for owner, and nothing in the groups or "all" columns. We can also see that it is owned by a username john and it is in the group of users
There's mutiple solutions to solving this:
We can switch user to john, su john
We can change the owner of the file, chown pi somefile.txt
We can change the modeline of the file using chmod
to make it so the 'users' group has read access chmod g+r somefile.txt
to make it so everyone has read access chmod a+r somefile.txt
We ended up giving everyone read access to john's 'somefile.txt' - we also do it as root, as we don't have johns login password.
sudo chmod a+r somefile.txt
It should be obvious why root is a fundamental security concern.
You can also see the x permission, this stands for eXecute, and allows files to be ran as programs. Without the x flag the file is just a collection of binary data. When the x flag is enabled, Linux will read the file and try to run it, attaching it to the system processes.
You can enable execution for owner, groups, or all, by using the a+x argument to chmod
(chmod and chown both have an -R flag, for directories and recursion as well. Keep this in mind if you have entire directories that you need to fix the permissions to)
Install and Archive commands
The strength of the open source community is the wide variety of software packages that are able to be installed. Usually called a 'package' due to the multiple files contained within, software packages exist for anything relating to how your computer behaves. Software packages are usually installed by a package manager but it does not have to always be the case. As you'll soon see, using a package manager makes it very easy to set up new software on your linux system.
Raspbian is a Debian flavour of linux, along with Ubuntu which is popular for desktop systems. The package manager in Debian styled distros is apt and you can search, install, remove and update through the apt command (previously it was apt-get but they are interchangable commands now.) Maintained software is software that has someone maintaining it's position on the Debian software registry, applying software updates and ensuring that it works with other Debian software.
It is best to look for the program that you want to install, rather than to look for generic terms such as 'text editor.'
You can also install multiple programs in the same invoccation through the apt command, meaning a complete new system can be created with only a few lines of terminal commands.
Both updating and installing software will have to be done as root.
apt search firefoxapt install vim screen git# "vim" "screen" and "git" are 3 seperate software packages
System updates and upgrades are done through a two step process. Firstly, the package list is updated, which will point to the download locations of all the new versions of software, then the packages themselves will be updated based off that new software version.
With updates, it's possible that old dependencies might not exist anymore, so you will be told as such through apt while you're updating.
apt update# updates package listings apt upgrade# upgrades the software packages you have installed,apt autoremove# removes packages that you don't need on the system
There are two modes of uninstalling software: remove or purge. The remove command will simply remove the binary, where purge will also remove all configuration files; This could make it easy if you want to start again, or if you want to uninstall and install again on a later date.
apt purge filefox#purge will also remove all the configuration files.
Debian distros have packages in the form of *.deb files, which is what apt uses under the hood. Installing from .deb is not recomended, as it skips a lot of the system checks and is a fast way to get into dependency hell.
To install from a deb file, use the command:
dpkg -i file.deb#perhaps you will also have to "fix" the system afterwards:apt install -f
More recent versions of apt can install them and perform the checks, so experiment (notice the ./ infront, to signify "from this folder" ):
apt install ./filename.deb
Here at Jaycar, we are embracing the GitHub platform as a way to easily share our projects and code. GitHub provides a great way to share and collaborate on code, and will form the basis of it's own article soon.
Our github account is Jaycar-Electronics
For now, the basics of git is to download and update software code. Code and projects are stored in "repositories" which you clone onto your machine and pull changes into.
It's important to note that github repos are more of a "living system" and are much more dynamic than regular maintained software. If the repos are changing often, you might pulling down a version of the code that does not yet compile, depending on developers of that repository. If you find that it does not work as expected
git clone https://github.com/Jaycar-Electronics/GPS-World-Clock# this will download the project files and store it in a folder named GPS-World-Clockcd GPS-World-Clock# Now we are in the project foldergit pull# "pull" updates from github for this repository
Most Repos will have a README.md file located in the root of their folder, which you can simply read with less readme.md or less README.md ( use the tab key )
There's a few different types of archive files on linux, which depend on where they came from. The most common in the linux universe is the .tar.gz format; but more and more are coming in the 7zip format as well:
Filename | Use this command to extract |
---|---|
file.tar.gz | tar -xzf file.tar.gz |
file.tgz | tar -xzf file.tgz |
file.zip | unzip file.zip |
file.rar | unrar e file.rar |
file.7z | p7zip -d file.7z |
Some archives will extract into their own directory, while others will extract into your current directory, so make sure to keep track of where your files have gotten to and if possible, move the archive into it's own folder as a just in case.
Piping and redirection
The power of linux command line lies in piping and redirection of information; entire programs have been ultimately replaced by single-line commands that can do the same thing much faster than before. While systems are getting less and less in need of such command line usage, and text editors are becoming more intuitive to use, it could perhaps be falling out of fashion.
When a program asks for input, it does so through what is called the Standard Input Stream, or stdin. Similarly, the output it presents to the terminal upon completion is the Standard Output Stream, or stdout. A program can ask for your name, then read whatever response through stdin, before using it again.
> What is your name?< John > Hello John
This input / output system leads way to chaining multiple programs together; for instance, if another program would OUTPUT a name, which could then feed into a program that requires a name as input. This is possible with linux pipes, used by the | character (read: break character, the key above enter on us-styled keyboards.)
It is easier to see the effects by example. Consider the programs cat which reads a file and prints it to stdout - as well as wc which gives a word count. We can do the following:
cat somefile.txt | wc# this would count the number of words in the file
The cat somefile.txt is redundant in this example, as you can also call wc somefile.txt to the same effect. However, if the information that you want to use wc on is not located in a file, but on a web resource:
wget -O - https://google.com | wc
This will get you the number of lines, characters, and bytes for the google homepage HTML code.
wget is a program that allows for data to be downloaded from the web. use -O to specify a filename and - as a filename to specify stdout.
If you have been paying attention, you could also do wget https://google.com -O gooogle.html; wc google.html to the same effect.
If you have ran this command, you will find that it also spits out many things to the terminal, such as "Resolving google.com ..." This is debug information that you might or might not need, and it is fed through another standard output stream, called the Standard Error Stream, or stderr.
You can redirect the either of the output streams using the > character. For stderr you can redirect via "2>" such as the following example:
wget -O - https://google.com 2>/dev/null | wc
We are redirecting the stderr stream into a file located at /dev/null which is a device file that acts like a black-hole. Basically cancelling and doing nothing with any of the stderr. You can also specify a file instead of /dev/null by putting a filename instead:
wget -O - https://google.com 2>debugInfo.txt | wccat debugInfo.txt
this can work with stdout and has an append function as well:
dmesg > logfile.txt # redirect the output from dmesg into the file 'logfile.txt'lsusb >> logfile.text# append the output from lsusb into the file 'logfile.txt' - note that it's still a redirection.
This is very useful when you need to save the output into files to email later.
Extracting and sorting information
There are a few options to slim down on the amount of information that flows through stdout. Below is going to be a fairly comprehensive example of how you can chain together multiple commands to filter out the information that you need. This is a seperate example from the others, so feel free to continue reading after this and come back at any point.
As the more common extraction tools, head and tail extract only the top or bottom 10 lines of any input; you can also change the number of lines by the -n switch:
dmesg | tail#the last 10 lines of system diagnostics dmesg | head -n 3 #the first 3 lines of system diagnostics
You can filter along the lines of text by piping the output, which will treat each new line as a new string, to other sorting and extracting programs.
Suppose you have a csv file of scores that you want to sort by who has the best scores, like below:
# scores.csv1,amy,4002,john,6503,patrick,2564,suzanne,2465,tim,2006,mel,500
You can use the linux command sort to sort the information based on the 3rd value, saying that each delimiter of entries is a comma, and you want to sort the lines based on the 3rd field, like so:
cat scores.csv | sort -t ',' -k 3 -r# output scores.csv into the sort program, specifying what seperates the value, which value, and which order.
-t for specifing the comma, -k for specifying what field you want sorted by, and -r to sort in descending, rather than ascending order.
You could also further cut the output, to only get the 2nd field, which are the names:
cat scores.csv | sort -t ',' -k 3 -r | cut -d ',' -f 2# as before, but now feed that into the cut program, specifying where to cut (each comma) and what to keep (2nd block)
Here we specify -d for the delimiter as a comma, and -f field as 2.
To get only one name, use head -n 1
cat scores.csv | sort -t ',' -k 3 -r | cut -d ',' -f 2 | head -n 1# only get the top single line from the output of all the other commands.
which you should see that will return just the name of "john" (excuse the lack of capitalisation).
grep is one of the most popular and known tools in the linux developer scene, and is a great tool to know about when programming. It works off the basis of 'Regular Expressions' or RegEx to define search patterns and look for patterns in text. GREP is a very simple tool to explain, but you will find regex available in almost every programming language as a feature of the language, as well as a tool outside of the language. If you are thinking about becoming a software developer in the long term, or atleast tinkering, RegEx can save you a lot of time.
The basic pattern to grep is grep [OPTIONS] pattern [FILES] and can be thought of as a pattern matcher.
Consider a file like this:
this is a long filewith some very basic informationthat could be formed over many different lines and have differnt types of information like1234 numbers 0987.654321qor other patterns1234asdf5678asdf123abc456def
Try some of these for yourself: ( for ease, we are going to use pipes, press the up key to get the last line from terminal history)
cat plaintext.txt | grep 'this' #look for the word 'this'cat plaintext.txt | grep 'that' -n #look for the word 'that' and give line (n)umber. cat plaintext.txt | grep 'that' -C2 #look for the word 'that' and give (C)ontext for 2 lines, or try (A)bove and (B)elow.cat plaintext.txt | grep '[0-9]' #look for any line that has a number between 0-9 in it. cat plaintext.txt | grep '[0-9]{4}' -P #look for any line that has numbers [0-9] 4 times in a row. Use -P to turn on regex.
what might seem a bit trivial to look at quickly becomes a tool while developing. For instance if you're dealing with some Arduino code, you can quickly find where the setup() function is by doing the following:
grep 'setup()' */* 2>/dev/null -n#the "*/*" means all files in all directories. redirect any warnings or errors into the black hole of /dev/null. -n switch gives us line number.
Of course, the possibilities are endless once you start to get an understanding of linux:
dmesg | grep 'sd[abcd]'#Get kernel diagnostic information for any of your harddrives.
System Diagnostics
Thankfully there's a few programs in linux that can help you figure out what has gone wrong when something goes wrong. Below are some simple commands to help with diagnosing issues, and tied with the Grepping and Filtering commands mentioned above, it should make for easy work of finding important information to help with your issue.
There is an "interactive" program called less that can allow you to scroll through information. If you have a wealth of information but no mouse to scroll through, pipe it into less so that you can do more. Give it a try with dmesg | less (use 'q' to quit)
dmesg is the output straight from the kernel buffer. It can provide a large amount of low level information about the hardware and core system changes to your device. A useful feature of dmesg is the --follow switch, which will only update when changes occur. Try the following code, then when it is running, connect and disconnect your mouse or keyboard:
dmesg --follow
From here you can see that there are some strings saying USB, idVendor, etc, which might be useful to grep for earlier in the dmesg logs.
For instance, if you had a problem with USB:
dmesg | grep 'USB' | less
There are a few commands that start with ls and can list different parts of the system,
lsusb will list usb devices
lspci will list hardware devices, such as video and WiFi cards.
lsblk will list block (storage) devices, such as harddrives, CD Drives, and USB thumb drives.
Looking at these listings can help with determining whether the problem is with the hardware, or if the hardware is even working at all.
When all else fails, there's some commands you can try
killall programName
Attempt to terminate every running executable called programName
sync
Syncs data to thumbdrives and harddrives, if your files are not unmounting properly.
Ctrl-Alt-F2Â hotkey
Changes to virtual terminal 2. ( change back to graphical terminal with F7 )
Similar projects you may be interested in