From the course: LPIC-1 Exam 102 (Version 5.0) Cert Prep

Add and removing users - Linux Tutorial

From the course: LPIC-1 Exam 102 (Version 5.0) Cert Prep

Start my 1-month free trial

Add and removing users

- [Instructor] It's good to know which files to edit in order to manage user account settings on Linux. User accounts are stored in /etc/password, passwords and account aging are stored in /etc/shadow, and other account defaults are stored in /etc/login.defs and /etc/default/useradd. This last file also specifies our skeleton directory. The skeleton directory contains the files that are copied to each new user's home directory automatically. We'll observe this in a moment. The process of creating users in Linux is pretty straightforward. Let's go to our terminal and run useradd. Type in sudo useradd and hit Enter. Here we see the many options for useradd, including specifying the user's home directory, user ID, primary group ID, supplemental groups, and shell. Thanks to the stored defaults, we don't have to specify any options to create a user. Any options that we leave out are taken from the system defaults. Now, let's create a new user named bob. Type in clear, and then type in sudo useradd bob and hit Enter. We can verify that this user exists by viewing the etc password file with cat. Type in cat /etc/passwd and hit Enter. The very last line is the account information for the new user named bob. The fourth column is the user's primary group ID. In this case, bob's ID is 1001. We can cross-reference this number in the etc group file. Let's cat /etc/group. Type in cat /etc/group and hit Enter. We see that bob's primary group is also named bob. In Red Hat-based distributions, the primary group is created automatically. The last thing we'll do is view the password and aging information in the etc shadow file. We'll need to elevate privileges using sudo for this, as this is where the hashed passwords are stored. Type in sudo cat /etc/shadow and hit Enter. The last line, again, belongs to the new user named bob. We can see that the second column, where the password should be, has two exclamation points. This means the password hasn't been set yet, and thus, bob can't log in. Let's set the password using the password command and view the etc shadow file again. Type in clear, and then type in sudo passwd bob and hit Enter, and then enter bob's password twice. And once that's done, view the shadow file again. Type in sudo cat /etc/shadow and hit Enter. We now see that the bob user has an encoded password in the second field. Now, let's take a look at /home for home directories. Type in clear, and then type in ls /home and hit Enter. We see the bob user's home directory is present. Type in sudo ls -la /home/bob and hit Enter. The files we see were copied from the etc skel directory when we created the user. These are the skeleton files. Deleting users is pretty straightforward. If you'd like to delete the users but keep their home directories and their files, type in sudo userdel bob and hit Enter. However, if you want to get rid of the bob account and bob's home directory, we'd want to type in sudo userdel -r bob. We can cat the etc password file to verify the account is gone. Type in clear, and then type in cat /etc/passwd and hit Enter, and we don't see it in the list. If we wanted to verify that bob's home directory has been removed, we're going to type in clear, and then type in ls /home and hit Enter. In our case, it has not, because we didn't provide the -r option. So let's manually delete it. Type in sudo rm -Rf /home/bob and hit Enter, and that should take care of it. Now, let's add a new user that we'll use later in the course. This new user's name will be user1, and we'll also give them a password and place them in the wheel group, so they have admin privileges. Type in clear, then type in sudo useradd user1 and hit Enter. Now, give user1 a password. Type in sudo passwd user1 and hit Enter. Now type in the password twice. Once you've typed in the password, add user1 to the wheel group. Type in sudo usermod -a for append, uppercase G for a supplemental group, wheel user1, and hit Enter. Now user1 should be able to use sudo with admin privileges. Now, let's verify that user1 is in the wheel group. Type in cat /etc/group and hit Enter. Scroll up until you get to the wheel group. You can see that user1 is listed. To view more useradd options, check the man page by typing in man useradd. Be sure to add user1 on rhos2 as well.

Contents