Posted: . At: 11:22 AM. This was 2 years ago. Post ID: 12751
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to create a new user on Linux with the command line.


This simple command-line will create a new user on Linux named test5.

┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ sudo useradd test5 -m -K PASS_MAX_DAYS=5 -K PASS_WARN_AGE=3 -K LOGIN_RETRIES=1

Then set a password for the new user.

┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ sudo passwd test5
New password: 
Retype new password: 
passwd: password updated successfully

This adds a new user to your system named test5, this user can only try and login again once if they get their password wrong. The -m parameter to useradd ensures that a home directory will be created when the user is created. use the -M parameter to avoid this if required. The /etc/skel directory contains all of the files that will be placed in a user`s home directory upon creation. This can be used to add directories as well, such as Desktop, Pictures and Documents. This means that a user can be already set up and ready after creation with all needed SSH keys and other important files. That saves a lot of time when creating a new set of users on a Linux server, and you have limited time.

Use the finger(1) command to get information about a Linux user. This is a very versatile command.

┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ finger test5
Login: test5          			Name: 
Directory: /home/test5              	Shell: /bin/sh
Never logged in.
No mail.
No Plan.

If I use the command on my user, it will show the last time I logged in.

┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ finger jason
Login: jason          			Name: jason
Directory: /home/jason              	Shell: /bin/bash
Last login Thu Dec  2 14:56 (AEDT) on tty3
No mail.
No Plan.
┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ lslogins | awk '$1 >= 1000 && $1 <= 2000'
 1001 jason                 91                        07:21 jason,,,
 1002 test5                  0

The command above lists all users with a UID > than 1000 and < than 2000.

awk '$1 >= 1000 && $1 <= 2000'

The above awk statement does this for you. This is very easy. Instead of using complicated if statements, this is easier.

The number in the second column is the number of running processes. The third column contains other information such as the login time and the GECOS information which may not all be filled out by a user when creating an account.

This is how to dress up this output with nice column headers.

┌──[jason@11000000.10101000.00000001.00000011][~]
└──╼  ╼ $ lslogins | awk '$1 >= 1000 && $1 <= 2000' | awk -F, 'NR==1 {print "UID","Username","Procs","Login","GECOS"} {gsub(/"/,""); print $1,$2,$3}' | column -t
UID   Username  Procs  Login  GECOS
1001  jason     94     07:21  jason
1002  test5     0

Very nice looking output.

A very organised server rack.
A very organised server rack.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.