30 Useful Linux Commands for System Administrators

[‘

n

In this article we are going to review some of the useful and frequently used Linux or Unix commands for Linux System Administrators that are used in their daily life.

n

This is not complete but it’s a compact list of commands to refer to when needed. Let us start one by one how we can use those commands with examples.

n

1. Uptime Command

n

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 a system for 1, 5, and 15 minutes intervals.

n

# uptimernrn08:16:26 up 22 min,  1 user,  load average: 0.00, 0.03, 0.22

n

Check Uptime Version

n

Uptime command don’t have other options other than uptime and version. It gives information only in hours:mins:sec if it is less than 1 day.

n

# uptime -Vrnprocps version 3.2.8

n

2. W Command

n

The w command will display users currently logged in and their process along with showing load averages, login name, tty name, remote host, login time, idle time, JCPU, PCPU, command, and processes.

n

# wrnrn08:27:44 up 34 min,  1 user,  load average: 0.00, 0.00, 0.08rnUSER     TTY      FROM              [emailxa0protected]   IDLE   JCPU   PCPU WHATrntecmint  pts/0    192.168.50.1     07:59    0.00s  0.29s  0.09s w

n

Available Options

n

    n

  • -h : displays no header entries.
  • n

  • -s : without JCPU and PCPU.
  • n

  • -f : Removes from the field.
  • n

  • -V : (upper letter) – Shows versions.
  • n

n

3. Users Command

n

Users command displays currently logged-in users. This command doesn’t have other parameters other than help and version.

n

# usersrnrntecmint

n

4. Who Command

n

who command simply returns the user name, date, time, and host information. who command is similar to w command. Unlike the w command who doesn’t print what users are doing. Let’s illustrate and see the difference between who and w commands.

n

# whornrntecmint  pts/0        2012-09-18 07:59 (192.168.50.1)

n

# wrnrn08:43:58 up 50 min,  1 user,  load average: 0.64, 0.18, 0.06rnUSER     TTY      FROM              [emailxa0protected]   IDLE   JCPU   PCPU WHATrntecmint  pts/0    192.168.50.1     07:59    0.00s  0.43s  0.10s w

n

Who command Options

n

    n

  • -b: Displays last system reboot date and time.
  • n

  • -r: Shows current runlet.
  • n

  • -a, –all: Displays all information cumulatively.
  • n

n

5. Whoami Command

n

In Linux, a whoami command is used to print the currently logged-in username into your Linux system. If you are logged in as a root using sudo command “whoami” command return root as the current user.

n

# whoamirnrntecmint

n

6. ls Command

n

ls command displays a list of files in a human-readable format.

n

# ls -lrnrntotal 114rndr-xr-xr-x.   2 root root  4096 Sep 18 08:46 binrndr-xr-xr-x.   5 root root  1024 Sep  8 15:49 boot

n

Sort file as per last modified time.

n

# ls -ltrrnrntotal 40rn-rw-r--r--. 1 root root  6546 Sep 17 18:42 install.log.syslogrn-rw-r--r--. 1 root root 22435 Sep 17 18:45 install.logrn-rw-------. 1 root root  1003 Sep 17 18:45 anaconda-ks.cfg

n

For more examples of the ls command, please check out our articles:

n

n

7. Crontab Command

n

List schedule jobs for current user with crontab command and -l option.

n

# crontab -lrnrn00 10 * * * /bin/ls >/ls.txt

n

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

n

# crontab -e

n

For more examples of Linux Cron Command, please read our earlier articles:

n

n

8. Less Command

n

less command allows quickly viewing the file. You can page up and down. Press ‘q‘ to quit from less window.

n

# less install.logrnrnInstalling setup-2.8.14-10.el6.noarchrnwarning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEYrnInstalling filesystem-2.4.30-2.1.el6.i686rnInstalling ca-certificates-2010.63-3.el6.noarchrnInstalling xml-common-0.6.3-32.el6.noarchrnInstalling tzdata-2010l-1.el6.noarchrnInstalling iso-codes-3.16-2.el6.noarch

n

9. More Command

n

more command allows quickly view file and shows details in percentage. You can page up and down. Press ‘q‘ to quit out from more window.

n

# more install.logrnrnInstalling setup-2.8.14-10.el6.noarchrnwarning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEYrnInstalling filesystem-2.4.30-2.1.el6.i686rnInstalling ca-certificates-2010.63-3.el6.noarchrnInstalling xml-common-0.6.3-32.el6.noarchrnInstalling tzdata-2010l-1.el6.noarchrnInstalling iso-codes-3.16-2.el6.noarchrn--More--(10%)

n

[ You might also like: Learn Why ‘less’ is Faster Than ‘more’ Command for Effective File Navigation ]

n

10. CP Command

n

A cp command copies file from source to destination preserving the same mode.

n

# cp -p fileA fileB

n

You will be prompted before overwriting to file.

n

# cp -i fileA fileB

n

[ You might also like: How to Force cp Command to Overwrite without Confirmation ]

n

11. MV Command

n

An mv command renames fileA to fileB using the -i option, which prompts confirmation before overwriting. Ask for confirmation if exist already.

n

# mv -i fileA fileB

n

12. Cat Command

n

The cat command is used to view multiple files at the same time.

n

# cat fileA fileB

n

You combine more and less command with cat command to view file contain if that doesn’t fit in single screen/page.

n

# cat install.log | lessrnrn# cat install.log | more

n

For more examples of Linux, cat commands read our article on 13 Basic Cat Command Examples in Linux.

n

13. cd command (change directory)

n

with the cd command (change directory or switch directory) it will go to fileA directory.

n

# cd /fileA

n

14. pwd command (print working directory)

n

A pwd command return with the present working directory.

n

# pwdrnrn/root

n

15. Sort command

n

The sort command is used to sort lines of text files in ascending order. with -r options will sort in descending order.

n

# sort fileA.txtrnrn# sort -r fileA.txt

n

16. VI Command

n

Vi is the most popular text editor available in most UNIX-like OS. Below examples open file in read-only with -R option. Press ‘:q‘ to quit from vi windows.

n

# vi -R /etc/shadows

n

To learn more about vi editor, read our articles:

n

n

17. SSH Command (Secure Shell)

n

SSH command is used to login into the remote host. For example, the below ssh command will connect to the remote host (192.168.50.2) using the user as Narad.

n

# ssh [emailxa0protected]

n

To check the version of ssh use the option -V (uppercase) shows version of ssh.

n

# ssh -VrnrnOpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  31 Mar 2020

n

To learn more about SSH, read our articles:

n

n

18. Ftp or sftp Command

n

ftp or sftp command is used to connect to remote ftp host. ftp is (file transfer protocol) and sftp is (secure file transfer protocol). For example, the below commands will connect to ftp host (192.168.50.2).

n

# ftp 192.168.50.2rnrn# sftp 192.168.50.2

n

Putting multiple files in remote host with mput similarly, we can do mget to download multiple files from the remote host.

n

# ftp > mput *.txtrnrn# ftp > mget *.txt

n

19. Systemctl Command

n

Systemctl command is a systemd management tool that is used to manage services, check running statuses, start and enable services and work with the configuration files.

n

# systemctl start httpd.servicern# systemctl enable httpd.servicern# systemctl status httpd.servicern

n

20. Free command

n

The freexa0command shows free, total, and swap memory information in bytes.

n

# freern             total       used       free     shared    buffers     cachedrnMem:       1030800     735944     294856          0      51648     547696rn-/+ buffers/cache:     136600     894200rnSwap:      2064376          0    2064376

n

Free with -t options show total memory used and available to use in bytes.

n

# free -trn             total       used       free     shared    buffers     cachedrnMem:       1030800     736096     294704          0      51720     547704rn-/+ buffers/cache:     136672     894128rnSwap:      2064376          0    2064376rnTotal:     3095176     736096    2359080

n

21. Top Command

n

top command displays processor activity of your system and also displays tasks managed by kernel in real-time. It’ll show processor and memory are being used.

n

Using the top command with u the option will display specific User process details as shown below. Press ‘O‘ (uppercase letter) to sort as desired by you. Press ‘q‘ to quit from the top screen.

n

# top -u tecmintrnrntop - 11:13:11 up  3:19,  2 users,  load average: 0.00, 0.00, 0.00rnTasks: 116 total,   1 running, 115 sleeping,   0 stopped,   0 zombiernCpu(s):  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%strnMem:   1030800k total,   736188k used,   294612k free,    51760k buffersrnSwap:  2064376k total,        0k used,  2064376k free,   547704k cachedrnrnPID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMANDrn1889 tecmint   20   0 11468 1648  920 S  0.0  0.2   0:00.59 sshdrn1890 tecmint   20   0  5124 1668 1416 S  0.0  0.2   0:00.44 bashrn6698 tecmint   20   0 11600 1668  924 S  0.0  0.2   0:01.19 sshdrn6699 tecmint   20   0  5124 1596 1352 S  0.0  0.2   0:00.11 bash

n

For more about top command, we’ve already compiled a list of 12 TOP Command Examples in Linux.

n

22. Tar Command

n

The tar command is used to compress files and folders in Linux. For example, the below command will create an archive for /home directory with the file name archive-name.tar.

n

# tar -cvf archive-name.tar /home

n

To extract the tar archive file use the option as follows.

n

# tar -xvf archive-name.tar

n

To understand more about tar command we’ve created a complete how-to guide on tar command at 18 Tar Command Examples in Linux.

n

23. Grep Command

n

grep command search for a given string in a file. Only tecmint user displays from /etc/passwd file. we can use -i an option for ignoring case sensitivity.

n

# grep tecmint /etc/passwdrnrntecmint:x:500:500::/home/tecmint:/bin/bash

n

24. Find Command

n

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

n

# find / -name tecmintrnrn/var/spool/mail/tecmintrn/home/tecmintrn/root/home/tecmint

n

For a complete guide on Linux find command examples fount at 35 Practical Examples of Linux Find Command.

n

25. lsof Command

n

lsof mean List of all open files. Below lsof a command list of all opened files by user tecmint.

n

# lsof -u tecmintrnrnCOMMAND  PID    USER   FD   TYPE     DEVICE SIZE/OFF   NODE NAMErnsshd    1889 tecmint  cwd    DIR      253,0     4096      2 /rnsshd    1889 tecmint  txt    REG      253,0   532336 298069 /usr/sbin/sshdrnsshd    1889 tecmint  DEL    REG      253,0          412940 /lib/libcom_err.so.2.1rnsshd    1889 tecmint  DEL    REG      253,0          393156 /lib/ld-2.12.sornsshd    1889 tecmint  DEL    REG      253,0          298643 /usr/lib/libcrypto.so.1.0.0rnsshd    1889 tecmint  DEL    REG      253,0          393173 /lib/libnsl-2.12.sornsshd    1889 tecmint  DEL    REG      253,0          412937 /lib/libkrb5support.so.0.1rnsshd    1889 tecmint  DEL    REG      253,0          412961 /lib/libplc4.so

n

For more lsof command examples visit 10 lsof Command Examples in Linux.

n

26. last command

n

With the last command, we can watch the user’s activity in the system. This command can execute normal users also. It will display complete user’s info like terminal, time, date, system reboot or boot, and kernel version. A useful command to troubleshoot.

n

# lastrnrntecmint  pts/1        192.168.50.1     Tue Sep 18 08:50   still logged inrntecmint  pts/0        192.168.50.1     Tue Sep 18 07:59   still logged inrnreboot   system boot  2.6.32-279.el6.i Tue Sep 18 07:54 - 11:38  (03:43)rnroot     pts/1        192.168.50.1     Sun Sep 16 10:40 - down   (03:53)rnroot     pts/0        :0.0             Sun Sep 16 10:36 - 13:09  (02:32)rnroot     tty1         :0               Sun Sep 16 10:07 - down   (04:26)rnreboot   system boot  2.6.32-279.el6.i Sun Sep 16 09:57 - 14:33  (04:35)rnnarad    pts/2        192.168.50.1     Thu Sep 13 08:07 - down   (01:15)

n

You can use last with username to know for specific user’s activity as shown below.

n

# last tecmintrnrntecmint  pts/1        192.168.50.1     Tue Sep 18 08:50   still logged inrntecmint  pts/0        192.168.50.1     Tue Sep 18 07:59   still logged inrntecmint  pts/1        192.168.50.1     Thu Sep 13 08:07 - down   (01:15)rntecmint  pts/4        192.168.50.1     Wed Sep 12 10:12 - 12:29  (02:17)

n

27. ps command

n

The ps command displays processes running in the system. The below example show the init to process only.

n

# ps -ef | grep initrnrnroot         1     0  0 07:53 ?        00:00:04 /sbin/initrnroot      7508  6825  0 11:48 pts/1    00:00:00 grep init

n

28. kill command

n

Use 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.

n

# ps -ef | grep initrnroot         1     0  0 07:53 ?        00:00:04 /sbin/initrnroot      7508  6825  0 11:48 pts/1    00:00:00 grep initrnrn# kill- 9 7508

n

29. rm command

n

rm command used to remove or delete a file without prompting for confirmation.

n

# rm filename

n

Use the -i option to get confirmation before removing it. Using options ‘-r‘ and ‘-f‘ will remove the file forcefully without confirmation.

n

# rm -i test.txtrnrnrm: remove regular file `test.txt'?

n

30. mkdir command example.

n

mkdir command is used to create directories under Linux.

n

# mkdir directoryname

n

This is a handy day-to-day used basic commands in Linux / Unix-like operating system. Kindly share through our comment box if we missed out.

n

‘]