We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials

More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsOperating SystemsLinuxAdministration

An Introduction to Linux Terminal

Cloud Servers 727 × 90
An Introduction to Linux Terminal

In this tutorial you will get familiar with Bash command line interface (terminal) in Linux environment. As almost all servers in the world, your VPS server has a minimal installation of the Linux operating system installed by default. That means that it does not have a graphical interface and you must manage your server from Bash command line. At the beginning, the Linux command line may seem daunting, complex and scary, but it is not hard to manage your server from command line when you have clear basic knowledge. So if you are new with Linux server administration, read this tutorial very carefully.

First of all, you log in to your server. Logging in requires special software to be used. Here are some free programs which can be used to log in to your server via Secure Shell:

Windows: PuTTy

Mac and Linux: Terminal

Terminal is already included in all Mac and Linux operating systems. Windows does not have built-in software for connections via SSH, so you have to download one. The best free SSH client is PuTTY and it can be downloaded here.

From the terminal your server can be accessed by typing ssh server_IP and if you wish to access from PuTTY, just type your server’s IP address to the “Host name (or IP address)” section and click “Open” as displayed below:

Enter username and password:

login as: root

root@191.101.163.69’s password:

Type your password (it will not be visible, but do not panic – it is a security feature) and then click enter button on your keyboard.

You will see Message of the Day or MOTD which displays the main information about your server (operating system, last login information and etc.) after each time you successfully connect to the server. Next, you will see the default command prompt:

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 2.6.32-042stab108.2 x86_64)

* Documentation: https://help.ubuntu.com/

Last login: Wed Sep 30 11:41:32 2015 from 85.234.123.187

root@server:~#

Where:

root – the username you are logged in with.

Server – the hostname of your server

~ – the current folder you are in at the moment. By default, you are always dropped in your user’s home directory, which is marked as ~, but the actual current path is /home/root/ (because you are logged in as root).

# – marks the end of command prompt.

In Linux operating systems the root user is a super user who can access all files and does not have any restrictions in performing tasks on the system.

Commands in Linux Shell can be executed easily by typing the commands in the command prompt. There are plenty built-in commands and utilities in every Linux distribution that allow you to perform basic actions in your operating system. Almost all commands (or programs) can be executed with additional options, which can be viewed by typing –help or -h OPTION next to command. For example:

[root@server ~]# cat --help

Usage: cat [OPTION]… [FILE]…

[…]

-A, –show-all equivalent to -vET

-b, –number-nonblank number nonempty output lines

-e equivalent to –vE

[…]

You can also search for manual files using man command, for example type man –a yum and you will get all the information about yum command.

NOTE: Almost everything in Linux environment is CaSe sensitive, including commands, files and folders, additional arguments and etc. So you should always double check the spelling and case of your commands if something is not working.

Every executed command is also known as a task or a process. All processes usually run in background and return to the command prompt when finished, so you can execute other commands while others are already running.

If there is nothing to do in root home directory at the moment, change the directory, for example, to /var/log/. Use cd (change directory) command with /var/log ARGUMENT for this purpose:

cd /var/log/

Now you know the path where you are, but sometimes you can forget it, so there is a command pwd, which shows your current location in the file system:

[root@server log]# pwd

/var/log

Since you know where you are now you may want to know what files are located in this directory. In order to get list of files of directory, use ls (list contents of directories) command:

[root@server log]# ls

boot.log cron httpd mail messages secure tallylog yum.log

btmp dmesg lastlog maillog samba spooler wtmp

You can also use additional OPTIONS next to commands, for example ls –la will list all files (even hidden) with additional information:

[root@server log]# ls -la

total 200

drwxr-xr-x 5 root root 4096 Sep 25 08:12 .

drwxr-xr-x 20 root root 4096 Aug 30 13:04 ..

-rw——- 1 root root 0 Sep 25 08:11 boot.log

-rw——- 1 root utmp 2688 Sep 26 03:30 btmp

[…]

-rw-rw-r– 1 root utmp 2304 Sep 26 08:36 wtmp

-rw——- 1 root root 4646 Sep 25 09:53 yum.log

Not only you can add additional OPTIONS next to command, but also insert ARGUMENTS, for example:

ls –la /home/

will display the contents of /home directory.

Let’s analyze information about yum.log file (we are running RHEL/CentOS operating system, there will be no yum.log file in Debian/Ubuntu distributions):

--rw------- – means file/folder permission settings.

1 – number of files/folders (this value will always be 1 if it is a file).

root root – means owner and group of this file.

4646 – file size in bytes.

Sep 25 09:53 – Last modification date.

yum.log – file name.

The detailed tutorial how to manage files and folder permissions and ownership can be found here.

Now you know how to change the current position and list files and folders of the directory, but what if you need to read a specific file? There are plenty of commands for reading files, but in this tutorial you will be introduced with the most popular commands:

cat – dumps the whole file in plain text.

more – filters for paging through text one screen at a time.

less – is similar to more, but less allows backward movement in the file as well as forward.

tail – outputs the last part of files.

For example, if you want to tail secure file (which logs information about connections via SSH), you have to use tail COMMAND with an additional OPTION –f and ARGUMENT /var/log/secure:

tail –f /var/log/secure

Try to open a new connection to your server via SSH and you will see how it works.

As it was already mentioned, every executed command is also known as task or process. There are few commands for checking currently active processes – top and ps. Top command shows currently running active processes in real time, RAM memory usage, system local time, connected users, load averages and other useful information. The command ps shows processes that are running on the current time of command execution.

top - 06:06:05 up 1 day, 21:55, 1 user, load average: 0.00, 0.00, 0.00

Tasks: 20 total, 1 running, 19 sleeping, 0 stopped, 0 zombie

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

Mem: 1048576k total, 34392k used, 1014184k free, 0k buffers

Swap: 524288k total, 5512k used, 518776k free, 22200k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1 root 20 0 19236 224 84 S 0.0 0.0 0:00.09 init

[…]

540 root 20 0 21720 236 4 S 0.0 0.0 0:00.00 xinetd

551 root 20 0 66412 580 0 S 0.0 0.1 0:00.00 saslauthd

[…]

As you can see from the example above, there is a lot information displayed about the usage of the system. However, now you are interested in information about the processes – the owner of the process (USER), process ID number (PID) and COMMAND.

If you notice that some process is stuck or requires too many resources in your system, you can simply kill it by running kill command. For example, kill 551 kills sasltauthd process.

Another thing that may be useful for you is disk usage statistics. If your hard drive has no free space remaining, you may run into various problems, such as MySQL database or other process failure. The disk usage can be checked with df command, but it is hard to understand the output of this command because it shows the whole usage in bytes. The command df has an OPTION –h, which means human readable in order to make the information more understandable for people:

df –h

Sample output:

Filesystem Size Used Avail Use% Mounted on

/dev/ploop15799p1 40G 2.6G 35G 7% /

none 512M 4.0K 512M 1% /dev

none 512M 0 512M 0% /dev/shm

Let’s analyze the sample above. /dev/ploop15799p1 is the main file system of the server, which is mounted on / mount point. This partition has 35 GB available of 40 GB overall.

It is important to know what exact files and folders are using disk space. There is a command – du – for such a task to perform, but if you use it without additional options, it will display all file sizes in bytes. Add value s (summarize) and h (human readable) and argument * (calculate all files) – du –sh *:

du –sh *

Sample output:

[…]

4.0K dev

7.5M etc

4.0K home

11M lib

23M lib64

[…]

Here you can see file and folder size in MB and KB, as well as the name of file or folder.

After reading this article you should be familiar with Linux SSH command line, know a few main commands and have basic knowledge on how to check your system performance and resource usage. In order to get more and more information about Linux operating systems we recommend to keep reading our tutorials.

Rate this Tutorial:
No Comments
Cloud Servers 1 230×230

Other (47)

Popular Keywords

VPS Banner 1 230×230