Greg's Blog

helping me remember what I figure out

Adding and Removing Users Under Linux

| Comments

Adding and removing users under Linux

I guess one of the more important things to do is to know how to administrate users under Linux. This little document will show you how to add and remove users. Everything in this document relates to administrating users from the shell. The reason being that you can do all this remotely by telneting into the server and hence don’t have the graphical user interface that you usually have if you are running X-Windows.

Adding users
Let’s first off start by creating a new user. Not only will the user be able to login into the Linux server, but this process also automatically creates a user email account. So let’s begin. You start off by having to have superuser access rights or by being logged in as root. Also in the examples don’t type the information in brackets, unless othewise specified, as it’s only their for comment purposes. Next you can type:

adduser username

It’s a pretty straightforward command where adduser is the command to be executed and username is the name of the actual user you wish to create an account for. So if we say had a user named John Smith and you had the user naming convention of using the first name initial and the surname your command would look something like this:

adduser jsmith

Decide as early on as possible which type of naming convention you are going to choose and apply it rigourosly. I recommend using the first name initial followed by the surname, as it gives you more scope than using say firstname followed by the first letter of the surname. Also you can use other symbols such as full stop or underscore when creating user names, i.e. j.smith or j_smith.

Once you have entered the command adduser username the system will remind you to set the username password. You do this by typing the following command:

passwd username

Yes it is passwd and not password. You will then be prompted to enter the username password and confirm it after you have hit return once. If you fail to confirm the password the password is not set (by either hitting return or mistyping it) and you’ll have to type passwd username. Again think about your password convention. Later versions of Linux enforce a strict policy, if I remember correctly no less than 6 characters an it’s not allowed to be a dictionary word. Older versions don’t necessarily enforce this policy, so it’s up to you as the administrator to enforce it. For security purposes try also mixing upper case and lower case, and also make the password alpha numeric. UK car number plates make great passwords, as long as it’s not your current car sat outside in the car park. So for our user John Smith the complete sequence would look something like this:

passwd jsmith
Enter the password:******
Confirm password:******
Password set.

Removing users
Deleting users is slightly more involving than adding users as you have tomake sure that all references have been removed from the system. Again newer versions of Linux are better at it than the older ones. But for safety’s reasons we’ll go through all the files you need to check. You’ll need to be familiar with Vi editing (see Basic Vi editing for an introduction).

Right onto the ineteresting stuff. First off let’s delete the user account. To delete a user you need to type in the following command (again you’ll superuser access rights or be logged in as root):

userdel -r username

Again subsitute username for your actual username, using John Smith as our example your command would look as follows:

userdel -r jsmith

Also notice that we have used a switch (-r), for a whole listing a switches and their explanation you can type man userdel from the command prompt. Right now you have removed the username from the user database, all you need to do now is make sure that all the user directories and entries in the password file have been removed. First off let’s check the directory entries. Linux when adding a user creates a directory with the users login name in the /home directory. Secondly it creates a file, again with the user’s login name in the /var/spool/mail directory, where all mail messages are held if they are kept on the server. So if you find an entry in any of those directories (making sure that you change to the relevant directory first), you can type:

rm -rf username

Or using our example:
cd /home
ls -l |more
(if entry is found)
rm -rf jsmith
cd /var/spool/mail
ls -l |more
(if entry is found)
rm -rf jsmith

This command should remove the users directories and files (again for a complete listing and meaning of the command rm switches type: man rm). Now we need to check that all entries relating to the user have been removed from the password file. So change to your /etc directory and open up group file in your vi editor (type: vi group) and scroll through it looking for and entry relating to your user, it will look something like this username:501:. Delete the entry and then quite and save the ammended file.

You don’t have to stop or restart the server. Just make sure you edit that file carefully. That completes the introduction to Adding and Removing users. Hope you found it useful.