How can a user be added and deleted from a Unix system?

Answered by Antonio Sutton

Adding a User to a Unix System:

To add a user to a Unix system, you can use the useradd command. This command is typically executed with root privileges. The useradd command requires certain parameters and options to create a new user.

1. Open a terminal or log in to the Unix system as the root user.
2. Execute the following command to add a user:
“`
Useradd [options] username
“`
Here, “username” is the desired username for the new user.

Some commonly used options for the useradd command are:
– -c: This option is used to provide a comment or description for the user.
– -d: This option specifies the home directory for the user. By default, the home directory is created in the /home directory with the same name as the username.
– -m: This option creates the user’s home directory if it does not already exist.
– -s: This option sets the user’s default shell.
– -u: This option specifies the user ID (UID) for the new user.

For example, to add a user named “john” with a comment “John Doe” and a home directory of /home/john, the command would be:
“`
Useradd -c “John Doe” -d /home/john -m john
“`

3. Set a password for the new user using the passwd command:
“`
Passwd username
“`
Replace “username” with the actual username you created. Follow the prompts to set the password.

4. The new user is now added to the Unix system and can log in using the provided username and password.

Deleting a User from a Unix System:

To delete a user from a Unix system, you can use the userdel command. This command should be used with caution as it permanently removes the user and associated files.

1. Open a terminal or log in to the Unix system as the root user.
2. Execute the following command to delete a user:
“`
Userdel [options] username
“`
Here, “username” is the username of the user you want to delete.

The userdel command has some commonly used options:
– -r: This option removes the user’s home directory and mail spool.
– -f: This option forces the deletion of the user, even if the user is currently logged in.

For example, to delete the user “john” and remove their home directory and mail spool, the command would be:
“`
Userdel -r john
“`

3. Confirm the deletion by checking if the user’s account and associated files have been removed.

It is important to exercise caution when using the userdel command, as it permanently deletes user data. Make sure to double-check the username and consider taking a backup of important files before deleting a user.