what-is-sudo

A series on Linux. Find your way around a console.

Connecting to Linux servers

I’ve mentioned before how if you are deploying your application to a server, it will most likely be Linux. It is therefore important that you are able to connect to your Linux server and understand how that connection works.

This post is part of the "what is sudo?" series, which aims to help understand Linux and text consoles.

Remember when you rent a server, what you are renting is a real computer. When you connect to it, you are connecting to a computer, and so it behaves just like your desktop would.

If you don't currently have a server but you want one, you can follow this guide.

Connecting to a server is usually done in one of two ways:

  • Username and password; or
  • SSH keys.

In all cases, you would log in using the following command from your computer:

ssh <user>@<ip>  

If the user you are trying to log in as does not have a valid SSH key, then you will be prompted for a password. If the user you are trying to log in as has a valid SSH key, then the log in will just work, without the need for a password.

Username and password

This is the least secure of the two methods, because it relies on nobody else knowing your username and password. However, it does not require much set up and allows you to access the server from anywhere, as long as you know the username and password.

SSH keys

This is more secure, but it relies on nobody stealing your computer or your private keys.

SSH keys work on a per-login basis. Lets say you create an account in your server called jose. You can then put an SSH key into that account that will let you log in from your computer directly into that account, without using a password. The SSH key will not work for other accounts.

In your server, SSH keys are in the ~/.ssh/ folder, where, as you already know, ~ is the user’s home folder.

Generating SSH keys

From your computer, you can generate SSH keys using the command below (if you are using Mac or Linux):

$ ssh-keygen
Generating public/private rsa key pair.  
Enter file in which to save the key (/Users/yourname/.ssh/id_rsa):  
Enter passphrase (empty for no passphrase):  
Enter same passphrase again:  
Your identification has been saved in id_rsa.  
Your public key has been saved in id_rsa.pub.  
The key fingerprint is:  
(long number here)
The key's randomart image is:  
(random art here)

I would suggest just accepting the default file, and then enter a passphrase (a password) that you will remember. This is a way to secure your SSH key so that even if someone gains access to your computer, they still won’t be able to use the SSH key.

If you are using Windows, install https://git-for-windows.github.io, and then you will be able to run the command above from a program called "Git Shell".

Adding SSH keys to your server

Login to the server using username and password. Then, copy the contents of the SSH key file (in the example above, /Users/yourname/.ssh/id_rsa) and then:

vi ~/.ssh/authorized_keys  

Then, press i and paste the contents of your SSH key.

Now your SSH key will be accepted by that user in that server. Next time you connect to the server, you won't be asked for a password.

Contents of files and introduction to Vi

Let’s talk about reading and modifying files.

This post is part of the "what is sudo?" series, which aims to help understand Linux and text consoles.

The cat command

Using the cat command, you can view the text contents of a file:

cat my_file.txt  

You can also use cat to append to a file, and to overwrite a file.

The code below appends “Hello, world” to the file my_file.txt.

cat >> my_file.txt  
Hello, world

[CTRL+C]

The code below overwrites my_file.txt with the words “Hello, world”.

cat > my_file.txt  
Hello, world

[CTRL+C]

The vi command

Note, sometimes this command is vim (for vi “improved”).

Vi is a text editor. You can open files, edit them, and save them.

It is powerful, but it takes some getting used to.

Open files like so:

vi my_file.txt  

Here’s a quick list of commands:

  • i gets you into ‘Insert’ mode. Think of this as your normal text editor mode, where you can write characters, use your backspace, etc…
  • v gets you into Visual mode, where you can select characters.
  • V gets you into Visual (line) mode, where you can select lines.
  • ESC gets you out of any mode.
  • While not in a mode, :w saves the file to disk.
  • While not in a mode, :q quits the current file.

You can chain the last two commands like so: :wq, so save and quit.

If you want to learn how to use Vi well, I recommend this: http://vim-adventures.com

A word on programs

A common misconception is that things like mkdir, rm or adduser are different from things you install in your system (e.g. postgresql, or mongod).

They are not.

All of them are programs. Some already come installed, and others you have to install yourself. They are not “magically built-in”.

You can find out where a program executable lives using the which program:

$ which mkdir
/bin/cd
$ which which
/usr/bin/which

However, there are some built-in commands in the Bourne Again SHell (BASH). They are not very many:

  • alias;
  • cd;
  • wait;
  • and a few others

If you try executing which cd, chances are it will not be found, because it is using the built-in command, as opposed to a program.

Some built-in commands, such as kill, are sometimes overwritten by the distribution.

It is important that you know which commands are available to you. Some consoles may not use BASH, and so the cd command might not be available. An example of this is the Windows console, which uses chdir instead.

Most Linux distributions use BASH, so you won’t have a problem with that.

Getting help

Most Linux distributions come with a very handy program called man. You can use this program to get the help pages of many other programs.

For example, execute the following command:

man mkdir  

You can even get the help pages on BASH built-in commands:

man cd  

It was a short blog post today, but the key thing to remember is that the commands you are executing in your console are really programs which have been installed in your system. They can be un-installed, they can be overwritten, or they can just not be there yet.

You can install programs such as man and other programs that you might expect to already be there. We'll get to package managers in the next post!

Basic Linux console commands

Just like navigating a desktop, it is important to be able to navigate a console.

This post is part of the "what is sudo?" series, which aims to help understand Linux and text consoles.

Dealing with files

Every folder in a system except the root folder is inside another folder. This other folder is the parent folder. The root folder, or top-most folder, is the folder that contains everything in your system.

The root folder is: /.

Some users in your system have associated "home" folders. You can think of this folder as where a user would normally put their own "Documents", "Photos", etc.

Whenever you use a system, you must be logged in as a specific user. Let's find out what your current user is.

Open a console, and type the following command:

whoami  

This will tell you the name of the user you are currently logged in as.

Next, lets navigate to your user's "home" folder:

cd ~  

The cd command performs a change directory command. The ~ folder is the current user's "home" folder.

Creating files and folders

To create a folder, we use the mkdir command.
To create an empty file, we use the touch command.

# This creates a folder 'documents' inside your home folder.
mkdir ~/documents

# This creates an empty file 'hello.txt' in the documents folder.
touch ~/documents/hello.txt  

If we want to create a folder inside another folder, neither of which exist:

mkdir -p ~/documents/work/journal  

This creates a folder called 'work', and then a folder called 'journal' inside it. If we omit the -p, then it gives an error because it attempts to create the 'journal' folder inside a folder that doesn’t yet exist ('work').

Deleting files and folders

Finally, lets look at deleting. We delete using the rm command.

# This deletes the file 'hello.txt'
rm ~/documents/hello.txt  
# This deletes the folder 'work', including everything it contains
rm -rf ~/documents/work  

The -rf argument is required in order to delete folders. This is because we want to delete everything inside the folder, as well as the folder. The r stands for "recursive", which tells the rm command to go into each sub-folder and delete everything in it. The f argument attempts to delete files even if they are not owned by the currently logged in user.

Review

  • whoami shows the currently logged in user.
  • cd [args] changes directory to another directory.
  • mkdir [-p] [args] creates a directory.
  • touch [args] creates an empty file.
  • rm [-rf] [args] removes a file or directory.

Why would you want to use a console?

You probably have a desk. You can put things on the desk, or move things, or pile things on top of one another. You have a folder in which you put important documents. Maybe that folder sits along many other folders in a filing cabinet.

Your computer also has a desktop. You can create files and folders, which appear as magic. Some time ago, computers did not have desktops. Computers did not have mice, windows, or icons. “It must have been very slow to use computers,” I hear you say. “I’m glad it’s no longer necessary to learn how to use a computer without a graphical interface,” I hear often.

I must pull you from your fantasy. Graphical interfaces are that: interfaces. They allow us to easily interact with an underlying system. But what if I told you you can interact with the system directly, the same way that was used to create the system in the first place?

mkdir week{1..5}  

Many clicks of the mouse are required to do that line of code using a graphical interface. It creates five folders, called week1 to week5.

Even more powerful is nesting:

mkdir -p week{1..5}/lesson{1..3}  

As that creates folders week1 to week5, with each folder having the following three sub-folders: lesson1, lesson2, and lesson3.

Naturally you can use the console for far more than creating folders. Time-consuming tasks like finding which files in a directory contain the text “Find me”:

grep -l "Find me" *  

You still haven’t convinced me

Fine, the console looks ugly and you don’t want to use it. The time it saves isn’t worth the effort of learning how to use.

Well, sorry but you don’t have a choice.

If you want to deploy your applications to a server, you need to use a console. There is simply no other reliable way to interact with a remote server. Graphical interfaces are too slow to load over an internet connection and they require too much power.

It is very likely that if you use a web server, it will be running some Linux distribution (http://w3techs.com/technologies/details/os-unix/all/all). This makes it all the more important for you to learn to find your way around a Linux console.