Open In App

How to list all users in linux who have never logged in ?

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Linux we have a term called user management, which includes all the tasks related to handling user who are logged in on system or may have been created for different purposes but never logged in. when it comes to managing accounts Linux is pretty handy it comes with lot’s of options for administration tasks. you can create users, add them to groups, manage their permissions throughout the system and files, and also lock or unlock the users. In this article we will see how you can list down the users who never logged in the system but are present or created. The process is pretty simple it’s just one command you need to type on your terminal and you will have the results.

Methods To list User’s In Linux:

Method 1: Using lastlog command

Step 1: Open your terminal in linux, to open it you can either go and search in applications or you can also use shortcut key that is CTRL + ALT + T this will open your terminal.

CTRL + ALT + T

Screenshot-2023-10-08-201050

Step 2: Final step, as we discussed above it’s only one command you need to the command is : lastlog

$ lastlog

imag

you can see in the below screenshot we have a list of all the users that have never logged in on the system.

Method 2: Checking the /etc/passwd File

We will use a bash-script to find all the user’s who have never logged into system. below are the steps to follow:

Step 1: Open your terminal in linux, to open it you can either go and search in applications or you can also use shortcut key that is CTRL + ALT + T this will open your terminal.

CTRL + ALT + T

Screenshot-2023-10-08-201050

Step 2: Open nano editor by typing nano in terminal and then type the following bash-script. and save the file with .sh extension, after that we need to make the script runnable by using chmod command. follow the commands mentioned below:

nano filename.sh

img1

!/bin/bash
# This is a script to check users who have never logged in.

# Loop through each username from the /etc/passwd file
for user in $(cut -d: -f1 /etc/passwd); do
  # Check last login information for the current user using lastlog
  if ! lastlog -u $user | grep -q "Never logged in"; then
    # If "Never logged in" is not found, the user has logged in before
    # Move on to the next user.
    continue
  fi

  # If "Never logged in" is found, the user has never logged in.
  # Print a message indicating that.
  echo "$user has never logged in"
done

img2

after you save the file by pressing CTRL + X and then enter on yes option. now we need to make the script runnable with command chmod.

chmod +x file_name.sh

img3

Now, we can run the file just by the command:

./file_name.sh

img4

Method 3: Using the “last” Command

You can use Last command to find out the user’s who have logged in the system. below is the command you can use with bunch of arguments to refine our results.

last | awk '!/logged in/'

img5

The above command will list all unique usernames from the last command’s output, showing users who have logged in.

To list User’s In Ubuntu:

In Ubuntu the process is exactly same as in the kali linux, that we have seen in the above instructions. below are the screenshots of the implementation of the commands.

1. Using Lastlog:

lastlog

u1

2. Checking /etc/passwd file:

Follow the same steps mentioned above, see the series of commands mentioned below and screenshots.

nano filename.sh

u2


# Loop through each username from the /etc/passwd file
for user in $(cut -d: -f1 /etc/passwd); do

  if ! lastlog -u $user | grep -q "Never logged in"; then
    # Move on to the next user.
    continue
  fi

  # If "Never logged in" is found, the user has never logged in.
  # Print a message indicating that.
  echo "$user has never logged in"
done

u3

chmod +x demo.sh
./demo.sh

u4

As you can see that the steps are exactly the same for ubuntu also, because they both are linux based distributions and also uses same package manager and bash. This is how you can list users who have never logged in into the system in ubuntu.

FAQs:

1. How to check user login history in Linux?

Ans. You can check user login history in Linux using commands like last or ac. These commands provide information about when and how long users have been logged in.

2. How to find users who have never logged in to the system?

Ans. You can find users who have never logged in by checking the “Never logged in” status in the lastlog command, parsing the /etc/passwd file, or using the “last” command to analyze login history.

3. How to list users in Ubuntu who have never logged in?

Ans. To list users in Ubuntu who have never logged in, you can use the lastlog command or parse the /etc/passwd file, as shown in the methods above.

4. Can I delete users who have never logged in?

Ans. Yes, you can delete users who have never logged in, but it’s important to be cautious. Before deleting a user, make sure they are no longer needed, and back up any user-specific data associated with their account.

5. Is it common to have users who have never logged in on a Linux system?

Ans. Yes, it is common to have users who have never logged in, especially on servers or systems where accounts are created but not actively used, or for system accounts that perform background tasks.

Conclusion

To conclude the article, there are hundreds of commands you can tryout and see for yourself what happens in linux. But do not try to change/delete the system file it can crash you system eventually. Linux is great when it comes to managing things, it can be user, files, accounts, server’s and many more. the above article cover’s the process or you can say the command which can help you to find all the user’s that have never logged-in on system.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads