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
- 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>,
# passwd myuser Changing password for user myuser. New password: Retype new password: passwd: all authentication tokens updated successfully.
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,
Example: Login using sudo command
root@linux # su mysuer myuser@linux root]$ cd ~ myuser@linux root]$ cd pwd /home/myuser
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 ~]$