Commands / Linux · February 10, 2022 0

20 Linux Commands every System administrator should know

1. Command: Uptime
In Linux uptime command shows how long your system is running and the number of users who are currently logged in and also displays the load average of the system:  5.53, 2.99, and 1.73 

[syslof@syslof ~]$ uptime
17:31:10 up 29 min, 1 user, load average: 1.00, 2.13, 1.54

2. Command: w
The w command will display how many users are logged on the system and their process along with showing load averageslogin nametty name,  login timeidle timeJCPUPCPU, command, and processes.

[syslof@syslof ~]$ w
17:29:27 up 27 min, 1 user, load average: 5.33, 2.99, 1.73
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
syslof tty3 tty3 17:22 27:26 49.53s 2.63s /usr/bin/gnome-software --gapplication-service


Arguments:

  • -h : displays no header entries.
  • -s : without JCPU and PCPU.
  • -f : Removes from the field.
  • -V : (upper letter) – Shows versions.


3. Command: who

The who command simply returns the user namedate,  and time.

[syslof@syslof ~]$ who
syslof tty3 2022-02-10 17:22 (tty3)

Arguments:

  • -b: Displays last system reboot date and time.
  • -r: Shows current runlet.
  • -a, –all: Displays all information cumulatively.

4. Command: ls
One of the most used commands is ls, used without any argument ls displays the list on current directory. There are couple of important arguments which comes with ls.

Arguments:

h: Print a human readable format.
-r: Reverse order while sorting
-a, –all: Displays all information cumulatively.

* How to check all files in human readable format. You can simple use the ls -lah combination.

[syslof@syslof syslof-demon]$ ls -lah
total 4.0K
drwxr-xr-x 3 syslof root 79 Feb 10 19:56 .
drwx------ 16 syslof root 4.0K Feb 10 19:54 ..
-rw-r--r-- 1 syslof root 0 Feb 10 19:56 demo1.syslof.cfg
-rw-r--r-- 1 syslof root 0 Feb 10 19:55 demo.syslof.cfg
drwxr-xr-x 2 syslof root 6 Feb 10 19:55 folder.demo.syslof


* To sort the files as per last modified time you can use ls -ltr combination.
[syslof@syslof syslof-demon]$ ls -ltr
total 0
-rw-r--r-- 1 syslof root 0 Feb 10 19:55 demo.syslof.cfg
drwxr-xr-x 2 syslof root 6 Feb 10 19:55 folder.demo.syslof
-rw-r--r-- 1 syslof root 0 Feb 10 19:56 demo1.syslof.cfg

5. Command: crontab
The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. The abbreviation is “cron table”.

By crontab -l you can display what is running on the crontab.

[root@syslof ~]# crontab -l
* * * * /bin/bash my-bash-script.sh


Edit your crontab with -e the option. In the below example will open schedule jobs in vim editor. Make necessary changes and quit pressing :wq! keys that save the setting.


* * * * /bin/bash my-bash-script.sh

Type :qa! and press to abandon all changes and exit Vim

6. Command: Less
less command allows quickly viewing the file. You can use ‘gg’ to go to the top of the file and ‘GG’ to go to the bottom of the file. By pressing ‘q‘ to quit from less window.

7. Command: cp
The Linux cp command is used for copying files and directories to another location.

Copy a file to a different location using the cp -rpf

[root@syslof syslof]# cp -rpf demo.cfg /home/syslof/syslof-demon/

8. Command: mv
The mv command is a command line utility that moves files or directories from one place to another or renames fileA to fileB using the -i option, which prompts confirmation before overwriting.

Move the file to a folder by using mv

[root@syslof syslof]# mv demo.cfg /home/syslof/syslof-demon/

Rename a file

[root@syslof syslof]# mv demo.cfg demo.cfg-OLD

9. Command: cat
One of its most commonly known usages is to print the content of a file onto the standard output stream.

Display whole content of a file by

[root@syslof syslof]# cat demo.txt

And also there is a method you can combine cat with less and read the whole file page by page

[root@syslof syslof]# cat demo.txt |less

10. Command: free
The free command gives information about used and unused memory usage and swap memory of a system. By default, it displays memory in kb (kilobytes).

[syslof@syslof ~]$ free
total used free shared buff/cache available
Mem: 1860784 976152 178184 19136 706448 715008
Swap: 2097148 14916 2082232

there is a way how to display the total memory, used memory and the free memory in human readable format and it’s total of the machine by using free -ht

[syslof@syslof ~]$ free -ht
total used free shared buff/cache available
Mem: 1.8Gi 968Mi 134Mi 18Mi 714Mi 683Mi
Swap: 2.0Gi 14Mi 2.0Gi
Total: 3.8Gi 982Mi 2.1Gi

11. Command: df
The df command (short for disk free), is used to display information related to file systems about total space and available space.

[syslof@syslof ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 901768 0 901768 0% /dev
tmpfs 930392 0 930392 0% /dev/shm
tmpfs 930392 9440 920952 2% /run
tmpfs 930392 0 930392 0% /sys/fs/cgroup
/dev/mapper/cl-root 17811456 5848268 11963188 33% /
/dev/sda1 1038336 445264 593072 43% /boot
tmpfs 186076 8 186068 1% /run/user/1000
tmpfs 186076 32 186044 1% /run/user/200

There are many useful arguments for the df which can be combined. Some of them are.
-h: Print a human readable format.
-i: List inode information instead of block usage
-T: Print file system type

Displaying the used disk in human readable and the file system by using

[syslof@syslof ~]$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 881M 0 881M 0% /dev
tmpfs tmpfs 909M 0 909M 0% /dev/shm
tmpfs tmpfs 909M 9.3M 900M 2% /run
tmpfs tmpfs 909M 0 909M 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 17G 5.6G 12G 33% /
/dev/sda1 xfs 1014M 435M 580M 43% /boot
tmpfs tmpfs 182M 8.0K 182M 1% /run/user/1000
tmpfs tmpfs 182M 32K 182M 1% /run/user/200

The other option in our guide is show the inode status on the machine

[syslof@syslof ~]$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 225442 378 225064 1% /dev
tmpfs 232598 1 232597 1% /dev/shm
tmpfs 232598 767 231831 1% /run
tmpfs 232598 17 232581 1% /sys/fs/cgroup
/dev/mapper/cl-root 8910848 193963 8716885 3% /
/dev/sda1 524288 326 523962 1% /boot
tmpfs 232598 18 232580 1% /run/user/1000
tmpfs 232598 28 232570 1% /run/user/200

12. Command: tar
The tar command stands for tape achieve. It allows for you to quickly access a collection of files and placed them into a highly compressed archive file commonly called tarball, or tar, gzip, and bzip in Linux.

The command below will create an archive and put it in on the same folder.
[syslof@syslof ~]$ tar -cvf demo2.txt.tar .

The opposite command to extract the file content of the file is
[syslof@syslof ~]$ tar -xvf demo2.txt.tar .

13. Command: grep
Grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
There are couple of arguments we can use in that manner for now we will only check one which will search for a text into a file.
[syslof@syslof ~]$ grep syslof /etc/passwd
syslof:x:200:0:System Lots Of Fun:/home/syslof:/bin/bash

14. Command: find
In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object. 

Find command used to search filesstrings, and directories. The below example of find command search syslof word in ‘/‘ partition and return the output.

[syslof@syslof ~]$ sudo find / -name syslof
/run/sudo/ts/syslof
/run/console/syslof
/var/lib/AccountsService/users/syslof
/var/db/sudo/lectured/syslof
/var/spool/mail/syslof
/home/syslof

15. Command: ls
lsof mean List of all open files. Below lsof a command list of all opened files by user syslof.

[syslof@syslof ~]$ lsof -u syslof

16. Command: ps
The ps command, short for Process Status, is a command line utility that is used to display or view information related to the processes running in a Linux system.

[syslof@syslof ~]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:33 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
root 2 0 0 09:33 ? 00:00:00 [kthreadd]
root 3 2 0 09:33 ? 00:00:00 [rcu_gp]
root 4 2 0 09:33 ? 00:00:00 [rcu_par_gp]

17. Command: kill
The kill command to terminate the process. First, find process id with ps command as shown below and kill the process with kill -9 command.

[syslof@syslof ~]$ ps -ef |grep syslof
syslof 1 1 0 09:37 ? 00:00:01 /sbin/init
syslof 2876 2873 0 09:37 ? 00:00:00 grep init

Now kill the process with
kill- 9 2876

18. Command: rm
Rm removes each file specified on the command line. By default, it does not remove directories. When rm is executed with the -r or -R options, it recursively deletes any matching directories, their subdirectories, and all files they contain.

[syslof@syslof ~]$ rm -i demo2.txt
rm: remove regular file 'demo2.txt'?

19. Command: mkdir
mkdir is used to create directories under Linux.
[syslof@syslof ~]$ mkdir demo-test

20. Command: last
The last command in Linux is used to display the list of all the users logged in and out since the file /var/log/wtmp was created. 

[syslof@syslof ~]$ last
syslof tty3 tty3 Fri Feb 11 09:37 still logged in

Thus were the basic commands that every admin should know. 🙂 If you have any other suggestions or topics we have to cover or to edit please feel free to contact us and we will review and manage to fulfil the needs.

We love the open source.