Adding a new user on Linux can be easily done on command line using useradd. These are some scenarios and handy commands to add a user on Linux. We’ll use Ubuntu for the purpose of this tutorial.
Using useradd with no arguments
This may not create home directory.
$ sudo useradd user2
useradd – specific login shell
$ sudo useradd -s /bin/bash user2
useradd – specific login shell and create home dir
This may be a typical case.
$ sudo useradd -m -s /bin/bash user2
useradd – specific group
By default useradd create group with same name as user. In case we want to use a specific group:
$ sudo useradd -g GROUPID_OR_NAME user2
useradd – with specific password
By default useradd creates user without password.
$ sudo useradd -p ENCRYPTED_PASSWORD_AS_RETURNED_BY_CRYPT user2
To generate password to be used with -p option, you can use the following command:
$ openssl passwd -1 somepassword $1$q5tPBYXv$824B0Ve7yrRW4frZ0.In1/
Some notes on passwords:
- In password “$1$q5tPBYXv$824B0Ve7yrRW4frZ0.In1/” string q5tPBYXv is salt used while generating the password. It could also have been specified as an option.
- To be able to login using password, password based login has to be enabled on ssh server.