The userdel Command
Start course
Difficulty
Intermediate
Duration
2h 42m
Students
252
Ratings
4.6/5
starstarstarstarstar-half
Description

In this course, we'll cover a range of topics designed to help you enhance your Linux scripts. We'll start off by looking at case statements, which are used to make a decision based on the value of a given variable. We'll cover functions before moving and then move on to how to process command-line options using the shell built-in getopts.

In the second part of the course, we'll look at managing users including how to disable, delete, and archive users on a Linux system. We'll then do a walkthrough exercise showing you how to delete a user, which you can follow along with.

This course is part of the Linux Shell Scripting learning path. To follow along with this course, you can download all the necessary resources here.

Learning Objectives

  • Learn about case statements and functions to make your scripts more efficient
  • Process command line options using getopts
  • Manage users in Linux

Intended Audience

  • Anyone who wants to learn Linux shell scripting 
  • Linux system administrators, developers, or programmers

Prerequisites

To get the most out of this course, you should have a basic understanding of the Linux command line.

Transcript

Now let's find out how to actually use this userdel command. Again, since it's a file on the file system, we know we can use the man subsystem to get some information and help on this particular command. Briefly, the userdel command deletes a user account and related files. If we look forward here to the -f option, it stands for force and it removes the account even if the user is logged in and it also deletes the user's home directory, even if that home directory is shared with another user. Normal user accounts don't share the same home directories usually, or they shouldn't. But in some cases you might have multiple application accounts that share the same home directory. There's a little note here that says to use caution when using this option. Now let's keep looking through the options here. The -r option is the one that we're most interested in as it removes the user's home directory. So let's try out this userdel command. First let's look at some users on our local system here. We'll do tail /etc/password, and we can see we have einstein, isaac, tedison, jLocke, et cetera. And these are all accounts that were created through previous exercises. And through some of my previous work I've been doing on this virtual machine. So if we do this, let's delete the einstein user, sudo userdel einstein and hit enter. Now, if we run id against the einstein user, it'll say, hey there is no Einstein user available. So sure enough, the userdel command got rid of einstein and let's look in the home directory. So this really proves to us that running userdel without any options removes the user, but leaves their files, because you can see here the Einstein directory is still listed in /home. This might come in handy if you want access to a user's files after they have been removed. Now there's something else to notice here. Notice that the user and group for the Einstein's home directory are actually numbers. This is because there's no account associated with UID 1001 any longer, because we deleted that account that had the UID of 1001, which is the einstein account. So if you ever run an ls command and see numbers, where you would normally see a user then know that you're looking at a UID or GID, and that there is no account associated with that particular UID or GID. So while we're talking about UIDs, let's look at a couple of users. The root user always has the UID of zero. So if we do ID -u, which returns a user ID follow that with the root account we should always get back zero. Now there are some other accounts on the system that are considered to be system accounts or application accounts. Let's look at another one of those. Let's look at the sshd account. Here it has an id or user id of 74. Now let's look at the UID for our current vagrant user. It has a UID of 1000. As you saw earlier, einstein has a UID of 1001. So I don't know if you notice a pattern here but really system accounts have lower UIDs. This is actually set in a file called login.defs and that's located in etc. So let's just look at that really quickly. Page down here until we get here. Here, we see UID_MIN UID_MAX. So these are the minimum and maximum values for automatic UID selection and user add. So when you create the very first user on a Linux system, it's going to have a user ID of 1000. And by the way, that is the user ID of our vagrant user because that was the first account created on the system. Then the next time we add an account, the number will get incremented by one. So einstein was the next account that was created. So it had a UID of 1001 and so on. By the way, if you look below this you see SYS_UID_MIN and SYS_UID_MAX. This means that the maximum user ID for a system account or what's considered a system account is 999. If you want to make sure that you're not deleting a very important account, perhaps it's a good idea to check its UID first. If it's less than a thousand, then clearly be careful of what you're doing because it could operate a service that you may need on your server. Okay, let's jump out of here and let's move on to our next user ID -u, isaac. This user has a UID of 1002. Let's delete this user, but this time remove its home directory. All right, if we tried to get the user ID of the user again, it says, now it's deleted. It's gone. There is no such user. And if we look for a home directory for this user, it doesn't exist. So again, that proves the -r option removes the home directory and all the files that were within that home directory as well.

About the Author
Students
16725
Courses
61
Learning Paths
18

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics