Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Adding Users to a Unix System

Posted on 2002-04-06
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +6
LVL 51

Accepted Solution

ahoffmann earned 1200 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)
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 21

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

704 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