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.
Commands to get user-related information:
1. id: 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
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: 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.
3. getent: This 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: To see all the username and user ids. This 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: This command will print the usernames of logged-in to the current host.
users
This is the only user logged in currently.
6. who: To show who is logged-in. This 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
7. w: 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: 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: This 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 Script
Now we will create a shell script using some above-mentioned commands to get user details. We are approaching the solution in a way that the user is asked for input by given suggestions. That input will be then used to check against the available cases, and then the matched case will be allowed to run.
Open gedit file:
Open any editor according to your preferences, we have used gedit editor because of its simple user interface and the color combination present.
gedit userAccounts.sh
Code:
Here in the userAccounts.sh we will write our code, and use switch cases to compare the user input. We have used commands like lslogins, who, groups, etc. which will help us to satisfy the user requirements. You could find the use of these commands more extended above. So, let us begin the script.
#!/bin/bash #here we are going to develop a script for various options on user accounts echo -e "\n [ 1 ] for listing all the user accounts name \n [ 2 ] for counting the number of logged-in user accounts \n [ 3 ] for listing the names of currently logged-in users\n [ 4 ] for checking the groups to which the current user belong \n" #Now take user input read userInput #Now we will use switch cases for various input operations case $userInput in 1) #syntax lslogins <option[=output field]> lslogins -o USER ;; 2) #syntax who <option> <user optional> #grep used to filter who --count | grep users ;; 3) #-q option is to count the number of users and print the logged-in users. # instead of -q, --count can also be used. # -v is used to exclude any pattern who -q | grep -v users ;; 4) #syntax groups <option> [USERNAME] groups ;; *) echo -e "Please Enter Correct Input \n" ;; esac
Grant executable permissions
Executable permissions must be granted to the files to make them run or execute on the system. We could also use “777” instead of “+x” in the chmod command. Also please run the script as root to
# chmod +x userAccounts.sh
Run the script
./userAccounts.sh
Example 1:
./userAccounts.sh 1
Example 2:
./userAccounts.sh 2
Example 3:
./userAccounts.sh 3
Hence, we were able to find out various login-related results using our shell script.
Please Login to comment...