Linux display login users

How to see users currently logged in to Linux

Linux is a multi-user operating system that allows multiple users to access the system at the same time.

As a Linux system administrator, you have to check who are logged into the system before starting to work on any issues, especially when you have a team members spread across multiple locations. Because, if multiple users are making the changes in the same configuration file, it may create additional problems.

So, make sure nobody is currently working on the issue before you take it up. To avoid these things, we need to check who all are logged into the system and what are they doing.

In this tutorial, we will show you how to check the current logged-in users with several commands in Linux.

Knowing more than one command to find the same information will not hurt you, and do not hesitate to check the alternate options.

Method-1: Checking logged-in users with ‘w’ command

‘w command’ shows who are logged-in and what are they doing. It displays information about current users on the machine by reading the file /var/run/utmp , and their processes /proc .

w command output comes with header information, which displays system activity such as current time, system up time, how many users are currently logged-in, and the system load (which averages for the past 1, 5, and 15 minutes)

w command contains the following values:

login user name, tty number, remote host, user’s login time, idle time, JCPU (time used by all processes attached to the tty), PCPU (time used by the current process), and which commands are currently being executed by the users. Please see below:

# w 17:13:34 up 1:52, 1 user, load average: 0.11, 0.18, 0.15 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 203.99.204.108 15:22 6.00s 0.18s 0.00s w

Method-2: Identifying who is Logged-in using ‘who’ command

‘who command’ shows information about users who are currently logged in. It uses ‘/var/run/utmp’ & ‘/var/log/wtmp’ files to get those details.

  • /var/run/utmp: It contains information about the users who are currently logged onto the system. Who command is used to fetch the information from the file.
  • /var/log/wtmp: It contains historical utmp. It keeps the users login and logout history. The last command uses this file to display the information.
Читайте также:  Unable to open display astra linux

who command output contains the following values such as login user name, tty number, date & time, and remote host.

# who root pts/0 2017-05-31 15:22 (203.99.204.108)

Method-3: How to see Logged in users with ‘whoami’ command

whoami is basically the concatenation of the strings “who”,”am”,”i” as whoami. It displays the username of the current user. It’s similar to running the id command with the options -un as shown below:

Also, when you use whoami with space (who am i) that will give you a different output. It will display more details compared to whoami command as shown below:

$ who am i daygeek pts/1 2019-06-17 22:01 (192.168.1.6)

“id” command prints user and group information for a specified username, but we can add -un option with the “id” command to display all the currently logged-in users as shown below:

Method-4: Using users command

‘users command’ prints the usernames of users currently logged in to the current host. It uses /var/run/utmp & /var/log/wtmp files to get the details as shown below:

Method-5: Show currently logged-in users with ‘finger’ command

‘finger’ is a utility, which allows users to see the information about system users (login name, home directory, name, how long they’ve been logged in to the system, etc.).

Finger utility is available in all major Linux distributions, but it doesn’t come installed by default. Use distribution package manager to install “finger” on your system.

$ finger Login Name Tty Idle Login Time Office Office Phone magi daygeek tty7 7 Jun 1 16:05 (203.99.204.108)

Bonus Tips

Additionally, you can use the following methods to identify who all are logged-in on your system:

Bonus Tips-1: How to show current logged-in users with ‘last’ command

‘last command’ shows a list of last logged in users by searching the data from /var/log/wtmp file. Also, it shows the system reboot information.

last” command output contains login user name, tty number, remote host, date, login time, logout time, and the total duration (working time).

Run the following command to show who all logged-in today. Also, you can check who’s currently logged in by filtering with the “still logged in” string.

# last -p today linuxgee tty2 tty2 Thu Mar 4 14:27 gone - no logout linuxgee : : Thu Mar 4 14:27 gone - no logout

Bonus Tips-2: Manual Way to check who’s logged-in

Last but not the least, we can get a list of logged in users on Linux machine manually by using less commands or more commands or head command or tail command, followed by the log file location.

User authentication logs are located @ /var/log/secure for RHEL based systems & /var/log/auth.log for Debian based systems.

$ head -5 /var/log/auth.log Jun 1 16:05:01 daygeek CRON[1944]: pam_unix(cron:session): session opened for user root by (uid=0) Jun 1 16:05:01 daygeek CRON[1944]: pam_unix(cron:session): session closed for user root Jun 1 16:05:44 daygeek lightdm: pam_unix(lightdm-greeter:session): session closed for user lightdm Jun 1 16:05:44 daygeek lightdm: pam_unix(lightdm:session): session opened for user magi by (uid=0) Jun 1 16:05:44 daygeek systemd: pam_unix(systemd-user:session): session opened for user magi by (uid=0)

Over to You

In this guide, you learnt how to find out who all are currently logged-in on your Linux system employing different commands.

Читайте также:  Арч линукс настройка звука

If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!

Источник

How to Print the Usernames of Currently Logged-In Users in Linux

A Linux system can have many users logged in at a go. The Linux administrator checks the logged-in users and can see what each user is doing on their terminal. Checking who is currently logged in is one way of enhancing the security of your system.

This guide discusses how you can print the usernames of the currently logged-in users in Linux.

Different Ways of Printing the Usernames of Logged-In Users in Linux

Linux can be accessed remotely, and many users utilize a protocol like SSH to access their Linux systems remotely. Keeping a tab on who is logged into the system is part of the administrator’s job. Let’s see the various ways to achieve that.

1. Using the “W” Command

A command like “w” retrieves the details of the currently logged users from the /var/run/utmp file when executed. It then displays the login details in a table-like manner for analysis.

You will get an output like the one in the previous image. Let’s understand what details it gives:

  • USER – They represent the usernames of the logged-in users.
  • TTY – It represents the terminal in which the user is logged in with.
  • FROM – This is the hostname or the IP address from where a user is logged into the system.
  • IDLE – It represents the idle time of a given user.
  • LOGIN@ – It is when a given user logged into the system.
  • WHAT – It outlines the details of the current process that the user is doing.
  • JCPU – It represents the total time consumed by all processes in the TTY.
  • PCPU – It represents the time used by the active process represented under the WHAT of each user.

That’s how to interpret the details about the usernames that are currently logged in Linux.

The “w” command lets you narrow down the login details of a particular user. For that, begin by listing the usernames of logged-in users as we did earlier. Then, specify one username to get more details.

Читайте также:  Usb read error linux

When using “w” to print the usernames of logged-in users, you can reduce the information displayed by adding the -s flag as shown in the following:

2. Using Who

When you want to print the username of logged-in users with less details, the who command is your best choice. The command outputs the username, the terminal device, the login date and time, and the hostname or the logged-in user’s IP address.

Adding the -a option gives the details of the last system boot and other information while still printing the usernames of logged users.

3. Using Whoami

Unlike who, the whoami command only prints the username of the logged user of the active shell. Therefore, you can’t use it to monitor others.

The command mainly applies when you want to get your username and confirm who is using the active shell.

Its output only gives the username of the active shell.

4. Using ID

When combined with the –un flag, the id command works the same as the whoami. It displays the username of the active shell as follows:

You can’t use this method to get the usernames of other logged-in users.

5. Using Users

Linux offers the users command which prints the usernames of all currently logged-in users without adding any details. You will only see their username as displayed in the following:

6. Using Finger

Finger is a Linux package that acts as a lookup utility. When executed, it shows the user details such as their terminal name, login name, idle time, and login time.

To use the lookup utility, install it first using the following command for Ubuntu:

Once the package installs, run it as follows to look up the user details:

7. Using the Last Command

In Linux, last displays the log details of all the login sessions for a given time range. It shows how and when the different users logged into the system and who is currently logged in.

The previous output is extensive. You can narrow it down. For instance, you can retrieve the last five login details by adding 5 to the command.

That way, you narrow the output for easy system monitoring. The logged-in users have the “logged in” message on the right.

Conclusion

When different users are logged into the system, it’s possible to monitor them and print their usernames using various options including the “who”, “w”, “last”, “finger”, “users”, and whoami” commands. We’ve seen the examples of using each of them to retrieve the usernames. Try them and find which works best for your case.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

Оцените статью
Adblock
detector