Linux

Creating a New User

June 29, 2009

Creating a user in Linux is a simple task. Well, kinda. You can quickly create a user without specifying much of it’s characteristics, or you can get much more in depth with security, default shells and other customizations. I recommend taking the time to learn beyond the basics or you may find yourself making a simple mistake. For the longest time I created my user’s with a command like this:

sudo useradd -d /home/username -m username
sudo passwd username

I had thought for a long time this was sufficient. It created their home directory with the -m parameter and specified that location with the -d parameter. After that I set the password and the user was now able to login to the system with basic privileges. But after a while I began to notice some quirks with those users. They didn’t act like the initial admin user that was created by the installation process.  I found that the prompt was a bit less informative.  It also lacked a history, and the ability to hit tab to complete a directory as you typed it.  I even found myself copying over the root .bashrc and trying to figure out what ws different between the users.  Then I typed in the following command just to make sure it was using bash:

echo $SHELL

I looked at the response.

/bin/sh

*face-palm*

No wonder .bashrc didn’t do anything, I was using a completely different shell for my subsequent users.  I had never taken the time to learn the difference between the various shells, most systems use bash by default so that is what I learned and got used to.  So I never even realized they were using sh instead of bash.

To quickly change the shell for an established user to bash just run the command:

chsh -s /bin/bash username

Since then I’ve adjusted my create user command to include bash as my default shell.

sudo useradd -d /home/username -m username -s /bin/bash

There are many more parameters that you can add on to this command. If you’d like to read up on those then just type man useradd and read through the documentation.  Or if you want a very verbose and easy to use command to create your users you can always try adduser.  Instead of passing in which parameters you want to specify it will prompt you on every possibility.  Great for if you don’t know which options exist, but also somewhat overwhelming in the beginning.

mradmin@microsoft$ sudo adduser username
Password: ******
Adding user 'username'
Adding new group 'username' (1337).
Adding new user 'username' (1337) with group 'username'
Creating home directory '/home/username'
Copying files from '/etc/skel'
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []: Example User
Room Number []: 0
Work Phone []: 604-6122
Home Phone []: 604-6222
Other []:
Is the information correct? [y/N] 

The choice of which two methods to use is a mater of preference. In the long run, what matters is that you educate yourself on what all of the available options do. And remember, man useradd and man adduser are your friends!

Only registered users can comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.