Adding a User in Linux
1. Log in as root
2. Use the command useradd "name of the user"
useradd -m <username>
Example : useradd -m gauravkumar
Note: -m to create user home folder in /home directory.
Note: useradd command adds an entry to the /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files.
To be able to log in as the newly created user, we need to set the user password. To do that run the passwd command followed by the username:
sudo passwd username
We will be prompted to enter and confirm the password. Make sure we use a strong password.
3. Use su(switching users) suffixed with the name of the user we just added above
su - gauravkumar
Note: su “-“ will execute the login profile. All the env variables and alias for the user will be available.
4. Execute below command and it will open VI editor, insert new user public keys in the vi editor. Save and Exit.
All command need to execute in /home/<new user folder>
mkdir .ssh;
chmod 700 .ssh;
cd .ssh;
touch authorized_keys;
chmod 600 authorized_keys;
vi authorized_keys
5.At home dir
Use vi /etc/group
Check the log here: tail -f /var/log/secure
When the added user will log using the ssh keys in their env then it will show the related log here.
Deleting a User From Linux
We can use userdel command and “name of the user”.
Example : userdel gauravkumar
Note: -r option with userdel : help to delete the user as well as its home directory.
Example: userdel -r gauravkumar
Create a User with Specific Home Directory
useradd command with -m option creates the user’s home directory in /home by default.
If we would like to create the user’s home directory in any other location, then we could use the d (--home) option.
Example which shows how to create a new user named <username> with a home directory of /opt/username:
sudo useradd -m -d /opt/gauravkumar gauravkumar
Create a User with Specific User ID
As we know that users are identified by unique UID and username, In Linux, User identifier (UID) is a unique positive integer assigned to each user.
when a new user is created, the system assigns the next available UID from the range of user IDs specified in the login.defs file.
useradd with the -u (--uid) option to create a user with a specific UID.
For example: to create a new user named gauravkumar with UID of 2022 we need to execute below command:
sudo useradd -u 2022 gauravkumar
We can check the user’s UID, using the id command like below:
id -u username
Result: 2022
No comments:
Post a Comment