Create a Linux User with non-interactive shell
Question : Create a Linux User with a non-interactive shell The System Admin Team of XfusionCorp Industries has installed a backup agent tool on all app servers. As per the tool’s requirements, they need to create a user with a non-interactive shell.
In this article, we’ll discuss a way to create new user with non-interactive shell using the useradd command.
useradd may be a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead.
- /sbin/nologin is specifically designed to switch a shell and produces output complaining you cannot log-in.
The name of a replacement user’s login shell. this feature sets the SHELL variable in /etc/default/useradd.
1. At first login to the respective mentioned server in the task. Mine is stapp02
thor@jump_host /$ ssh steve@stapp02
The authenticity of host ‘stapp02 (172.16.238.11)’ can’t be established.
ECDSA key fingerprint is SHA256:NgyY+fBF5b56d9UEyUmbNHqK7eZTbk5n5/Ypr19rQkk.
ECDSA key fingerprint is MD5:ef:d2:32:cb:57:6f:44:cb:0c:0b:4f:82:f0:dc:92:70.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘stapp02,172.16.238.11’ (ECDSA) to the list of known hosts.
[steve@stapp02 ~]$ sudo su —
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#3) With great power comes great responsibility.
2. 1st check user is existed on the server by below command
[root@stapp02 ~]# id ravi
3. If the user is not found the then you create a user with a non-interactive shell
[root@stapp02 ~]# adduser ravi -s /sbin/nologin
4. Validate user is created successfully as per the task request.
[root@stapp02 ~]# id ravi
uid=1002(ravi) gid=1002(ravi) groups=1002(ravi)
[root@stapp02 ~]# cat /etc/passwd |grep ravi
Apart from this if you need more clarity, I have made a tutorial video on this , please go through and share your comments. Like and share the knowledge
Creating a user who cannot get an interactive shell
What are interactive and non-interactive shell? Questions: Create a user john who should not get an interactive shell. How can we do this?
5 Answers 5
The /etc/passwd file has as the last item on a user’s line the program to be run upon login. For normal users this is typically set to /bin/sh or other shell (e.g. bash, zsh).
Traditionally, identities that are used to own processes or files or other resources have their «shell» set to /bin/false as in
syslog:x:101:102::/home/syslog:/bin/false
The pseudo-user syslog owns /var/log/syslog and is the only UID which has write permission for that file, but one cannot log in as user syslog as there is no command interpreter to be run.
Regarding the question you are trying to answer:
Create a user john who should not get an interactive shell.
The question means «Create a user named john who won’t be able to log in and run commands from a shell». Interactive describe the way the shell works: the user types something, and the shell does something accordingly (there is a kind of communication between the user and the shell). The word «interactive» doesn’t really add any information to the question, because as long as there is a user typing commands, the shell session is interactive.
There is also the non-interactive mode, where the user saves a series of commands they want to run in a file (called a shell script), and executes the file afterwards. Some commands behave differently depending on whether the shell is run interactively or non-interactively. You can read more here (this document is for bash , but the same concept applies for other shells).
On how to create such a user, if you skim through man useradd there is an option to set the login shell, -s or —shell . You can also create a user normally, in anyway you want (which I assume you know already?), then edit /etc/passwd like in msw’s answer.
How can I create a non-login user?
I’d like to create a user and a group both called subversion on a RHEL 5 system. I looked at the man page for useradd and I guess the command would be just be.
However, not sure how to avoid creating a home dir. Also, I don’t want it to be a user that can log in to the system. The main purpose is just to provide an owner for a SVN repository.
8 Answers 8
You can use the -M switch (make sure it’s a capital) to ensure no home directory will be created:
then lock the account to prevent logging in:
This isn’t a particularly strong answer, the user created by this means still has a shell. And you did not even warn the OP that this was the case. Retrospectively that would be usermod -s /bin/false subversion , or with —shell /bin/false to useradd
@beak actually only the root user would be able to su to the locked account, but why bother if the person has gained root access already? And setting the shell doesn’t do much when a user can run su -s /bin/bash username and bypass that.
thanks for taking the time to have the discussion, you are of course correct; but it pains me to see non-login users with shells defined, it strikes me as lazy, and incase someone is unfamiliar with the system, it’s nice that they can’t accidentally do something unintended; hackers are a different breed, if they already got a shell on the machine, I think it’s basically game over
You can use the following command:
For more info, check manual pages with this command:
You will find in this documentation the following flag that can be used for your purpose.
-r, --system create a system account
The -r flag will create a system user — one which does not have a password, a home dir and is unable to login.
this command will even create a group for the user called the same. So the «subversion» user will be in the «subversion» group. Great for when you later want to do «sudo chown -R subversion:subversion /path/to/folder»
@c4il But the only one that can login into them is root, right? I mean, they don’t have a password, so I would expect only root to be able to log into them.
Another solution to create a system user, using adduser :
adduser --system --no-create-home --group yourusername
You can remove —group if you don’t need group yourusername, and —no-create-home if you do need a home for this user.
As mentionned by py4on in comments, on some systems one may need to use the —disabled-login option in order to, well, disable login for this user. It seems to be the default behaviour under Debian, though.
Beware that the numeric ID of the user will be of a system account. You can fix the uid using the —uid option, though.
Finally, note that on some systems (e.g. Fedora) adduser is a symlink to useradd , in which case this answer is not valid.