Linux: Create a New User and Password and Login Example


In order to create a user in Linux, the first thing required is that you have access to root user, or else you will get a "Permission denied" error.

When you create or edit a user, the user details will get updated in the /etc/passwd file.

How to create new User?

  • Make sure you are logged in as a root user (can use sudo if you prefer),
  • Now to create a user myuser, type command useradd muser,
    root@linux # useradd myuser
    Add new user using Linux Command
  • You can check the user details in the /etc/passwd file,
    # cat /etc/passwd | grep myuser
    myuser:x:1002:1002::/home/myuser:/bin/bash
    1) myuser -> our newly created username
    2) x -> password is saved in the /etc/shadow file.
    3) 1002 -> is the user id of user myuser
    4) 1002 -> is the myusers primary group id
    5) empty -> users description.
    6) /home/myuser -> users home directory
    7) /bin/bash -> users default login shell

How to Change Password for a new User?

You must have noticed that we never set any password for our new user, to set the password make use of the passwd command followed by the username.

  • On the Terminal type passwd <username>, Example:
    # passwd myuser
    Changing password for user myuser.
    New password: 
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    Change Password for new User Linux
  • Note: if the password is based on a dictionary word you will get a "BAD PASSWORD" warning.


How to login with a new user?

Now that we have the new user setup with username and password, let's try to log in using it.

As we were logged in as the root user, we can make use of su or sudo command followed by username or simply exit and login using the new username while doing ssh,

Example: Login using su command,
root@linux # su mysuer
myuser@linux root]$ cd ~
myuser@linux root]$ cd pwd
/home/myuser
Example: Login using sudo command
root@linux # sudo -u myuser bash
myuser@linux root]$ cd ~
myuser@linux root]$ cd pwd
/home/myuser

Login with the new user using SSH

To login to the server/system from a remote location, you can ssh as follows,

# ssh myuser@ip-of-the-server
password:
myuser@linux ~]$

Topics Coverage:


🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram