Pages

Tuesday, June 21, 2011

Adding a new user to a UNIX based system

This page is all about adding users to your system using the command line. All the all steps in account creation will be explained. On this post I will go over adding the user to the system with the useradd and adduser commands. Then I will use the passwd command to set the users password on the system. I also mention the usermod command that modifies existing system accounts.

This page is a work in progress if you have an input post below and I may add the content to this blog.

The useradd and adduser commands add new user to the UNIX based system.
Affected files:
/etc/passwd
/etc/shadow
/etc/usr_attr
/etc/groups

Some System Administrators add accounts to there systems by editing the above files by hand or with a script. It can be done this way without any problems, but using the useradd and adduser commands are better because they copy the default files to the new users home directory and set the proper permissions
useradd command syntax
useradd [options] {username}
Example:
root@earth> useradd -u 25 -g staff -G ftp,users -m -d /export/home/newuser -c "newuser" -s /bin/bash newuser

Explained
-uSets users ID to 25
-gSets primary group membership to staff
-G Sets secondary groups memberships to ftp,users
-mMakes the uses home directory
-dSets path to home directory
-sPuts in a comment into the /etc/passwd file.
-sSets users default shell
newuser       Put the name of the account at the end
-fSets the number days the account can be inactive before it is locked (Solaris). For Linux systems it sets the number of days after the users password expires before the account is locked.
-eSets account expiration date

In many Linux distros you need only to do is this.
useradd  newuser
The OS will fill in the rest for you based on the system default.

adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire mm/dd/yy] [-p passwd] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell] [-f inactive] [-e expire mm/dd/yy]

If you make a mistake then you can use usermod to modify an existing account. The usermod command works just like useradd,

passwd command changes the password of a user account.

passwd username

Remove password hash in /etc/shadow for a user.
passwd -d username

passwd newuser
  • Note: if you don't put a user name at the end of the passwd command then it will change the root password.