Linux System Administration Commands Explained with Examples

This tutorial explains essential system administration commands on Linux. These commands form the foundation of Linux system administration. Understanding how to use these commands help you manage and troubleshoot your system effectively.

Listing the currently logged-in users

When a user logs in, Linux stores his information in the /var/run/utmp file. This information includes his username, terminal number and login time. It stores this information in a raw text format. You can use the cat command to view this information.

#cat /var/run/utmp

Since it saves information in raw text format and the cat command does not change the text format, the output of this command looks messy. You can use the following two commands to view the adequately formatted output.

#who
#who am i

Both commands work similarly. Both commands read raw text from the /var/run/utmp file and adequately format it before displaying it on the command prompt. The only difference between both commands is that the who command displays information about all users while the who am i command displays information only about the user who executes it.

Linux system administration commands Example 1

Knowing the last successful login, failed login attempts and system reboot time

Like keeping track of active users, Linux also records previous successful login sessions, failed login attempts and last system reboots. It stores information about previous successful login sessions in the file /var/log/wtmp. It uses the file /var/log/btmp to store information about the failed login attempts. You can read these files directly or use the following commands.

#last
#last reboot
#lastb

The last command prints the list of users who successfully accessed the system. This command does not tell you what the user did after login. It only tells you which user logged in at what time on which terminal and how long he logged in.

The last reboot command prints the last reboot time of the system. This command lets you know when the system rebooted previous times.

The lastb command prints the list of failed login attempts. This command lets you know which user used the wrong password to log in on which terminal and at what time.

Linux system administration commands Example 2

Getting detailed information about active users

If you need more detailed information about active users, including what they are doing, you can use the w command. The w command provides two types of information: system-specific and user-specific.

System specific information
  • Current time
  • System uptime
  • Total number of active users (currently logged in)
  • Average load (number of jobs in the run queue)
User-specific information
  • User login name
  • The terminal number from which the user logged in
  • Host name (Name of the system on which the user logged in)
  • Login session time (the time the user stayed logged in)
  • Last activity time (the time since the user last typed anything)
  • JCPU time (CPU time used by all processes excluding past background jobs.)
  • PCPU time (the time taken by the current process)
  • Current process

Linux system administration commands Example 3

Knowing the system uptime

You can use the uptime command to know how long the system has been up. Besides the system's uptime, it also provides information about the current system time, number of currently logged-in users, and system load average for the past 1, 5 and 15 minutes.

You can obtain the same information from the w command. However, the difference between both commands is that the w command provides this information with the information of active users. If you want to obtain both types of information, use the w command. If you only need system-specific information, use the uptime command.

Linux system administration commands example 4

Viewing login name/username

You can view the login name using the logname or the whoami command. Both commands display the username. The difference between both commands is that the logname command displays the username of the user account used to log in. In contrast, the whoami command displays the username of the current user account.

Let's take an example to understand the difference between both commands.

Login from a user account and use both commands to print the username. Both commands will display the same result.

Change the user account through the su command and rerun both commands. This time, the whoami command will display the username of the new user account, while the logname command will still display the old login name.

Now log out of the new account and rerun both commands. This time, both commands will display the same result again.

Linux system administration commands Example 5

Knowing the hostname, kernel version number, processor type and hardware architecture

You can use the uname command to view the basic system information such as hostname, operating system name, hardware platform and kernel information (name, version, built, etc.) Without any option, this command only displays the operating system name. To view other information, you have to use the related option. The following table lists some standard options and their description.

Option Description
-s kernel name
-n node name
-r kernel release
-v kernel build date
-m hardware name
-p processor type
-i hardware platform
-o OS name
-a all the above information

Linux system administration commands Example 6

Viewing and setting the hostname

You can view the hostname using the hostname or hostnamectl command. To view the hostname only, use the hostname command. To view the detailed information about the hostname and the hardware information, use the hostnamectl command.

Linux system administration commands Example 7

Viewing and setting the date and time

To view or set the date and time, you can use the date or timedatectl command. Without any option, both commands display the current date and time. The date command shows basic information, while the timedatectl displays detailed information.

Use the following syntax to change the date and time from the date command.

# date --set "YYYY-MM-DD HH:MM:SS"

Use the following syntaxes to change the date and time using the timedatectl command.

# timedatectl set-time YYYY-MM-DD
# timedatectl set-time HH:MM:SS

Linux system administration commands Example 8

Finding the command location

Every command has an associated script file. When we type a command at the command prompt and hit Enter, the shell finds the related script file and executes it. You can use the which command to know the location of a command's script file.

Linux system administration commands Example 9

Counting the lines, words and characters

You can use the wc command to count a file's lines, words and characters. The wc command counts and displays the number of lines, words and characters of the supplied file.

Linux system administration commands Example 10

Viewing all running processes in the system

The ps –ef command lists all running processes in the system. To know whether a particular process is running, you can filter this command's output with the grep command. For example, you can use the following command to determine whether Firefox is running.

#ps -ef |grep firefox

Terminating a halted process

Linux assigns a unique process ID to each running process. You can use it to terminate a halted process. To know the process ID of the halted process, use the following command.

#ps -ef |grep [name of halted process]

Once you know the process ID, you can use the kill command to terminate it.

Linux system administration commands Example 11

Viewing the real-time usage of hardware resources

To view the real-time hardware usage, you can use the top command. The top command displays the real-time usage of hardware resources such as CPU and memory.

Linux system administration commands example 12

To terminate the command, press the q key.

Getting detailed hardware information

You can use the lspci, lsscsi, lsusb and lscpu commands to view information about particular hardware types.

  • lspci:- This command provides information about the PCI buses and their attached devices.
  • lsscsi:- This command provides information about the scsi devices.
  • lsusb:- This command displays information about the USB ports and attached devices.
  • lscpu:- This command displays information about the CPU.

Linux system administration commands Example 13

Conclusion

Linux system administration involves commands. This tutorial explained the essential system administration commands through examples. You can learn and use these commands to effectively manage a Linux system.

ComputerNetworkingNotes Linux Tutorials Linux System Administration Commands Explained with Examples

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com