I have been tasked with the following: You have been asked by your boss to set up 100 user accounts for your SLES9 Linux server. Each user account should have the following details: Login name: userxx Password: p@Ssxx UID: 1900+xx GID: 747 (name of group is IMFDelegates) Informational name: “Secret Agent xx” Home directory: /home/agentxx Login shell: /bin/sh For example, the first user will have the following details: Login name: user00 Password: p@Ss00 UID: 1900 GID: 747 Informational name: “Secret Agent 00” Home directory: /home/agent00 Login shell: /bin/sh And for example, the last user will have the following details: Login name: user99 Password: p@Ss99 UID: 1999 GID: 747 Informational name: “Secret Agent 99” Home directory: /home/agent99 Login shell: /bin/sh Note: You will need to create a shell script to automate this task. You can make use of the groupadd and useradd scripts inside your shell script. You can also use the mkpass command to create the encrypted passwords. Would like to have some pointers from the experts here on creating the script, and at the same time showing me source codes of creating the scripts as mentioned above. Thank You! Lastly i would lke to know thea meaning of UID and GID? Not forgettiing the examples of these 2.
Yea... The problem is how do i go about creating the script and how do i add the command to create user name and password. Especially if i should use "for" loop or the "do while" loop to make count the number of users. Btw if i use the "man useradd" and "man groupadd" means i create the group 714 and and user 1900?
You won't learn by having people just hand you working source code... man useradd and man groupadd lays out the syntax for using those commands to do what you want. http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html is a link to a site showing basic bash scripting. You'll have to put in some effort and come up with an attempt at writing it yourself before people will help you (generally speaking), otherwise what's the point? Why bother "learning it"? And, I don't think it's even so much how to do this script, but how to find the information you need to create the script, that's more important than the script itself. After you first posted this thread I decided to write one myself, and searched, and tested through trial and error, and found out how to do it. I now have a working script that does exactly what you want. So, put some effort into it and I will help you further, post your code, and explain what you are having trouble understanding... ...or not, it's up to you.
here is my source code need some advice Code: #!/bin/sh sux=”sux” dash=”-“ passwd_1=”123pass” gad=”groupadd” g=”-g” gid=747 re=”IMFDelegates” vi=”vi” ep=”/etc/password” uid=1900 num=0 u=”user” cross=”x” iname_1=”Secret” iname_2=”Agent” homedir=”/home/agent” logshell=”bin/sh” p=”passwd” password_2=”p@Ss” $sux $dash #input command line sux – to enter root $passwd_1 #input password 123pass $gad $g $gid $re #create group with command line groupadd –g 747 IMFDelegates while [ $num –lt 99] do echo $vi $ep echo $u:$cross:$uid:$gid:$iname_1 $iname_2$num:$homedir$cross:$logshell echo $p $u$num echo $passwd_2$num num=$((num+1)) done echo “All 99 user accounts created”
Okay, have you tried running your code? Does it do what you need? I have a few words of advice: 1) You do not need to declare so many variables. Variables generally hold values that change, hence the word variable. So, in our script, the only value that really changes is the User ID. For each different User ID we perform the same actions. We can virtually eliminate all the variables you have declared, except one. I'll give you a hint: keep the $uid variable. 2) System commands do not need to be declared as variables. They can just be called from the script. That is what a script is; a series of system commands. So things like groupadd and useradd can just be called. HINT: you may need to set the path to your user commands depending on the distibution you are using. For instance in Fedora 7 (which I am using) I need to be specific about calling /usr/sbin/useradd. Just calling useradd gives me an error command not found. 3) The biggest problem I see with your code is that you are trying to manipulate the /etc/passwd file directly. THIS IS NOT A GOOD IDEA. The useradd, usermod, and userdel commands will handle all this for you, and they will edit the file correctly. Use the tools in the toolbox and don't try to reinvent the wheel. Letting these system commands do the work for you will also cut down your code substantially (The script I have written is only 8 lines long). You are on the right track. Your use of the while loop is correct, though the contents of it are not (again, do not manipulate the /etc/passwd file directly). Give it another try, taking into account the advice I've given here.
okie so i just type groupadd –g 747 IMFDelegates in to the script itself? Btw i using "konsole" CLI program from my SUSE LINUX ENTERPRISE SEVER 9. The part on manipualtion i dun understand can show me another examples on how to avoid the manipuation on etc/passwd?
Just so you know, I am presuming what your code will do because I do not want to run it on my system. Yes, you can put that call into your script. Do not put it in the while loop because we only need to create the group once, not each time we create a user. Basically it looks to me like your script is trying to open the /etc/passwd file with vi editor, and write a new line with the user information: Code: while [ $num –lt 99] do echo $vi $ep echo $u:$cross:$uid:$gid:$iname_1 $iname_2$num:$homedir$cross:$logshell echo $p $u$num echo $passwd_2$num num=$((num+1)) done Is that what you are trying to do here? If you pass your new user information to the useradd command instead (in the form of command-line arguments), useradd will make the entry in /etc/passwd for you. It will also create the home directory for the new user. Type useradd --help or man useradd for information on using that command. HINT: It will look something like this (I'm pretty much giving it to you): Code: useradd -c common-name -d /home/username -g gid -u uid -s shell -p password login-name
EDIT: for above (I am still unable to edit my posts ) The last line login-name should be the last part of the useradd command in the code tags!
To run the script you would use: Code: bash <scriptname>.sh I'm just going to look over your code in the PM and I will respond ASAP.
I will go through some parts of your code with you, though I know that you have not run this code because it will not do anything productive: Code: sux – #input command line sux – to enter root echo“123pass” #input password 123pass Here you are trying to use the script to automate switching to root. It is extremely dangerous to include your root password in a shell script. This is clear text and when you try to run this piece of code you will still be prompted for your root password, and then your password will be echoed to the command-line. Very dangerous from a security standpoint. And regardless of that, the code will not work. Code: groupadd –g 747 IMFDelegates #create group with command line groupadd –g 747 IMFDelegates Good. Create your group. Code: useradd -c common-name -d /home/username -g gid -u uid -s shell -p password I said that the code will look something like this. You just cut and pasted that into your code, but it is the wrong syntax and in the wrong place. I said that i pretty much gave it to you, not that I did give it to you. BAD! And this will not do what you want it to do. Code: while [ $num –lt 99] do echo user:x:$uid:747:Secret Agent${num}:/home/agent:/bin/sh #create account num=$((num+1)) passwd user${num} #create password echo “p@Ss${num}” done It is inside the while loop that you should have your useradd syntax. Instead, you are still trying to recreate the format of the /etc/passwd file, though now you are just echoing it to the command-line. The $num variable is present as a counter, but you don't really need it, you can just use the $uid to count (one less variable to worry about). Basically I hope you are just having a hard time digesting the way scripting works because it doesn't look like you've put too much effort into making this script work. Again I have to ask if you've even tried to run it? Against my better judgement, here is the script I wrote. I just hope that when you take this into class and they ask you what each part is doing, you can explain it: Code: #!/bin/bash path="/usr/sbin/" uid=1900; ${path}groupadd -g 747 IMFDelegates while [ ${uid} -lt 2000 ]; do ${path}useradd -c "Secret Agent $(($uid-1900))" -d /home/agent$(($uid-1900)) -g 747 -u ${uid} -s /bin/sh -p p@sS$(($uid-1900)) user$(($uid-1900)) let uid=uid+1 done
Yes i managed to run successfully! Thank you very much on your source code i get the group, user id and user name correct the only problem i face is the password whe i keyed in correctly it says login failed. Do i have to make changes in the shadow file for it to recognise the password? anyway i was tasked to create a script to delete the useracct and groups here is my code Code: #!/bin/bash path="/usr/sbin/" uid=1900; while [ ${uid} -lt 2000 ]; do ${path}userdel -p "Secret Agent $(($uid-1900))" -d /home/agent$(($uid-1900)) -g 747 -u ${uid} -s /bin/sh -p user$(($uid-1900)) let uid=uid+1 done ${path}groupdel -p 747 IMFDelegates When i run it says error for userdel.....
Again, look at the documentation for these commands. man userdel or userdel --help man groupdel or groupdel --help Your answer is there.