Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to prohibit users from logging in in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to prohibit users from logging in Linux". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to prohibit users from logging in in Linux"!

Setting to disable shell login when creating a user

By default, when you create a user, the shell is assigned to the user 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 one way to prevent users from logging in to shell. Here is how to use it:

Useradd-s / sbin/nologin {username}

For the following example, create a user with shell set to / sbin/nologin:

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

Looking at / etc/passwd, you can see that the shell of user01 is / sbin/nologin

Set the password for the user01 user, and then test the ssh login:

[root@localhost ~] # echo '123' | passwd-- stdin user01Changing password for user user01.passwd: all authentication tokens updated successfully. [root@localhost ~] # ssh user01@localhostuser01@localhost's password:This account is currently not available.Connection to localhost closed.

After entering the password, prompt This account is current not available, and the connection is closed.

Set to disable shell login for existing users

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

The syntax for the chsh command is as follows:

Chsh-s / sbin/nologin {username}

Modify the shell of the user02 user as follows:

# Centos8 does not install chsh by default. Use the following command to install: [root@localhost ~] # yum-y install util-linux-user [root@localhost ~] # chsh-s / sbin/nologin user02Changing shell for user02.chsh: Warning: "/ sbin/nologin" is not listed in/ etc/shells.Shell changed.

The syntax for the usermod command is as follows:

Usermod-s / sbin/nologin {username}

Modify the shell of the user03 user as follows:

[root@localhost] # usermod-s / sbin/nologin user03

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

At this point, I believe you have a deeper understanding of "how to prohibit users from logging in in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report