How add and remove user in Unix?

Answered by Ricardo McCardle

Adding a User in Unix:

1. Using the “adduser” command:
– If you have root access, you can add a new user by using the “adduser” command followed by the desired username. For example, to add a user named “new_user_name”, you would run the command:
“`
Adduser new_user_name
“`
– This command will prompt you to enter various details for the new user, such as a password, full name, and contact information. You can provide this information or leave it blank to use the default values.

2. Using the “sudo adduser” command:
– If you do not have root access, you can use the “sudo” command to run the “adduser” command with root privileges. For example, to add a user named “new_user_name”, you would run the command:
“`
Sudo adduser new_user_name
“`
– You will be prompted to enter your own password before the “adduser” command is executed.

3. Verifying the user’s groups:
– To check which groups the new user belongs to, you can use the “groups” command followed by the username. For example, to check the groups of the user “new_user”, you would run the command:
“`
Groups new_user
“`
– This will display a list of groups that the user is a member of.

4. Listing all groups:
– If you want to see a list of all the groups on your system, you can use the “getent group” command. This command retrieves group information from the system’s databases. To make the output more readable, you can use the “cut” command to extract only the group names. For example, you can run the command:
“`
Getent group | cut -d: -f1
“`
– This will display a list of group names, one per line.

5. Adding a user to a group:
– To add an existing user to a specific group, you can use the “usermod” command with the “-aG” option, followed by the group name and the username. For example, to add the user “user_name” to the group “group_name”, you would run the command:
“`
Usermod -aG group_name user_name
“`
– This command adds the user to the specified group without removing them from their existing groups.

Removing a User in Unix:

1. Removing a user:
– To remove a user from the system, you can use the “deluser” command followed by the username. For example, to remove a user named “newuser”, you would run the command:
“`
Sudo deluser newuser
“`
– This command will remove the user from the system, but it will not delete their home directory or any files associated with the user.

2. Removing a user and their home directory:
– If you want to completely remove a user, including their home directory and any files associated with them, you can use the “–remove-home” option with the “deluser” command. For example, to remove the user “newuser” and their home directory, you would run the command:
“`
Sudo deluser –remove-home newuser
“`
– This command will delete the user’s home directory and any files within it, in addition to removing the user from the system.

Adding and removing users in Unix can be done using the “adduser” and “deluser” commands. The “usermod” command can be used to add a user to a specific group. It is important to have the necessary privileges (root access or using “sudo”) to perform these actions.