Manage your Linux Server via the Command Line Interface

Introduction

When working with cloud-based servers, you should master the basic Linux commands to perform administrative tasks. These commands are important when installing applications, monitoring server resources, managing the file system, and more.

Although there are many GUI-based applications that you can use to manage your server, the command-line interface is free, fast, easy to use. It offers more control, especially when you’re remotely connected to your server via an OpenSSH Client. In this guide, you’ll learn how to use the most powerful and popular Linux commands on your server.

Prerequisites

To complete this tutorial, you need:

A Vultr Linux server.
A non-root user with sudo access.
An OpenSSH Client.

1. Linux Server Administration Commands

SSH to your server to run the following Linux administration commands.

sudo

The sudo command stands for superuser do. It allows you to run Linux commands with escalated privileges of the root user. You should pass sudo and the command name in one line as shown below.

$ sudo SAMPLE_COMMAND

You can learn more about sudo by reading the man page.

$ man sudo

See more articles about sudo.

date

Run the date command to display or set the current date and time on your server.

To see the current date:

$ date
Tue 05 Oct 2021 06:22:58 PM UTC

To set the date, use the `-s’ parameter. For example:

$ date -s '2021-10-17 11:00:00'
Thu Oct 17 11:00:00 EDT 2021

You can learn more about date by reading the man page.

$ man date

reboot

The reboot command restarts your server.

$ sudo reboot

You’ll lose your SSH connection and need to reconnect to the server after the operating system reboots. You can learn more about reboot by reading the man page.

$ man reboot

wget

Download files from the web using the wget command. For instance, to download http://www.example.com/index.html:

$ wget http://www.example.com/index.html
2021-09-28 07:47:30 (162 MB/s) - 'index.html' saved [1256/1256]

To specify the output filename, use the `-O’ parameter:

$ wget -O newfile.html http://www.example.com/index.html
2021-09-28 07:47:30 (162 MB/s) - 'newfile.html' saved [1256/1256]

You can learn more about wget by reading the man page.

$ man wget

history

See the list of commands that you’ve previously run by using the history command.

$ history

To re-execute a command from your history list, use the ! symbol followed by a command.

!n : Refer to a command from your history list. For command number 109:

$ !109

!-n : Refer to the current command minus n. For the previous command:

$ !-1

!! : Refer to the previous command. This is a synonym for !-1.

$ !!

There are several other options, search patterns, and substitutions available. See the man page for more information.

$ man history

clear

If your terminal screen is overcrowded, run the clear command to reset the screen.

$ clear

2. Linux File Management Commands

Use the commands below to manage directories and files in your Linux server.

pwd

In Linux, pwd is an acronym for print working directory. This command retrieves the path of your current working directory starting from root /.

$ pwd

mkdir

The mkdir stands for make directory. It allows you to create directories, which are also called folders in other operating systems.

Create a sample directory in your current location.

$ mkdir sample

In most cases, you’ll use the mkdir command with the -p flag. This option tells Linux to create parent directories along the way if they don’t exist.

For instance, if you attempt to run the command below without the -p flag, it should fail.

$ mkdir sample/abc/def

Output.

mkdir: cannot create directory `sample/abc/def`: No such file or directory

However, if you use the -p flag, the command will create both the sample and abc directories if they don’t exist.

$ mkdir -p sample/abc/def

The Linux mkdir command is handy when creating directories for running multiple websites on your server, as shown below.

$ mkdir -p /var/www/example.com/public_html
$ mkdir -p /var/www/example.net/public_html
$ mkdir -p /var/www/example.org/public_html

cd

You can navigate to another directory by using the cd command which stands for change directory. For instance, cd to the new sample directory.

$ cd sample

Run the pwd command to see if you’ve switched to the new directory.

$ pwd

Output.

/home/SAMPLE_USER/sample

To move up one level in the directory structure, use the cd .. command.

    $ cd ..

To navigate back to your home directory, use the cd command with the tilde(~) character.

$ cd ~

ls

Use the Linux ls command to print a list of files in a directory. The output includes other sub-directories as well.

$ ls

Output.

sample

The ls command has different options that you can use to format the output. For instance, use the -l flag to show the list of files, including their sizes, time of modification, current owners, and permissions.

$ ls -l

Output.

    drwxrwxr-x 3 SAMPLE_USER SAMPLE_USER 4096 Sep 28 06:29 sample

You can also use the h flags to display the sizes of the files in a human-readable format.

$ ls -lh

Output.

total 4.0K
drwxrwxr-x 3 SAMPLE_USER SAMPLE_USER 4.0K Sep 28 06:29 sample

To display hidden files, pass the `a’ flag.

$ ls -lha

Output.

drwxr-xr-x 7 SAMPLE_USER SAMPLE_USER 4.0K Sep 28 06:25 .
drwxr-xr-x 3 root    root    4.0K Sep 25 08:56 ..
-rw------- 1 SAMPLE_USER SAMPLE_USER 2.7K Sep 27 10:15 .bash_history
...
drwxrwxr-x 3 SAMPLE_USER SAMPLE_USER 4.0K Sep 28 06:29 sample

touch

Use the touch command to change a files’ last access date without modifying the contents. If the file does not exist, it will create an empty file with zero length. This is useful when you don’t have content to put into the file at the creation time.

$ touch sample_file.txt

Confirm the success of the touch command by running the ls -lh command.

$ ls -lh

Output.

drwxrwxr-x 3 SAMPLE_USER SAMPLE_USER 4.0K Sep 28 06:29 sample
-rw-rw-r-- 1 SAMPLE_USER SAMPLE_USER    0 Sep 28 06:47 sample_file.txt

mv

The mv command allows you to move a file or a directory to another location. You can also use it to rename files. Rename the sample_file.txt to sample_file.bk.

$ mv sample_file.txt sample_file.bk

Move the new sample_file.bk file to the /tmp directory.

$ mv sample_file.bk /tmp

rmdir

The rmdir stands for remove directory. You can use it to delete empty directories. Create a sample_2 directory and delete it.

$ mkdir sample_2
$ rmdir sample_2

rm

Use the rm command to delete files and non-empty directories. You should be careful when executing this command since it can break applications or render your system useless.

Delete the sample_file.bk in the /tmp directory.

$ rm /tmp/sample_file.bk 

To delete a directory and its contents, use the -r option. This allows you to delete the directory contents recursively. Create a sample_3 directory and a sample_4 sub-directory and delete them.

$ mkdir -p sample_3/sample_4
$ rm  -r sample_3

cp

In Linux, you can copy files and directories using the cp command. Normally, you need to specify the source and destination of the files.

Create a sample_2.txt file and copy it over to the /tmp directory.

$ touch sample_2.txt
$ cp sample_2.txt /tmp

This command is useful when tweaking settings on the base configuration files. Copy the default file to a new location with a new name to avoid losing the default settings.

For instance, before configuring the Apache virtual host file, create a backup copy so that you can restore the file in case you run into problems.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bk

chown

The chown command allows you to change the ownership of directories and files in your server. For instance, when you create directories as a superuser using the sudo command, the root user owns the directories. Therefore, to navigate between the new directories without permission issues, you should take ownership of the new directories by executing the chown command.

For instance, create the directory below.

$ sudo mkdir -p /var/www/example.net

Take ownership of the new directory as the current user.

$ sudo chown -R $USER:$USER /var/www/example.net

Also, you can assign ownership of the new directories to another user. For instance, www-data.

$ sudo chown -R www-data:www-data /var/www/example.net

chmod

The chmod command allows you to change read, write, and execute permissions for files and directories. Normally, you should use this command alongside the following permission numbers:

0: No Permissions
1: Execute permissions
2: Write permissions
3: Execute and write permissions
4: Read permissions
5: Read and execute permissions
6: Write and read permissions
7: Full permissions (Read, write, and execute permissions).

The basic syntax for the chmod command is.

$ chmod USER_PERMISSIONS GROUP_PERMISSIONS OTHERS_USERS_PERMISSIONS

For instance, to assign john full permissions to the /var/www/example.net directory and deny anyone from accessing it, execute.

$ chmod 700 /var/www/example.net

To give the user john full permissions(7) and assign only read and execute(5) permission to the group and the rest of the users, execute.

$ chmod 755 /var/www/example.net

There are several other options available for chmod; please see the man file for more information.

$ man chmod

cat

The cat (concatenate) command is useful when you want to read the content of a file and display the data in your shell window. For instance, use the cat command to display the content of your .bash_history file.

$ cat .bash_history

You can also display the content of multiple files using the cat command.

$ cat FILE_NAME_1 FILE_NAME_2

tail

Use the tail command to display the last parts of a file. This command is useful when you want to read log files to troubleshoot applications. To avoid displaying lots of content on the screen, specify the number of lines that you want to display when running the command.

$ sudo tail -10 /var/log/auth.log

Use the -f (follow) parameter to keep the file open and display any updates. This is useful when monitoring a log file.

$ sudo tail -f /var/log/auth.log

3. User Management Commands

You can check your current username, create new users, and change their passwords using the following Linux shell commands.

whoami

Use the whoami command to check your current username.

$ whoami

Output.

john

adduser

The adduser command is useful for creating user accounts in your system. This command should be run by root or a sudo user. For instance, create a user named mary.

$ sudo adduser mary

Follow the on-screen prompts to finalize setting up the account for the user.

passwd

You can change the password of a user by executing the passwd command. For instance, to change the password for user mary, run the command below.

$ sudo passwd mary

Enter and confirm the new password to update the user account.

4. Memory, Disk, and CPU Management Commands

Use the following commands to monitor your Linux server’s memory, disk usage, and CPU load.

free

To view the available RAM, use the free command. Pass the -h option to display the figures in a human-readable format.

$ free -h

Output.

                total        used        free      shared  buff/cache   available
Mem:          981Mi       560Mi       103Mi       6.0Mi       317Mi       263Mi
Swap:            0B          0B          0B

du

The du command stands for disk usage. The utility allows you to gain disk usage information on your system.

$ sudo du -h /tmp

Output.

...

4.0K    /tmp/snap.lxd/tmp
8.0K    /tmp/snap.lxd
88K     /tmp

df

Use the df(disk free) command to display the total and available space in your system.

$ sudo df -h

Output.

Filesystem      Size  Used Avail Use% Mounted on
udev            474M     0  474M   0% /dev
tmpfs            99M  1.1M   98M   2% /run
/dev/vda1        25G  3.5G   21G  15% /
...

top

To display the active Linux processes running in your system, use the top command.

$ top

Output.

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
...
1 root      20   0  170332   9408   4964 S   0.0   0.9   0:03.11 systemd
2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
...   

5. File Searching Commands

Linux has five file searching commands: whereis, locate, which, type, and find. Each of them has its own characteristics and is designed for particular search scenarios.

whereis

The whereis command is used to search binary files, source code, and the online manual pages at several standard install directories for any program name specified.

Because whereis does not search every location on your system, any files out of these specific directories will not be found. For the same reason, you will get your search result quickly, whether found or not.

Also, it will not search for those commands which are built directly into the shell.

For example, if you want to find info about the ls command, run the following command on your terminal:

$ whereis ls

You will get some feedback like:

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

In the result, /bin/ls is the binary you want to locate, the other two are manpages for the ls program, and no source code for the ls program was found.

locate

The locate command is used to find files by name with the help of a database (/var/lib/mlocate/mlocate.db). This database is a snapshot of the layout of your filesystem. Any records in this database that match your query will be listed in the search result. By default, the locate command will use your query to match any part of the whole name of each record, including the pathname. Therefore, the number of matched files in the search result could be more than you expected.

As the database becomes outdated, the search result of the locate command becomes less accurate. You may notice files that no longer exist, or you won’t see matches for newly-created files. By default, the mlocate.db database is automatically updated once a day. You can use the updatedb command to manually update it.

The search speed is very fast because the search is performed on the database instead of the filesystem.

If you run the following command:

$ locate ls

You will get many records instead of your expected result.

In order to make the search result more accurate, you can use the -b flag to restrict the search range, using your query to match only the basename of each record:

$ locate -b "ls"

This time, you will get the location of each file exactly called ls on your filesystem:

/bin/ls

which

The which command will search for the command you specified in the environment variable PATH value and return the first result by default. If you are querying an alias, the which command will determine the actual command for the alias before performing your search.

The which command is also very fast because of its simplicity.

The usage of the which command is simple:

$ which your_command

If you want to show all of the matched commands instead of the first one, use the `-a’ flag:

$ which -a your_command

Here is an example for searching for a command that has been aliased:

$ which ll

The search result would be:

alias ll="ls -l --color=auto"
    /bin/ls

type

By default, the type command will indicate how a command name would be interpreted. Possible results include an alias, a keyword, a function, a builtin, or a file. Just like the whereis command, the type command will only search in several standard install directories to answer your query.

Some examples for typing different commands:

A shell builtin command:

$ type cd
cd is a shell builtin

A binary file on the filesystem:

$ type sudo
sudo is /usr/bin/sudo

An alias:

$ type ls
ls is aliased to `ls --color=auto'

find

The ‘ find ‘ command is the most powerful among the five file searching commands introduced in this tutorial. Unfortunately, it is also the slowest one. Unlike the other four commands, the find command will search for your file on the entire filesystem, one i-node by one i-node. With the find command, you can use sophisticated query criteria to find every file you need and execute actions on the files found.

Search criteria for the find command is too sophisticated to explain in such a short article; here are a few examples instead.

Basic format of the find command:

find [path] [option] [action]

To find all files in the working directory and all of its sub-directories:

$ find

To find a file called aaa in your home directory and all of its sub-directories:

$ find ~ -name 'aaa'

To find all of the files in the filesystem that were modified in the last 24 hours:

$ find / -mtime 0

To find all of the files in the web directory and all of its sub-directories that belong to user nginx:

$ find /usr/share/nginx/html/ -user nginx

To find all of the files in the working directory whose permissions are 0744:

$ find -perm -0744

To find a file with the name aaa in the working directory and list its detailed info:

$ find -name 'aaa' -exec ls -l {} ;

Conclusion

In this guide, you’ve gone through the list of the most common Linux commands for everyday use when administering your cloud server. While the commands might seem confusing at first, with time, you’ll get used to them once you begin to apply them in a production environment.

注:本教程在Vultr VPS上测试通过,如需部署请前往Vultr.com