Introduction to Linux

Posted by David Harding, Will Binns

Introduction to Linux

Many of the platforms supported by 21 are Linux-based or use Linux/Unix-style tools. This includes our Amazon AWS EC2 and VirtualBox development environments. This tutorial provides you with a basic introduction to the Linux command line to help you learn the basics.

Logging into remote Linux

Many Linux systems you use will be remote; that is, they won't be your main desktop. In order to access them, you will need to login to them from a Mac or Windows desktop. The instructions below should help you do that

If you use Mac OS X

  1. Get the username of the Linux user (not 21.co user) you want to login as. The links below can help you how to find that information based on what product you use:

  2. Get the IP address or DNS name of the Linux server you want to log into. The guides above will help you with that as well.

  3. Open the Terminal by typing "terminal" into Finder.

  4. In the terminal, type the following command but replace USER with your username and IP_ADDRESS with the IP address or DNS name of the Linux server.

ssh USER@IP_ADDRESS

If the command is successful, the command prompt (the text to the left of the cursor where you type text in the terminal) should be different than it was before. This means your commands will be run on the server instead of your Mac.

If you use Windows

  1. Download PuTTY, the ssh client for Windows.

  2. Get the username of the Linux user (not 21.co user) you want to login as. The links below can help you how to find that information based on what product you use:

  3. Get the IP address or DNS name of the Linux server you want to log into. The guides above will help you with that as well.

  4. Open PuTTY from your desktop and fill in the IP address or DNS name of you Linux server on the main screen:

    Putty setup

  5. Click the Open button. When prompted, enter the username you looked up earlier.

If the connection was successful, you should see a window waiting for your input.

Command line basics

After logging in, you should see a prompt that looks similar to the following (although it may look a bit different, and the user and linux parts will probably be different):

user@linux:~$

This is called the command line prompt. When you see it, you can type in a system command. Let's type a simple command:

ls

That command is the list command; it lists all the files in the current directory. Let's change directory to the temporary (tmp) directory:

cd /tmp

The slash before "tmp" indicates that this is a full path. Let's make directory and then change directory into it to see a relative path:

mkdir test
cd test

Notice that this time we used change directory without the leading slash, meaning we entered a child of the previous current directory. Can you guess the full path for the current test directory? The answer is below:

/tmp/test

All files and directories created in the /tmp directory are automatically deleted when your computer reboots, so this is a good place to experiment but not a good place to keep any real work. Let's go back to your home directory; you can change directory to it from anywhere by typing the following command:

cd

Notice that there's nothing after the cd command; we call the parts after the command "parameters" or "arguments", but in this case there are no parameters.

Now that we're back in your home, let's create a file using your text editor. There are several text editors commonly used on Linux, but for new users the recommended one is called nano:

nano test.txt

This should change your screen from the prompt to a screen that looks similar to the following screenshot:

Nano

You can start typing in this screen and the text will appear:

Text

You can save and exit the file by following the commands printed at the bottom of screen. The ^ character stands for press-and-hold the CTRL key while pressing the letter indicated. Here's a table:

Action Nano description Key combination
Get help ^G Ctrl-g
Write out (save file) ^O Ctrl-o
Read (open) file ^R Ctrl-r
Exit nano ^X Ctrl-x

After saving and exiting the file, you will be returned to the command line. You can display the file using the catenate command:

cat test.txt

Since we don't need this file and don't want it cluttering up our home directory, let's remove the file (note, double check that you typed everything correctly when you run the remove command; by default on Linux there's no way to undelete a file):

rm test.txt

More advanced commands

This section will describe some commands and control operators that you will commonly see used in other documentations on 21.co.

Bash comments

Sometimes you will see something similar to the following lines in our documentation:

# This is a comment

The # indicates the start of a comment, which continues until the end of the line. The content of the comment is not a command to be run or anything that is automatically used. It's just a small bit of text to provide extra information to humans like you.

python / python3

Most of the examples on this site are written in the Python programming language, specifically the third version of Python (python3). There are three ways we commonly call Python:

The first and most common way is with the same of a file containing a Python program (also called a script):

python3 program.py

That will run the program and print its output (if any).

The second way is opening the Python Read-Eval-Print-Lopp (REPL) where you type a line of code, Python reads it, evaluates it, prints the result, and then loops back to read the next line you type. To start Python in REPL mode, simple start it with no parameters:

python

When Python is reading input, the line will start with >>> and the lines it prints without prefix are the results of your code running.

sudo

Almost everything you do in Linux will run with what's called user permissions, which are the capabilities of a regular user on the system. Occasionally, you will need to run with what's called superuser or root permissions, which you need to install certain software or make other dangerous changes to your system. For that you will user the superuser do command:

sudo -l

The command above lists your superuser permissions. In the following sections, we'll see a few ways the command is normally used.

apt-get

There are many different types of Linux, called Linux distributions, but the distributions we typically recommend using all use the same software package manager, called the Advanced Package Tool (APT). To use this tool to install a package, you type sudo (because you need superuser access) followed by the apt-get command followed by the names of one or more packages you want to install. For example:

sudo apt-get install hello

Any time you need to use apt-get for one of the tutorials on this site, we will provide you with the names of the packages to install.

pip

Just as apt-get installs system packages, there's also a command that installs libraries for Python programs, called PIP (a recursive acronym for PIP Installs Packages). We will usually run PIP as `pip3', which installs Python 3 packages. For example, to install 21's two1 library, you would run:

pip3 install two1

wget

Often you will need to download files from the web. We use two utilities for this; the first is wget which is usually used to download and save files. For example, the following command will download the original Bitcoin paper written by Satoshi Nakamoto:

wget https://bitcoin.org/bitcoin.pdf

curl

Another way to download files from the web is curl. Instead of writing to a file by default (like wget), curl writes to your screen by default. This is very useful for checking web-payable services that you start. For example, running the following command should tell you that you need to make a payment in order to use the following URL:

curl https://mkt.21.co/21dotco/zip_code_data/zipdata/collect?zip_code=94109

touch

Some commands in Linux make use of a file's modification time (mtime). We don't currently use any of those in the 21.co tutorials, however there is a command designed for updating those mtimes that we do occasionally use, called touch (because it "touches" the file, updating it's modification time, even though it makes no modifications). When you run touch on a file that doesn't exist, it creates that file as an empty file, which is sometimes useful. For example:

touch new_file

bash

All the commands that you've run above are run inside what's called a shell. The default shell in Linux is called bash, which is a variant of the Bourne shell named after its creator Stephen Bourne; bash stands for Bourne Again SHell.

When you have a series of bash commands you want to run, you will often run bash specifically. For example, if you want to open a shell as the superuser, you can run the following command:

sudo bash

(If you run the command above, you should type exit to return to your normal user shell. Running commands as the superuser can be dangerous.)

Flow control

Within the bash shell, we occasionally use operators that are not commands but which affect which commands get run. The main one of these that you will see in the tutorial is the and operator, which runs a second command only if the first command is successful. For example:

echo 'first command' && echo 'second command'

Environmental variables

Commands and logic in both bash and Python can use what are called environmental variables, which are settings in the programming environment. In the 21.co tutorials, we often set these outside of programs themselves to avoid storing sensitive data or to allow you to easily change how a program works. To set an environmental variable in bash, you can run a command similar to the following;

export MY_VARIABLE="value_of_my_variable"

21

The final command we'll talk about is 21, which is the command line interface to 21. It contains a Bitcoin wallet and everything else you need to use Bitcoin for machine payments. To learn more about 21, please read the Introduction to 21