Adding Users to a Unix System

Posted on 2002-04-06
Last Modified: 2013-12-27
I am trying creat a script that allows me to add user to a certain staion.  Here are the requirements.  
1.  Log in as root(no prblem there)
2.  cd to cd/etc (directory)
3.  co -l passwd (checks out and locks the passwd file for revision control)
5.  vi passwd(edits the file)
6.  / next(Search for the word next)
7.  yy p(This copy the line)
8.  Then I edit the original line.(the username should be first 7 characters of the last name and the first character of the first name.
9.  Save and exit the passwd file.
10. ci -u passwd(checks in and unlocks the passwd file)
11.  Be able to log how use the file last
12.  mkdir username(make a dir with the new user name)
13.  chmod username(change the permission of the users home directory)
14.  cd /home/users/username(this changes directory to the users home directory)
15. ./home/users/(this part has to beable to setup the user enviroment(this creates the users enviroment)
16. pwconv( this adds the new users form the /etc/passwd file to the /etc/shadow file so that the passwd can be created.)
17. passwd username (changes the user password)
18. create a file that copies the passwd and the shadow files to different unix servers.

How can I do all of this?  I think the process is too long?  Help me.

I pretty new at this so please try to explain with as much details as you can.  The simpler you can make this for me the better.  Because I am a new kid on the block when it comes to unix.  Is there any way I can do the following task in a menu driven format?  For example; when I log is as root, I can type the name of a script to run, then it would ask me to add a user, the user name, the dir name, and such and such.  Then send the user infor to the other unix stations.
Question by:UnixKid
  • 3
  • 2
  • 2
  • +6
LVL 51

Accepted Solution

ahoffmann earned 300 total points
ID: 6923196
if [ $# -lt 2 ]; then
   exit 1
\cd  /etc
f=`\echo $1|\cut -c 1,1`
l=`\echo $2|\cut -c 1-7`
i=`awk -F: '{u[$3]=1}END{for (i=1000;i<65534;i++){if (u[i]==0){print i;exit}}}' passwd`
\grep -q ^$u passwd
if [ $? -eq 0 ]; then
   \echo $u already exists
   exit 2
\co -l passwd
\echo "$u:x:$i:$g:$1 $2:/home/users/$u:/usr/bin/tcsh" >> passwd
\ci -u passwd
# 11.  Be able to log how use the file last
\mkdir /home/users/$u
\chown $i:$g /home/users/$u
# 15. ./home/users/(this part has to beable to setup the user enviroment(this creates the users enviroment)
\passwd $u      # keep in mind that this ask interactive
# 18. create a file that copies the passwd and the shadow files to different unix servers.
# just one example (requires ssh setup properly):
\scp passwd different_unix_servers:/etc/passwd
\scp shadow different_unix_servers:/etc/shadow

# chmod 555 above script, execute as root and enjoy;-)
LVL 15

Expert Comment

ID: 6923902
isn't it easier if we stick to Solaris Admintools, or some other WebBased Admin tool like Webmin (

just a suggestion.
LVL 51

Expert Comment

ID: 6923990
might be easier, I thought about it too, but Admintool laks a revision control (SCCS, RCS, or whatever)
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 38

Expert Comment

ID: 6924419
hi ahoffmann,

   if you copy both /etc/passwd and and /etc/shadow to the
other box, it end up the aother box has the same user accout as the workstation you creadted the new account.

   what do you just save the new entry to a text file. and then just append the usr record to the passwd and shadow files.

    UnixKid, if you look after many UNIX boxes in your next work, and you want to save you time, try to set up NIS+, or NIS ( I perfer to use NIS+).




Expert Comment

ID: 6924431
some of what you are doing is better done with

you can spcify a user directory to create
with the -d and -m flags:

useradd -c "User comment or GCOS" -d /home/newuser -g group -m -k /etc/skel -s /bin/sh newuser

/etc/skel contains standard profiles etc - you can set one of these up for each user type - eg:


similar things can be done with usermod for existing users.

as for change control - the files : /etc/passwd and /etc/shadow are backed up prior to update.
ie /etc/oshadow /etc/opasswd.

Locking is handled by the OS.
If you must have versioning then run the command
with your SCCS or RCS checkout prior to the useradd/usermod
and then check it in after.

if you have access to EXPECT - you can change
passwords etc programatically.

ie something like:

# Run as root

set timeout 20
spawn passwd $USERID
expect {
    -re "(does not exist)|(denied)" {
        send_user "user does not exist, or access denied\n"
        exit 1

expect "password: "
send   "$PASS\r"
expect "password: "
send   "$PASS\r"
expect "successfully changed"
send_user "Password Changed for $USERID"
exit 0

LVL 51

Expert Comment

ID: 6924640
> .. and then just append the usr record to ..

then I'll have no check for duplicate account names and duplicate UIDs, see my script.

NIS might solve the problem of keeping passwd in sync, but does not create the home directory (which should be on NFS, I suggest)

Expert Comment

ID: 6924891
interesting discussion for a course (just listening)
LVL 38

Expert Comment

ID: 6925108
use NIS+, and setup auto_home, so the user's home dir can be mounted across to all the boxes in the next work. (Of course, the machine where the user real home located need to be a NFS server, in this case, this will save a lof of work).


Expert Comment

ID: 6946089
How many servers? Have you thought about using "rdist" to keep all the password / shadow files identical?

Author Comment

ID: 6960278
Look I don't know much about unix so make it simple.  I need something nice and clean.  Any good books that I can read to help me learn this stuff

Expert Comment

ID: 6961513
"Essential System Administration" covers many flavours of unix in a very readable and well-written way:
ISBN 1-56592-127-5

"Unix in a nutshell" is the ideal companion book with an explanation of commands and how they are used:

Both books have online content available at the O'Reilly and Associates site :

LVL 20

Expert Comment

ID: 8052641
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by ahoffmann

Please leave any comments here within the next 7 days


Cleanup Volunteer

Expert Comment

ID: 8099605
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (…
FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question