RHCE Study Guide

This tutorial explains basic Linux system administration commands in details with examples.

How to list currently logged in users

When a user logged in, Linux stores his information in /var/run/utmp file. This information includes username, terminal number and login time. Although Linux allows us to read this file directly. But if we read this file directly, output will not be properly formatted. To get properly formatted output, we have to use who command. who command fetches the information from /var/run/utmp file and formats it before displaying.

linux who command

who command will list information about all logged users. If we want to get information only about ourselves, we have to use who am i command instead of who command. who am i command provides information only about the user who execute it.

Sometime we needs more detailed information about active users including what each user is doing. In such a case we can use w command. w command provides more detailed information about active users. This information includes :-

System specific information

  • Current time of day
  • System uptime
  • Total number of active users (currently logged in)
  • Average load (number of jobs in run queue)

User specific information

  • User login name
  • Terminal number from which user is logged in
  • Host name (Name of system where user is logged in)
  • Login session time (the time the user logged on)
  • Last activity time (the time since the user last typed anything)
  • JCPU time (the time taken by all process excluding past background jobs.)
  • PCPU time (the time taken by current process)
  • Current process

linux w command

How to know the terminal number

To know the terminal number we can use tty command. This command will display the terminal name where the user (who executes the command) is logged in.

linux tty command

How to know the system uptime

To know how long the system has been up we can use uptime command. Besides system’s uptime it also provides following information:-

Current system time, currently logged in user, system load average for past 1, 5 and 15 minutes respectively.

All this information will be displayed in a single line.

linux uptime command

Same information can be obtained from w command. First line of output (w command) will display the same information.

How to view the last login attempts

Just like utmp file which is used to keep track of active user session, Linux uses wtmp file to keep track of last user sessions. In this file Linux keeps record of all successful login session including username, terminal number where the user logged in and the duration till user stayed logged in. All failed login attempts are recorded in btmp file.

File Stored information Command to read the file
/var/run/utmp Active user sessions who, w, who am i, whoami, logname
/var/log/wtmp Last user sessions last
/var/log/lastlog Recent user sessions lastlog
/var/log/btmp Failed login attempts lastb

Following figure illustrates the use of last, last reboot and lastb command.

linux last command

Following figure demonstrates the example of lastlog command.

linux lastlog command

How to view login name

To view login name we can use logname command.

linux logname command

We can also use whoami command to view the login name. But whoami command display the current user name instead of login name. Linux allows us to switch between users. If user has switched, whoami command will display the current username. While logname command displays the username of user who initiated the shell prompt.

How to view operating system name

Without any option uname command will display the operating system name. With –a option it will display the basic system information such as hostname, operating system name, hardware platform and kernel information (name, version, built etc.).

linux uname command

uname command supports following options to view the information individually.

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 above information

How to view & set hostname

To view hostname we can use hostname command. To view more detailed information including hardware information we can use hostnamectl command. hostnamectl command also allows us to set hostname directly from command prompt. To change hostname use set-hostname option with hostnamectl command.

linux hostame command

How to view or set date and time

Just like hostname, we have two commands to view or set date and time; date and timedatectl. Without any option both commands will display current date and time. date command provides basic information while timedatectl provides more detailed information.

To change date and time from date command use following command :-

# date --set "2017-06-04 06:30:00" (This command will set date and time to 2017 06 04 06:30:00 )

To set same date and time from timedatectl use following commands:-

# timedatectl set-time 2017-06-04  (This command will set date to 2017 06 04)
# timedatectl set-time 06:30 (This command will set time to 06:30)

linux date command

How to find command location

Linux commands are the link of executable files. When we hit enter key after typing a command, shell finds and executes the matching script file. To know the location of executable file we can use which command. From source directory we can directly execute the script file using absolute path.

linux which command

How to count line, word and characters in a file

To count the line, word and characters we can use wc command. wc command will display the number of lines, words and characters in a file.

linux wc command

How to view the running process

ps –ef command will list all running process. From output we can filter specific process with grip command. Process will be displayed with PID (Process ID). PID allows us to kill particular process. For example following figure illustrates finding and killing Firefox web browser process.

linux ps command

How to view the real time hardware usages status

To view the real time hardware uses, we can use top command. top command displays the real time hardware usage such as CPU, memory, process and much more.

linux top command

To exit from output press q key.

How to view hardware information

To get hardware information we can use lspci, lssci, lsusb and lscpu commands.

lspci :- This command provides information about pci buses and their attached devices.

lssci :- This command provides information about scsi devices.

lsusb :- This command display information about USB ports and attached devices.

lscpu :- This command display information about CPU.

linux hardware info commands

That’s all for this article.