Creating a user with Ubuntu terminal

Like most of the blogs I write I am writing with myself in mind a bit but if it provides help to anyone, that’s great.  One of the many reasons for writing this post is that next time I go to create a new user on any Linux server I don’t need to go to several blogs trying to remember parts of the command or why if I missed –a that it matters.

If you use the man command in the terminal e.g. man usermod that will provide an explanation of the commands and switches for that command.

Create a user via the terminal

Sign in with an existing user (via ssh or on the box) that has sudo access.

sudo adduser testcreatinguser

This command will create the user testcreatinguser and ask the person creating the user to create a new password.  I recommend that you select a strong password when creating a new password.

It will also ask for the user’s full name, work phone etc.  The only one that I didn’t leave to the default was full name, which I set to “Test User”.  If you need to add them then you can type different information.

After creating the user I wanted to add the new user account to the sudo group.

sudo usermod –aG sudo testcreatinguser

The “-a” is important.  The new user will be removed from any other group they belong to if this is missing. “G” is for groups in this case “sudo”.

Expire user account

If the new user account access needs revoked for any reason.  The command is as follows:

sudo usermod –expiredate 1 testcreatinguser

The message received is: Your account has expired; please contact your system administrator.

To allow access again:

sudo usermod –expiredate “” testcreatinguser

Lock password for user account

Another way to stop the new user gaining access is to:

sudo passwd –l testcreatinguser

After successfully entering the command into the terminal, the user will receive “passwd: password expiry information changed.”

This will refuse the user’s password when they try to log in.

To reinstate the user account’s password:

sudo passwd –u testcreatinguser

Again, the user will receive “passwd: password expiry information changed.”

Most of this information can be found elsewhere on the internet but it might prove useful to have it in one post.  Until next time.

Leave a comment