Linux: Familiarize yourself with useradd

Tram Ho

If a lot of people are using your Linux machine at home or you are managing a server that provides access to multiple users, the useradd command is essential to creating the user.

In addition, many services you use as a developer may require their user accounts to function. So even when used on a personal computer, you may find yourself approaching these commands when you install MySQL or something similar.

You can get a complete overview of the various options available to you by typing the command: man useradd

But if that is overwhelming, here is a breakdown of some common options you can use when creating a user.

Create a user

The simple format for this command is useradd [options] USERNAME.

For example: useradd test (as root user – prefix with sudo if you are not logged in as root).

This will create a user called test, but it is a limited activity and will not create other useful things like home directory or password.

Add a password

Then, you add the password to the user test using the passwd : passwd test command. This will prompt you to enter the password for this user test.

There is an option to add encrypted passwords via the -p option on useradd , but this is not recommended for security purposes.

Note that the -p option doesn’t allow you to enter the original text password, it will ask you to encrypt it first. This is difficult, because you shouldn’t do it! Just use the passwd command.

Other common options

Home directories

To create a user with the default home directory, use the following option:

useradd -m test

This user currently has a /home/test .

To change the home directory, you can pass an additional option to modify this, for example:

useradd -m -d /alternate test

Shell

By default, your created user will be able to log in by default to bin/bash or bin/sh , which will be specified in /etc/default/useradd .

You can override this default with the -s option:

useradd -s usr/bin/zsh test

Putting it all together

To build the entire command, you set the options in turn – it doesn’t matter – and end with the username you want to create.

So creating a user with a home directory and a customized shell will look like this:

useradd -m -s /usr/bin/zsh user

And then you will add a password for the user: passwd user

Read the Fine Manual

Now you have seen the basics of what this tool can do.

man useradd will show you how to add things such as account expiration date, assign groups, etc.

Refer

https://www.freecodecamp.org/news/linux-how-to-add-users-and-create-users-with-useradd/

Share the news now

Source : Viblo