Introduction
In this lab, we will explore the Linux w command, which provides information about users currently logged into the system and their activities. We will learn how to analyze user login sessions, monitor system load, and utilize various options of the w command to gather detailed insights about the system's performance and user behavior. Understanding the w command is essential for effective system monitoring and management on Linux systems.
Understand the w Command
In this step, we will explore the w command in Linux, which provides information about users currently logged into the system and their activities.
The w command displays the following information:
- Username of the logged-in users
- The terminal they are logged in from
- The time they logged in
- How long they have been idle
- What they are currently doing
Let's start by running the w command in the terminal:
w
Example output:
17:30:32 up 1 day, 23:03, 0 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
The output shows that there are currently no users logged in. Let's log in as another user and see the output:
sudo su - user2
w
Example output:
17:31:00 up 1 day, 23:03, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user2 pts/0 172.17.0.1 17:30 0.00s 0.01s 0.00s w
The output now shows that the user user2 is logged in from the terminal pts/0 and has been idle for 0.00 seconds. The WHAT column shows that the user is currently running the w command.
You can also use the w command with various options to get more detailed information about the logged-in users and system load:
w -h: Omit the header linew -s: Short format, show only name, tty, and timew -i: Show idle time in short format (minutes)w username: Show information for a specific user
Understanding the w command is essential for monitoring user activity and system performance on your Linux system.
Analyze User Login Sessions
In this step, we will learn how to analyze user login sessions using the w command and other related tools.
First, let's list all the currently logged-in users using the w command:
w
Example output:
17:35:01 up 1 day, 23:07, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user2 pts/0 172.17.0.1 17:30 0.00s 0.01s 0.00s w
The output shows that the user user2 is currently logged in from the terminal pts/0.
To get more detailed information about user login sessions, we can use the who command:
who
Example output:
user2 pts/0 2023-04-18 17:30 (172.17.0.1)
The who command displays the username, terminal, login time, and the host from which the user logged in.
Another useful command is last, which shows a list of the last logged-in users:
last
Example output:
user2 pts/0 172.17.0.1 Tue Apr 18 17:30:00 2023 - 17:35:01 (0:05)
reboot system boot 4.15.0-162-gene Tue Apr 18 17:31:49 2023 - 17:35:01 (0:03)
user1 pts/0 172.17.0.1 Tue Apr 18 17:29:00 2023 - 17:30:00 (0:01)
The last command shows the username, terminal, host, login time, logout time, and the duration of the session.
By analyzing the output of these commands, you can get a clear understanding of the user login sessions on your system, which can be useful for monitoring and troubleshooting purposes.
Monitor System Load and Resource Utilization
In this step, we will learn how to monitor the system load and resource utilization using the w command and other related tools.
The w command provides information about the system load, which is displayed in the form of three load averages:
w
Example output:
17:40:01 up 1 day, 23:12, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user2 pts/0 172.17.0.1 17:30 0.00s 0.01s 0.00s w
The three load average numbers represent the system load over the last 1 minute, 5 minutes, and 15 minutes, respectively. A load average of 0.00 indicates that the system is not under any significant load.
To get more detailed information about system resource utilization, we can use the top command:
top
Example output:
top - 17:40:42 up 1 day, 23:12, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 105 total, 1 running, 104 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2044880 total, 1605244 free, 160676 used, 278960 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 1603204 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 41824 4464 2896 S 0.0 0.2 0:01.37 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
The top command provides real-time information about the system's CPU, memory, and process utilization. You can use it to identify the processes consuming the most resources and take appropriate actions.
Additionally, you can use the free command to get information about the system's memory usage:
free -h
Example output:
total used free shared buff/cache available
Mem: 1.9G 156M 1.5G 1.1M 278M 1.5G
Swap: 1.0G 0B 1.0G
The free command displays the total, used, and available memory on the system, as well as the swap space usage.
By monitoring the system load and resource utilization using these tools, you can identify performance bottlenecks and take appropriate actions to optimize your system's performance.
Summary
In this lab, we learned how to use the w command in Linux to understand user login sessions and monitor system load and resource utilization. We explored the information provided by the w command, including the username of logged-in users, the terminal they are using, the time they logged in, their idle time, and their current activity. We also learned how to use various options with the w command to get more detailed information, such as omitting the header line, showing only the name, terminal, and login time, and displaying idle time in a shorter format. Finally, we discussed how to analyze user login sessions and system performance using the w command and other related tools.



