Software & AppsOperating SystemLinux

How To Add a User Non-Interactively with adduser Command in Ubuntu

Ubuntu 21

In this article, we will delve into how to add a user non-interactively using the adduser command in Ubuntu. This method is particularly useful when you need to create multiple users or automate the user creation process.

Quick Answer

To add a user non-interactively with the adduser command in Ubuntu, you can use the --disabled-password and --gecos options. This allows you to create a user account without a password and skip the interactive prompts for user information. Alternatively, you can use the --comment option in newer versions of adduser or the useradd command for non-interactive user creation.

Understanding the adduser Command

The adduser command is a friendly interactive frontend to the low-level utility useradd. It is more user-friendly and provides a more straightforward interface for user management. The adduser command will take care of all the necessary files, such as creating a home directory, copying skeleton files, and setting password and group ownerships.

Adding a User Non-Interactively

To add a user non-interactively, we will use the --disabled-password and --gecos options with the adduser command. Here is the basic structure of the command:

adduser --disabled-password --gecos "" username

Replace username with the desired username for the new user.

Understanding the Parameters

  • --disabled-password: This option creates a user account without a password. It’s a secure option when creating a user non-interactively, as it prevents setting a password in plain text.
  • --gecos: The GECOS field is a set of comma-separated values that traditionally contains user information like full name, phone numbers, etc. By setting it to an empty string (""), we skip the interactive prompts for this information.

An Alternative Method: Using the –comment Option

In more recent versions of adduser, you can use the --comment option instead of --gecos. Here’s an example:

adduser --disabled-password --comment "" username

The --comment option serves the same purpose as --gecos, providing additional user information in a comma-separated format.

Using the useradd Command

If you prefer, you can use the useradd command, which does not have any built-in prompting and can be used to add users non-interactively. Here’s an example:

useradd -m -p <encryptedPassword> -s /bin/bash <username>
  • -m: This option creates a home directory for the user.
  • -p: This option sets the user’s password. Replace <encryptedPassword> with the encrypted password for the user. If you skip it, the password will be disabled.
  • -s: This option sets the user’s login shell. In this case, we’re using /bin/bash.

Conclusion

In this article, we’ve explored how to add a user non-interactively using the adduser command in Ubuntu. We’ve also discussed the parameters used in the command and their functions. This knowledge will be particularly useful when you need to create multiple users or automate the user creation process. Remember to always ensure the security of your system when creating users, particularly when setting passwords.

What is the difference between `adduser` and `useradd` commands?

The adduser command is a friendlier and more interactive frontend to the useradd command. It takes care of additional tasks such as creating a home directory, copying skeleton files, and setting password and group ownerships. On the other hand, useradd is a low-level utility that does not have any built-in prompting and is more suitable for non-interactive user creation.

Can I add a user with a password non-interactively using the `adduser` command?

Yes, you can add a user with a password non-interactively. However, it is not recommended for security reasons. By default, the adduser command does not provide an option to set a password non-interactively. It is better to use the --disabled-password option to create a user without a password and then set the password manually using the passwd command.

How can I specify additional user information like full name or phone numbers when adding a user non-interactively?

You can specify additional user information like full name or phone numbers using the --gecos option with the adduser command. By setting it to a comma-separated string, you can provide the desired information. Alternatively, in newer versions of adduser, you can use the --comment option for the same purpose.

Can I create a home directory for the user when adding them non-interactively?

Yes, you can create a home directory for the user when adding them non-interactively. In the adduser command, the -m option is used to create a home directory for the user. If you are using the useradd command, you can achieve the same result by including the -m option in the command.

How can I set a login shell for the user when adding them non-interactively?

To set a login shell for the user when adding them non-interactively, you can use the -s option with the useradd command. Specify the desired login shell path after the -s option. For example, -s /bin/bash sets the login shell to Bash.

Leave a Comment

Your email address will not be published. Required fields are marked *