Adding Users

Last edited

Manually adding a user

Note

A program already exists that does all this: adduser or useradd

  • Edit the passwd and shadow files to define the user’s account.
  • Add the user to the /etc/group file (not really necessary, but nice)
  • Create, chown, and chmod the user’s home directory
  • Copy default startup files to the user’s home directory.
  • Set an initial password adduser/useradd doesn’t do this, use passwd

Setting a passwd

sudo passwd user

Creating home directories

There’s nothing magical about home directories.
If you neglected to include a home directory when setting up a new user, you can create it with a simple mkdir.
You need to set ownership and permissions (700 or 750 for groups) on the new directory as well, but this is most efficiently done after you’ve installed any local startup files.

Startup files traditionally begin with a dot and end with the letters rc, short for “run command,” a relic of the CTSS operating system. The initial dot causes ls to hide these “uninteresting” files from directory listings unless the -a option is used.

  • Sample startup files are traditionally kept in /etc/skel.
  • Linux also keeps fragments in /etc/profile.d for several shells.

typically looks like this:

cp -r /etc/skel/. /home/username/
chown -R username:username /home/username
chmod 700 /home/username

Forcing a pw change

Ensure the user changes their password:

  • Set the password to expire within a short time
  • Or have a script check their hash in /etc/shadow has been changed (could be the same password since salt changes the output but it’s something..)

Scripts for adding users

SystemCommandsConfiguration files
All Linuxuseradd, usermod, userdel/etc/login.defs
/etc/default/useradd
Debian/Ubuntuaadduser, deluser/etc/adduser.conf
/etc/deluser.conf
FreeBSDadduser, rmuser/etc/login.conf

a This suite wraps the standard Linux version and includes a few more features.

parameters stored in /etc/default/useradd include:

  • home directories
  • default shell for new users

This can all be configured via the CLI interface as well but this file sets defaults

useradd disables accounts by default you must configure a passwd.

useradd example

sudo useradd \
  -c "Alice Johnson" \  # full name
  -d /home/ajohnson \   # home directory
  -g developers \       # primary group (/etc/passwd)
  -G sudo \             # addition groups (/etc/groups)
  -m \                  # create home dir
  -s /bin/bash \        # login shell
  ajohnson              # username
sudo passwd ajohnson

# Results
# /etc/passwd
ajohnson:x:1002:1002:Alice Johnson:/home/ajohnson:/bin/bash
# /etc/groups - had to create manually with groupadd
developers:x:1002: 

adduser on Debian and Ubuntu

Pithy

useradd = low-level
adduser = do the right thing wrapper with sensible defaults