Commands to get user-related information
1. id Command to Find How Many Users are Logged In
The id command is used to print the user and group information for the specified USER.
-a ignore, for compatibility with other versions
-Z, --context print only the security context of the process
-g, --group print only the effective group ID
-G, --groups print all group IDs
-n, --name print a name instead of a number, for -ugG
-r, --real print the real ID instead of the effective ID, with -ugG
-u, --user print only the effective user ID
-z, --zero delimit entries with NUL characters, not whitespace
id
This id command has produced all the user identifiers, group identifiers, and groups. If you want only a group identifier, use the below command.
id -G
2. groups Command
This will print the group to which the specified user belongs. If no specific username is given, it will search for the current users. Use the below command for the current user.
groups
3. getent Command
The getent command displays entries from the databases.
-i, --no-idn disable IDN encoding
-s, --service=CONFIG Service configuration to be used
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
Let us see the version of getent program present on our system.
getent -V
4. lslogins Command to Find How Many Users are Logged In
To see all the username and user ids. The lslogins command provides a list of several features like UID, USER, LAST-LOGIN, etc.
-a, --acc-expiration display info about passwords expiration
-c, --colon-separate display data in a format similar to /etc/passwd
-e, --export display in an export-able output format
-f, --failed display data about the users' last failed logins
-G, --supp-groups display information about groups
-g, --groups=<groups> display users belonging to a group in <groups>
-L, --last show info about the users' last login sessions
-l, --logins=<logins> display only users from <logins>
-n, --newline display each piece of information on a new line
--noheadings don't print headings
--notruncate don't truncate output
5. users Command to Print Current Logged-in User
The users command will print the usernames of logged-in to the current host.
users
This is the only user logged in currently.
6. who Command to Display who logged In
To show who is logged-in. The who command lists the users with id and the time and date of user login.
-a, --all same as âb, -d, --login, -p, -r, -t, -T, -u
-b, --boot time of last system boot
-d, --dead print dead processes
-H, --heading print line of column headings
-l, --login print system login processes
--lookup attempt to canonicalize hostnames via DNS
-m only hostname and user associated with stdin
-p, --process print active processes spawned by init
-q, --count all login names and number of users logged in
who
7. w Command to Show the Logged In User
The w command shows the logged-in user accounts and also shows what they are doing.
-h, --no-header do not print header
-u, --no-current ignore current process username
-s, --short short format
-f, --from show remote hostname field
-o, --old-style old style output
-i, --ip-addr display IP address instead of hostname (if possible)
This has some more features and columns than who, to give more detailed information about users.
w
8. last or lastb Command
The commands last and lastb shows a listing of last logged in users
-<number> how many lines to show
-a, --hostlast display hostnames in the last column
-d, --dns translate the IP number back into a hostname
-f, --file <file> use a specific file instead of /var/log/wtmp
-F, --fulltimes print full login and logout times and dates
-i, --ip display IP numbers in numbers-and-dots notation
-n, --limit <number> how many lines to show
-R, --nohostname don't display the hostname field
-s, --since <time> display the lines since the specified time
-t, --until <time> display the lines until the specified time
-p, --present <time> display who were present at the specified time
This provides all the login details of several users according to date and time.
last
9. lastlog Command to Display Recent Login Users
The lastlog command will produce a report of all the recent login users. This can also create a single-user report if specified.
-b, --before DAYS print only lastlog records older than DAYS
-C, --clear clear lastlog record of a user (usable only with -u)
-h, --help display this help message and exit
-R, --root CHROOT_DIR directory to chroot into
-S, --set set lastlog record to the current time (usable only with -u)
-t, --time DAYS print only lastlog records more recent than DAYS
-u, --user LOGIN print lastlog record of the specified LOGIN
This tells about the latest log of the users.
lastlog
Shell Scripts to Find How Many Users are Logged In
Every operating system provides a feature of multiple user accounts. Linux-based operating systems have some commands or functionalities to check user accountsâ details and change them. This ability is mainly used by the admin account user that is the root user, to provide permissions and access to different users. The admin can also check how many users are currently logged in, how many are logged out, and the login time. Here in this article, we will explore all these ways and also write a shell script to complete these tasks efficiently.
Table of Content
- Commands to get user-related information:
- Shell Script to Find How Many Users are Logged In
Contact Us