Linux Server setup

First steps and Linux common commands

If you already know the commands ls, cd, pwd, rm, mkdir, rmdir, nano and history, you can skip this chapter and go to Basic settings.

This chapter is dedicated to Linux newbies.

Since we now have a connection via a terminal, let's try out a handful of commands right away and slowly get used to the shell. I assume that whoever deals with Linux servers has enough prior knowledge to already know or quickly understand the following.


Short explanation of the code representation in the tutorial

These gray boxes are shell or code blocks:


__$ 
 

You can copy the content with STRG/CMD + c and paste it into a terminal. Either with STRG/CMD + v or with PuTTY with the right mouse button.

The symbols # and $ are signs in a shell and should not be copied. An explanation of the signs follows later.

So in this example, only the ls command is to be copied:


__$ ls
 

If several commands are combined in one box, each line has its own prefix (# or $). You can copy the lines individually or all at once.


__$ cd
__$ ls
 

You can recognize a multi-line command by the fact that only one sign is set. Here the last empty line should be copied, because otherwise a rest remains unexecuted in the shell. The next example is meant for testing. You will quickly see what makes the difference:


__$ echo "Here is 
__gt a multiline 
__gt command. 
__gt Copy the next empty line sometimes with and sometimes without." 
 
copy including empty line

The root user

The term root has two meanings in a Unix/Linux system. On the one hand it is the root directory (/) in the file system and on the other hand it is the superuser who has all system rights. In this tutorial, we mostly mean the root user. The superuser can install everything, but also delete everything. Therefore it is necessary to protect this user especially. And another peculiarity in this context: Usually users get their own directory under /home. The home directory of the root user, however, already starts in the root directory (/).

For security reasons, the direct system access of the root user is disabled and a deputy administrator is created instead. This user is added to the sudo group and gets root rights. The root user can then only be changed if you are already logged in with another user. There is discussion about whether another user is really necessary if no one else is working on the server. It is best to form your own opinion on this. I think it is a reasonable security concept, with which nothing is done wrong.

On our Linux distribution (Ubuntu Server Live), the root user is already locked and tom has already been created as the acting administrator.

The sign in the shell from the root user is the hash: #

If you see a # at the end of a line (after username and hostname) in the shell, it means that you are logged in as root. All other users have a $ symbol as prefix instead of the hash.


sudo

sudo is a command that allows processes to be run as the "superuser" root. To do this, this user must belong to the sudo group of the same name. Commands executed with sudo must be confirmed with the user's own password. The authorization is valid for a while and expires after a while.

The user tom that we created during the Linux installation has been given sudo privileges. We will use this privilege mainly for system-related tasks.


A few standard commands

As an exercise, here are a few commands that can be copied and pasted into the currently running shell.

Show folder contents with ls

The content of the current folder is output with ls.


__$ ls
 

Files whose names start with a dot . are not displayed. If the command is extended by the -a parameter, all contents of a folder are displayed:


__$ ls -a
 

With -l a detailed list is output:


__$ ls -l
 

This can also be combined:


__$ ls -la
 

To display the contents of another folder, a relative or absolute path can be specified:


__$ ls -la /etc/ssh
 

An absolute path starts with a / (root directory):


__$ ls -la /
 

If the output exceeds the screen height, | less can be used to interrupt the output. With the arrow keys it continues and with q it can be terminated:


__$ ls -la /etc | less
 

Change directory with cd

With cd you can change to another directory. For example with an absolute path:


__$ cd /etc/ssh
 

And from there continue with a relative path:


__$ cd ssh
 

pwd tells you where you are at the moment::


__$ pwd
 

Output:


/etc/ssh
 

cd .. changes one level higher:


__$ cd ..
__$ pwd
 

Output:


/etc
 

If only cd is executed, you will get to the home directory of the logged in user (/home/<USER>/). An equivalent for the path of the home directory (/home/<BENUTZER>/) is ~. So with cd ~ you can also change to the home directory.

The home directory of the root user is the root directory /.

If you run the following command as root, you will end up in the root directory.


__$ cd ~
__$ pwd
 

Output:


/
 

The Nano text editor

nano is a built-in editor that does not need to be installed separately. As a small exercise we create a file, save it in a folder for temporary files and delete it right away.


__$ nano /tmp/test
 

The editor opens and we write something: "Hello, World!". The /tmp/test file doesn't exist yet, so when we save it we are asked if we want to create it with this content. The key combination CTRL+s to save and CTRL+x to close.

If there are changes that were not saved, after CTRL+x a close menu opens. With y and Enter we confirm the displayed path to save.

Very useful is the shortcut CTRL+k to delete whole lines.

Delete file with rm

Files are deleted with rm .Let's delete the test file just created:


__$ rm /tmp/test
 

A quick check with ls to see if the file is still there:


__$ ls /tmp
 

Create folder with mkdir

Folders are created with mkdir:


__$ mkdir /tmp/test-ordner
 

Let's see if the folder exists:


__$ ls -la /tmp
 

Delete folder with rmdir

With rmdir we delete the folder we just created:


__$ rmdir /tmp/test-ordner
 

Auto complete with tab

When the first letters of a command or path have been typed, Tab can be used to automatically complete the rest.

Assuming we want to list the contents of the /home folder, it is enough to write ls /h and then press Tab. If there are several possibilities, pressing Tab twice will display a list. Try this with ls /lib and then Tab twice.


History and repeat commands

All commands entered are stored in the .bash_history file in the respective user folder. With the command history this can be read out:


__$ history
 

Usually the up/down arrow keys are used in the shell to scroll through the last entries.

Another possibility is the key combination CTRL+r. With it the last entries can be searched. If you now enter ls, for example, the last entry appears. With CTRL+SHIFT+r you can scroll through it. Cancel with Escape.