Unix stores account information in 2 files. These files are the password file /etc/passwd, and the shadow file /etc/shadow. Each user belongs to a group with this information being stored in /etc/group. Refer to the man pages for the purpose and format of these files. To successfully add a user, a line must be added to the password and shadow files. Because of the importance of these files to the systems operation, any production quality script should ensure that they are not corrupted by any circumstances during an update.
Your script MUST take the following arguments:
-P to specify the name of the password file to update.
-S to specify the name of the shadow file to update.
-G to specify the group file to consult.
Such arguments MAY be followed with:
-p to specify the new entry to be added to the password file.
-s to specify the entry to be added to the shadow password file
Do assign the default password for all the users created. This may take a bit more time, but don’t give up, do explore. Good luck!
Do check for errors, for example, if the user name entered already exist, then it should state that “User Exist! Try another Name”. Lastly in some circumstances you will have to make decisions. For example let’s say a home directory is specified that already exists and belongs to another user. What should you do here? This is a design decision – normally you would terminate with an error. What if the home directory does not exist? You would have to create it right? but also ensure the permissions are correct…All directories should be under /home – this is the norm.
In other words think about what could go wrong and try to handle it nicely… You should put comments in your code justifying your actions.
To test your files simply examine them and try to log in with a user you created.
can someone just explain to me then i start to implementing?
not really understand the question~
It looks like you are to write a UNIX shell script to handle user accounts on UNIX. Your instructor wants your script to write to the /etc/password (or a file with the same format); /etc/shadow (or a file with the same format); and the /etc/group file (or a file with the same format.)
Your instructor suggests that you should run the "man" command on these files and other UNIX commands to get ideas on how to approach your script.
Your instructor has given you lots of ideas on how to check for errors, and what errors you need to handle.
the user will auto add to
/etc/password
/etc/shadow
1 2 3
cat /etc/passwd
//or
cat /etc/shadow
already isn't?
so i not really understand at your script to write to the /etc/password (or a file with the same format); /etc/shadow (or a file with the same format); and the /etc/group file (or a file with the same format.)
the user will auto add to
/etc/password
/etc/shadow
1
2
3
cat /etc/passwd
//or
cat /etc/shadow
This depends on what your instructor wants you to build. If you are building a new script that does the same job as useradd, then what I described in my first posting still stands.
If your instructor wants you to build a script that callsuseradd, then I need to modify my answer.
Can you tell me which way you are supposed to code it?
echo "Enter username: "
read username
useradd $username
//when i press useradd $username
//$username will be auto save to
// /etc/passwd
// /etc/shadow file already
so i don't know what is mean by the
-P to specify the name of the password file to update.
-S to specify the name of the shadow file to update.
-G to specify the group file to consult.
sorry for disturbing again
i think my lecturer just want me do to some basic scripting. just i can't get what her mean.
and now holiday. not able to ask her
Hardcode is bad. It is much more flexible to set options in command line. Then again, repeatedly writing the same, most common options is boring. Hence, a wise default is convenient.
1 2
# foo -P mypw -G mygrp
# foo
Two calls to "foo". The first gets four options. If "foo" would be your script, then it would use files "mypw" and "mygroup".
The second call would use the default values: "/etc/passwd" and "/etc/group".
No. I totally forgot the username parameter from the example. "foo" is the name of a script.
You cannot use "useradd", because it is hardcoded to operate with /etc/{passwd,shadow,group,gshadow}.
IMHO, an exercise that would potentially wreck the authentication files is very very bad. You really should not mess with them and you don't want to make copies either.
The instructions that you did show clearly state that your script must be able to handle arguments. The linked thread shows an example on how to handle arguments.