- How to add a user without /home?
- 6 Answers 6
- How do I add a user in Linux without a home directory?
- useradd Command
- Without a Home Directory flag
- Create a user using the useradd Command
- Wrap Up
- About the author
- Shehroz Azam
- 15+ adduser command examples in Linux [Cheat Sheet]
- How adduser command works
- Different examples to use adduser command
- 1. adduser command to add a new user
- 2. Add a system user with adduser command
- 3. adduser command to create a new user group
- 4. Add a system group with adduser command
- 5. adduser command to add a new user in different primary group
- 6. adduser command to add a user with different home directory
- 7. adduser command to create a user without a home directory
- 8. Add a user with specific user ID with adduser command
- 9. adduser command to create a user with specific group ID
- 10. adduser command to add a user with a custom login shell
- 11. Add an existing user to an existing group with adduser command
- 12. adduser command to create a user with a disabled login
- 13. Create a user without password using adduser command
- 14. adduser command to add a user with a GECOS or comment
- 15. adduser command to print the debug information
- 16. Hide informational messages with adduser command
- Conclusion
- What’s Next
- Further Reading
How to add a user without /home?
I want to create a new user to run a service on the system but don’t want to have /home and other configuration files for it. Like there is a user for postgres but it doesn’t have any /home directory.
6 Answers 6
By default the command useradd doesn’t create home directories, but for a daemon I recommend you to use the system option and change the shell to a non-existent one so no one can login with said account (in ssh for example):
sudo useradd -r -s /bin/false USERNAME
You can see all the options with man useradd and man groupadd if you want to create a group for the user too.
I believe the best practice is using /sbin/nologin as the login shell, although, using /bin/false wouldn’t make any differences.
Try adduser —system —no-create-home USERNAME or simply have a look at the man adduser which claims to be a «friendlier front end to the low level tools like useradd. «.
I actually didn’t notice that this is adduser vs. useradd , and running the command useradd with these options did not produce any error, but created the account with shell /bin/bash . «Buyer Beware».
in centos7, group is not created by defaulty. ref: [root@srvr0 ava]# adduser —system —no-create-home —group bigdata adduser: group ‘bigdata’ does not exist
I needed something similar — a new user without login privileges and tied to a system service. However, the answer by Clausi creates a user with the primary group as ‘nogroup’, which wasn’t really desirable.
adduser —system —no-create-home —group USERNAME creates a system group with the same name as the user and associates it with the user as the primary group. This can then be verified by using the groups USERNAME or the id USERNAME command.
in centos7, group is not created by defaulty. ref: [root@srvr0 ava]# adduser —system —no-create-home —group bigdata adduser: group ‘bigdata’ does not exist
How do I add a user in Linux without a home directory?
While managing users, Linux administrators need to manage different types of users based on the privileges he/she wants to set. User management includes access control and Group management of the user. It is often seen that when we create users in Linux, there are two types of commands available useradd and adduser. We will add a user in Linux without a home directory using the useradd command in this post.
useradd Command
The useradd is a command used for creating a user in any Linux-based operating system. It is a low-level or less secure command for creating a user because it only creates a user until we specify a flag.
This command does not automatically create a home directory until a -m flag is specified.
Without a Home Directory flag
Moreover, this command also provides a flag or option if you do not want to create a home directory of a user while creating a user.
-M for creating a user without a Home directory
–no-create-home is also used for not creating a home directory of a user
Let’s do some practical, create users using the useradd command and witness it.
Create a user using the useradd Command
To create a user using the useradd command, type the command given below:
In the above command, Ivan is the username, so provide your username at the place of Ivan.
You can witness in the screenshot attached; the user is created without asking for any password.
To create a password for this user, execute the command given below:
Type the new password you want to set for the user:
After successfully creating a user and setting its password, log in to the user profile using the command given below:
Type the recently created password for the newly created user:
You can see we are logged in to Ivan’s shell, and you can also witness that the user does not have the home directory.
Wrap Up
User management is an extremely responsible task for any Linux administrator. Due to the security reasons and privileges of the users, Linux administrators have to create some users without a home directory. This article provides to-the-point detail for creating a user without a home directory.
About the author
Shehroz Azam
A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.
15+ adduser command examples in Linux [Cheat Sheet]
adduser is a command-line utility to create a new user in the Linux system. It is a friendlier frontend to the low-level tool ‘useradd’. When adding a new user, it also creates the user directory under the /home directory.
How adduser command works
The default configuration file for adduser is /etc/adduser.conf. adduser command will work according to the default value specified in adduser.conf file.
adduser command adds the details of the new user to the following files.
- /etc/passwd — It stores user account information.
- /etc/shadow — It contains the password information of users. The passwords are stored in an encrypted format.
- /etc/group — It stores group information.
Different examples to use adduser command
You can use adduser command to add a normal user, system user, or user group in the Linux system. Only users with root privileges can add a user to the system. So, you will need to log in as a root user or use sudo to execute adduser command.
The general syntax of adduser command is:
Now, let’s see some practical examples of adduser command to add a user or group in the Linux system.
1. adduser command to add a new user
With this command, you can add a new user to the system. When adding a new user, you will be asked to enter some information.
Sample Output:
/etc/passwd contains the user account information in the following format.
username:password:userID:groupID:comment:user_directory:shell
- Username: It is the login name for the user. The username should be between 1-32 characters.
- Password: It contains the user password in an encrypted format (x).
- User ID: Every user has a unique User Identification Number (UID). 1001 is the user ID of deepak.
- Group ID: It shows the primary group ID of a user. 1001 is the primary group ID of deepak.
- User detail: In between two colons, a GECOS or comment of a user will be displayed if added.
- Home Directory: The user’s home directory. The default location is the /home directory.
- Login shell: The login shell for the user. The default login shell is /bin/bash.
2. Add a system user with adduser command
You can add a new system user using —system option with adduser command.
Sample Output:
3. adduser command to create a new user group
—group option allows you to create a new user group in the system. The group is created without any users.
Sample Output:
ubuntu@golinux:~$ sudo adduser --group linux Adding group `linux' (GID 1002) . Done.
4. Add a system group with adduser command
We can use —system and —group option together to create a system group in the system. A system user with the same group name is also created.
$ sudo adduser --system --group group
Sample Output:
5. adduser command to add a new user in different primary group
adduser command adds a new group for a user with the same username. To add a user in a different group, we can use —ingroup option. The group should be already present in the system.
$ sudo adduser --ingroup GROUP user
Sample Output:
6. adduser command to add a user with different home directory
By default, adduser creates the user’s home directory with username under the /home directory. We can create a user’s home directory in a different location using —home option. It also allows us to specify a different name for the user’s home directory.
It is recommended to make sure the new home path doesn’t exist as it will be automatically created by adduser command with proper permission.
$ sudo adduser --home /directory/home_dir_name user
Sample Output:
Here, we are adding a user ‘sam‘ with its directory ‘record‘ under the /snap directory.
7. adduser command to create a user without a home directory
Normally, adduser command automatically creates the user’s home directory when adding a user. We can use —no-create-home option to add a user without its home directory.
$ sudo adduser --no-create-home user
Sample Output:
As we can see, the default home directory is /home/elliot but it is not created.
8. Add a user with specific user ID with adduser command
Every user has a unique user ID in the Linux system. By default, adduser command assigns the next available user ID to a new user. We can use -u or —uid option to add a new user with a custom user ID.
$ sudo adduser -u userID user
$ sudo adduser --uid userID user
Sample Output:
The user chris is assigned with a custom user ID 4567.
9. adduser command to create a user with specific group ID
We have already seen that the adduser command creates a new group with a user name and adds a user to that group. To add a user to a different group, we can use —gid option. The group should be already present in the system.
$ sudo adduser --gid groupID user
Sample Output:
10. adduser command to add a user with a custom login shell
adduser command selects the default login shell specified by DSHELL variable in /etc/adduser.conf. The default login shell is /bin/bash. To use a different login shell, we can use —shell option as shown below.
$ sudo adduser --shell SHELL user
Sample Output:
11. Add an existing user to an existing group with adduser command
Instead of adding a new user, you can also add an existing user to an existing group using adduser command.
Sample Output:
12. adduser command to create a user with a disabled login
—disabled-login option does not ask to set a password for a new user. The user won’t be able to use the account until the password is set.
$ sudo adduser --disabled-login user
Sample Output:
We can set or change the user password in the Linux system by using the following passwd command.
ubuntu@golinux:~$ sudo passwd maxim New password: Retype new password: passwd: password updated successfully
13. Create a user without password using adduser command
—disabled-password also does not ask to set a password when adding a user. It is similar to the above command —disabled-login .
$ sudo adduser --disabled-password user
Sample Output:
14. adduser command to add a user with a GECOS or comment
A GECOS or comment field is generally used for keeping the information related to the user. We can use —gecos option to add a GECOS or comment when creating a new user.
$ sudo adduser --gecos GECOS user
Sample Output:
15. adduser command to print the debug information
We can use —debug option to print the information in a verbose mode. It is useful when debugging the adduser.
Sample Output:
16. Hide informational messages with adduser command
—quiet option hides the informational messages in the output. It does not hide warnings and errors.
Sample Output:
Conclusion
adduser is a useful command to add a user or group in the Linux system. We hope you have learned to use adduser command from this article. If you still have any confusion, please feel free to ask us in the comment section.
What’s Next
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!