How to use the command 'lslogins' (with examples)
- Linux
- December 25, 2023
The ’lslogins’ command is used to obtain information about users on a Linux system. It provides various options to display different aspects of user accounts, groups, and login details.
Use case 1: Display users in the system
Code:
lslogins
Motivation: This use case is useful to quickly get an overview of all the users present on the Linux system.
Explanation: The ’lslogins’ command, when used without any options, displays information about all the users in the system. It provides details such as user ID, group ID, home directory, and user account status.
Example output:
UID USER USER_NAME. GROUP_ID HOME_DIRECTORY SHELL
1001 user1 User One 1001 /home/user1 /bin/bash
1002 user2 User Two 1002 /home/user2 /bin/bash
1003 user3 User Three 1003 /home/user3 /bin/bash
Use case 2: Display users belonging to a specific group
Code:
lslogins --groups=group_name
Motivation: This use case is helpful to find users who are members of a specific group. It allows for easy identification and management of group members.
Explanation: The ‘–groups’ option followed by a group name specifies that only users belonging to that particular group should be displayed.
Example output:
UID USER USER_NAME. GROUP_ID HOME_DIRECTORY SHELL
1001 user1 User One 1001 /home/user1 /bin/bash
1002 user2 User Two 1002 /home/user2 /bin/bash
Use case 3: Display user accounts
Code:
lslogins --user-accs
Motivation: This use case is useful to get a list of all the user accounts present on the system. It helps in monitoring and managing user accounts.
Explanation: The ‘–user-accs’ option displays all the user accounts available on the Linux system.
Example output:
UID USER USER_NAME. GROUP_ID HOME_DIRECTORY SHELL
1001 user1 User One 1001 /home/user1 /bin/bash
1002 user2 User Two 1002 /home/user2 /bin/bash
1003 user3 User Three 1003 /home/user3 /bin/bash
Use case 4: Display last logins
Code:
lslogins --last
Motivation: This use case helps in checking the last login details of users. It provides insights into user activity and can aid in detecting potential security breaches.
Explanation: The ‘–last’ option displays the last login information, including the login time, duration, and terminal used, for all users on the Linux system.
Example output:
UID USER USER_NAME. TERMINAL LOGIN_TIME DURATION IP_ADDRESS
1001 user1 User One tty1 2022-01-01 10:00:00 00:10:00 192.168.0.1
1002 user2 User Two pts/0 2022-01-01 09:00:00 00:05:00 192.168.0.2
1003 user3 User Three pts/1 2022-01-01 08:00:00 00:15:00 192.168.0.3
Use case 5: Display system accounts
Code:
lslogins --system-accs
Motivation: This use case is helpful to identify system accounts separately from regular user accounts. System accounts are usually used for running system services and should be treated differently.
Explanation: The ‘–system-accs’ option displays only the system accounts present on the Linux system, excluding regular user accounts.
Example output:
UID USER USER_NAME. GROUP_ID HOME_DIRECTORY SHELL
1001 user1 User One 1001 /home/user1 /bin/bash
1002 user2 User Two 1002 /home/user2 /bin/bash
Use case 6: Display supplementary groups
Code:
lslogins --supp-groups
Motivation: This use case is helpful for identifying the supplementary groups a user belongs to. Supplementary groups provide additional privileges and determine access permissions.
Explanation: The ‘–supp-groups’ option displays the supplementary groups associated with each user on the Linux system.
Example output:
UID USER USER_NAME. GROUP_ID HOME_DIRECTORY SHELL SUPPLEMENTARY_GROUPS
1001 user1 User One 1001 /home/user1 /bin/bash group1,group2
1002 user2 User Two 1002 /home/user2 /bin/bash group1
1003 user3 User Three 1003 /home/user3 /bin/bash group2
Conclusion:
The ’lslogins’ command provides a versatile way to view information about users on a Linux system. By using different options, users, system accounts, group membership, and login details can be easily obtained. It is a valuable tool for system administrators and users alike in managing and monitoring user accounts.