[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 20, 2023

How To Disable User Logins On Linux

How To Disable User Logins On Linux

1. Overview

In this tutorial, we will cover the various methods used to block user logins on Linux and their differences.

 

2. The nologin Command

We can use the nologin command to prevent a user from logging in. It prints a message and exits with a non-zero status code to indicate failure. We can change a user’s login shell with the usermod command’s -s flag.

As an example, let’s use it to prevent a user called baeldung from logging in:

$ sudo usermod baeldung -s /sbin/nologin
$ sudo su - baeldung
This account is currently not available.

Now, if we try to use su to log into the account, we see an error indicating that logins have been disabled.

We can also modify the error message by modifying the /etc/nologin.txt file:

 
$ echo "Hi, logins have been disabled for your account. Please contact your system administrator for more information" | sudo tee /etc/nologin.txt
$ su - baeldung
Hi, logins have been disabled for your account. Please contact your system administrator for more information

 

3. The false Command

The false command is a simple command we use to return a non-zero status code indicating failure. Let’s run it and check its status code:

$ false
$ echo $?
1

false is the opposite of the true command, which always returns a zero status code, indicating success:

$ true
$ echo $?
0

We can use them in Bash while statement to repeatedly execute code:

while false; do
    echo "This code will never run!"
done

while true; do
    echo "This code will run forever!"
done

The first code block is never executed since false always indicates failure, while true always indicates success:

$ ./script
This code will run forever!
This code will run forever!
This code will run forever!
...

While it is not the false command’s primary purpose, we can still use it for preventing user logins, just like we did with the nologin command. However, false does not print an error message and immediately exits the shell, which can cause confusion:

 
$ sudo usermod baeldung -s /bin/false  
$ sudo su - baeldung
$ echo $?
1

This means that we cannot customize error messages as we did with nologin in the previous section.

 

4. The passwd Command

We can use the passwd command’s -l flag to lock a user account, preventing logins:

$ sudo passwd -l baeldung
passwd: password expiry information changed.
$ su - baeldung
Password:
su: Authentication failure

Now when we try to login, su will treat all passwords as invalid. We can unlock the account with sudo passwd -u baeldung. This method is similar to the false command since it doesn’t allow us to display a descriptive message.

 

5. The usermod Command

Similar to the passwd command, we can use the usermod command with the -L or -U flags to lock/unlock a user account:

$ sudo usermod -L baeldung
$ su - baeldung
Password: 
su: Authentication failure
$ sudo usermod -U baeldung
$ su - baeldung
Password: 
$ echo $? # Success
0

 

6. Conclusion

In this article, we learned about various commands used to block user logins on Linux and their differences. Usually, the nologin command is preferred to other methods like false or passwd since it allows us to set a custom message explaining why the account was locked.

Keep reading
How to Copy, Cut and Paste in Vim / Vi
March 13, 2023

How to Copy, Cut and Paste in Vim / Vi

How To Copy, Cut And Paste In Vim / Vi This article shows how to copy, cut, and paste in Vim / Vi editor. When working with text files, copying, cutting, and pasting text is one of the most commonly performed tasks. Vim or its precursor Vi comes preinstalled on macOS and almost all Linux…

Read article
How to Add User to Sudoers in Debian
March 13, 2023

How to Add User to Sudoers in Debian

How To Add User To Sudoers In Debian This tutorial shows two ways to grant sudo privileges to a user. The first one is to add the user to the sudoers file . This file contains a set of rules that determines which users or groups are granted with sudo privileges, as well as the…

Read article
How to use stat command in linux.
March 13, 2023

How to use stat command in linux.

How To Use Stat Command In Linux. This article explains how to use stat command. stat is a command-line utility that displays detailed information about given files or file systems. Using the stat Command The syntax for the stat command is as follows: stat [OPTION]… FILE… stat accepts one or more…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00