Modifying and Deleting User Accounts

If you know how to create a user, modifying an existing user account is no big deal. The usermod command, which is used for this purpose, has a lot of options that are the same as the options used with useradd. For example, execute the following command to set the new primary group of user linda to the group with the unique ID 101:

usermod -g 101 linda

The usermod command has many other options; for a complete overview, consult the appropriate man page.

Another command you will need occasionally is userdel. Use this command to delete accounts from your server. Basically, userdel is a simple command; for example, userdel lynette will delete user lynette from your system. However, if used this way, userdel will leave the home directory of your user untouched. This may be necessary to ensure that your company still has access to the work a user has done, but it may also be necessary to delete the user's home directory as well. For this purpose, you can use the option -r; for example, userdel -r lynette will delete the home directory of user lynette as well. However, if this home directory contains files that are not owned by user lynette, userdel can't remove the home directory. If this is the case, use the option -f; this will make sure all files from the home directory are removed, even if they are not owned by the given user. So finally, to make sure user lynette is removed, including all the files in her home directory, execute userdel -rf lynette.

You now know how to remove a user including the user's home directory. But what about other files the user may have created in other directories on your system? They will not be removed automatically when using userdel. If you want to make sure these other files are removed as well, you may find the find command useful. With find, you can search for all the files owned by a given user and remove them automatically. For example, to locate all files on your system that are created by lynette and remove them automatically, you can execute the following:

This may, however, lead to problems on your server. Imagine an environment where lynette is an active user of the group sales and has created a lot of files in the directory /home/sales; they will all be removed as well, and that may lead to serious problems. Therefore, I don't recommend using the -exec option to remove files immediately; instead, copy them to a safe place. If after a couple of months no one has complained, you can remove them. To move all files owned by lynette to a directory called /trash/lynette (which must have been created beforehand), execute the following:

Was this article helpful?

0 0

Post a comment