Guide

Key Linux Commands to Manage a Server

Pinterest LinkedIn Tumblr

Here we list all the basic Linux commands essential to manage a Virtual Private Server, a cloud instance, or a dedicated server. We try to collect only the key commands you need to master to build a server to host your websites.

Linux itself is an operating system with hundreds or maybe thousands of commands. Luckily, you don’t need to remember it all. Just know what you really need for which in this case is for managing a web hosting server.

Let’s start from the very basic section, file management.

Organizing and managing files

1. Changing directory

Use cd to move from one directory to another. It’s a very simple and very basic command.

Example, moving from the root directory to /var/www/html:

cd /var/www/html

Typing cd (and hitting Enter) without any additional instrument will bring you directly to the root directory.

cd

The same also if you use cd ~ command:

cd ~

Use cd .. (cd space two dots) to move to the upper directory.

cd ..

A real example:

2. Listing directory files and folders

You are in the directory you want. Now, how to find out what files and folders are in the directory. The command you need is ls.

ls

Example, change to the nginx directory located at /etc/nginx and list the content:

Use ls -l to list files and directories along with detailed information including file size, User owner, Group owner, last modified or date created, and permission.

ls -l

Example:

Use ls -a to list all files and folders including hidden ones.

ls -a

For example, list all files and directories (hidden and not) in the root directory.

See how the result is different between ls and ls -a command.

3. Deleting Files and Directories

Use the rm command followed by the file name to remove it.

rm filename.extension

For instance, let’s remove the backup.php file:

rm backup.php

Use the rmdir command to remove a directory:

rmdir directoryname

For example, let’s remove a test directory:

rmdir test

The rmdir command won’t work if the directory is not empty. Use the rm -rf command instead.

rm -rf directoryname

4. Moving and Renaming Files and Directories

Use mv to move a file to another directory. Usage:

mv filename newpath

Example. moving configs.php to the scripts directory:

mv configs.php scripts

You can also use a complete directory path. For instance, let’s move the configs.php file from the scripts directory (/root/scripts/) to the web directory located at /var/www/html.

mv configs.php /var/www/html

You can also use the mv command to rename files or folders. Example, let’s rename the configs.php to setup.php:

mv configs.php setup.php

5. Copying and Duplicating Files

Use cp command to copy or to duplicate a file.

cp file1.ext file2.ext

or,

cp file1.ext /new/path/file1.txt

Example: duplicating the configs.php file to create a backup.

cp configs.php configbackup.php

System Management

1. Viewing the content of a file easily.

Use the cat command to display the contents of a file. It is usually used to easily view programs.

2. Inserting some text to a file quickly.

Us eth echo command to conveniently add a line of text to a file. For instance, let’s add “we test we know” text into the configs.php file.

3. Installing Nano Editor

On CentOS use:

yum install nano -y

On Ubuntu or Debian use:

apt-get install nano -y

On a recent version of Ubuntu or Debian use:

apt install nano

4. Editing a file with Nano

Use nano filename.ext. Example, to edit configs.php using Nano editor:

nano configs.php

5. Saving Edited File with Nano

Press Control+O buttons on your keyboard.

6. Quitting Nano / Exit the Editing Mode

Press Control+Xbuttons on your keyboard.

7. Checking RAM Usage

free -m

8. Changing Password

Use passwd to change the password of currently logged in user.

passwd

9. Checking Disk Partitions

df -m

This command shows the available disk space in each of the partitions. You can see each mounted partition and the used/available space in % and in MBs.

10. Checking Disk Usage of Files and Folders

Use du to check the disk usage for a particular folder or file.

du directoryname

11. Zipping and unzipping

Zipping the whole directory recursively:

zip -r filename.zip /path/to/directory

Unzipping any .zip file:

zip filename.zip

12. Showing information about the system

uname -a

The command prints the kernel release date, version, processor type, etc.

13. Showing Running Services

top

The top command prints all the running services along with its CPUs and RAM usage. You can also see the realtime system load status on the top.

Press Q on the keyboard to exit.

14. Canceling Any Command

Press Control+C on the keyboard to stop or to cancel the currently running task issued by the latest command. For instance, you have issued zip -r /var/www/html but the zipping process consumes a lot of time. Press Control+C to cancel it.

15. Quitting the SSH Session

exit

This will close your current SSH login session and quit Terminal / Putty.

That’s all folks. Please do not hesitate to add more if you know some. Also, please do not hesitate to ask questions.

A blogger by hobby. He is an ordinary worker at day, a hybrid sys-admin at night. Sharing knowledge through blogs is his passion. He likes ice cream BTW.

Write A Comment