search
HomeSystem TutorialLINUXTo ensure system security, we will teach you various methods to prohibit user login in Linux!

To ensure system security, we will teach you various methods to prohibit user login in Linux!

Feb 14, 2024 pm 05:33 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

Security is an issue that every Linux system administrator must pay attention to and pay attention to, and prohibiting certain users from logging into the system is one of the very important security measures. This article will introduce in detail through examples how to prohibit certain users from logging in in the Linux system to protect system security.

By default, when creating a user account in Linux, the user has shell access. In some cases a user account login shell is not required. This article describes how to disable shell login for existing users and disable shell login when creating users.

Set prohibit shell login when creating a user

By default, when a user is created, the user will be assigned a shell as defined in the /etc/default/useradd file.

Linux comes with a /sbin/nologinshell that displays a message "This account is current not available" when the user tries to connect. This is a way to disable a user from logging into the shell. Here’s how to use it:

useradd -s /sbin/nologin {username}

In the following example, create a user and set the shell to /sbin/nologin:

[root@localhost ~]# useradd user01 -s /sbin/nologin
[root@localhost ~]# tail -1 /etc/passwd
user01:x:1000:1000::/home/user01:/sbin/nologin

View/etc/passwdYou can see that the shell of user01 is /sbin/nologin

To ensure system security, we will teach you various methods to prohibit user login in Linux!

Set a password for user01, and then log in via ssh to test:

[root@localhost ~]# echo '123'|passwd --stdin user01
Changing password for user user01.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# ssh user01@localhost
user01@localhost's password: 
This account is currently not available.
Connection to localhost closed.
To ensure system security, we will teach you various methods to prohibit user login in Linux!

After entering the password, it prompts This account is current not available, and then the connection is closed.

Set prohibit shell login for existing users

To change the shell of an existing user, you can use the usermod and chsh commands to modify:

chshThe command syntax is as follows:

chsh -s /sbin/nologin {username}

Modify the shell of user02 below:

# Centos8默认没有安装chsh,使用下面命令安装:
[root@localhost ~]# yum -y install util-linux-user
[root@localhost ~]# chsh -s /sbin/nologin user02
Changing shell for user02.
chsh: Warning: "/sbin/nologin" is not listed in /etc/shells.
Shell changed.
To ensure system security, we will teach you various methods to prohibit user login in Linux!

usermod The command syntax is as follows:

usermod -s /sbin/nologin {username} 

Modify the shell of user03 below:

[root@localhost ~]# usermod -s /sbin/nologin user03
To ensure system security, we will teach you various methods to prohibit user login in Linux!

You can also manually modify the user shell in the /etc/passwd file.

In Linux systems, prohibiting certain users from logging in is a very important security measure that can effectively protect the system from unauthorized access. Based on actual practice, this article introduces readers to several methods of prohibiting user login in detail, and also mentions precautions to avoid misoperation. I believe that through studying this article, readers will have a deeper understanding of user management and security measures in Linux systems.

The above is the detailed content of To ensure system security, we will teach you various methods to prohibit user login in Linux!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version