Modify users password via script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify users password via script?
# 1  
Old 08-15-2007
Modify users password via script?

Ok, first off, I'm working on a Vmware ESX server, which I guess is loosely based off of Red Hat 9. But I'm brand new to it (today), so be nice.

I'm trying to write a useradd script that will create some users, generate a password, and set their password to this newly generated password.

Now by hand, I can use the useradd command, and then use passwd to change their password. But if I've got to set up a couple hundred users, this is not enjoyable.

So if I can't use passwd in a script (because it requires user interaction), how can I create these users with some default password?
# 2  
Old 08-15-2007
This is on an ESX 3.x.x box, but you've got a few options.

The first is to use the --stdin option to passwd, e.g.
Code:
# useradd -m -d /home/foo foo
# echo "foo" | passwd --stdin foo
Changing password for user foo.
passwd: all authentication tokens updated successfully.

This would require you storing the plain text password in your script. A *much* safer option is to add a user and set the password as you normally would to a standard value, e.g.
Code:
# useradd -m -d /home/tmpuser tmpuser
# passwd tmpuser
...

Now, you can use the encrypted password for this user when creating other accounts, so that all newly created accounts have the same password as "tmpuser", e.g.
Code:
# useradd -m -d /home/newuser -p `awk -vFS=':' '$1 ~ /^tmpuser/ {print $2}' /etc/shadow` newuser

Cheers,
ZB
# 3  
Old 08-21-2007
Quote:
Originally Posted by zazzybob
This is on an ESX 3.x.x box, but you've got a few options.

The first is to use the --stdin option to passwd, e.g.
Code:
# useradd -m -d /home/foo foo
# echo "foo" | passwd --stdin foo
Changing password for user foo.
passwd: all authentication tokens updated successfully.

This would require you storing the plain text password in your script. A *much* safer option is to add a user and set the password as you normally would to a standard value, e.g.
Code:
# useradd -m -d /home/tmpuser tmpuser
# passwd tmpuser
...

Now, you can use the encrypted password for this user when creating other accounts, so that all newly created accounts have the same password as "tmpuser", e.g.
Code:
# useradd -m -d /home/newuser -p `awk -vFS=':' '$1 ~ /^tmpuser/ {print $2}' /etc/shadow` newuser

Cheers,
ZB
Thanks for the reply. I would have replied back sooner, but haven't gotten a chance to try it out till now. I actually like the --stdin option. The script actually won't be holding a plain text password. What I would like to do is generate a random password in my script, and pass it to --stdin.

I just have one problem. I'm really new to Vmware ESX, but I was able to find a little script that generates a password. Here it is:

Code:
MAXSIZE=8
array1=(
q w e r t y u i o p a s d f g h j k l z x c v b n m
)
MODNUM=${#array1[*]}
pwd_len=0
while [ $pwd_len -lt $MAXSIZE ]
do
    index=$(($RANDOM%$MODNUM))
    echo -n "${array1[$index]}"
    ((pwd_len++))

echo
done

As you can see, all that script does is generate the password, and then echo it out. But I've never seen where you can just use "echo" and not tell it what to echo. So what variable is my password being stored in? If it's $index, then how can I use it with --stdin?

The problem comes when I try to use it, putting this in my code:
Code:
# echo "$index" | passwd --stdin foo

because the "echo" is also printing out the password, so do you know how I can use this to my advantage?

thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for creating multiple users with password

for UserName in `cat users` ; do useradd -d /u02 -s /usr/libexec/openssh/sftp-server -G ftp-users $UserName ; PassWord=$( echo $( tr '' '' <<< ${UserName:0:1} )${UserName:1} ) ; echo "$PassWord@123" | passwd $UserName --stdin ; done can some one explain what the bold text do Please use... (5 Replies)
Discussion started by: James0806
5 Replies

2. Shell Programming and Scripting

To exclude users in password file

Hi, I want to exclude the users below 500 in the password file with the below script. #!/bin/bash # get date in dd-mm-yyyy format NOW=$(date +"%d-%m-%Y") exec > "/root/SYSINFO/User_details_`uname -n`_$NOW.txt" cut -d: -f1 /etc/passwd > /tmp/pass.txt && for i in `cat /tmp/pass.txt`;... (2 Replies)
Discussion started by: gsiva
2 Replies

3. Solaris

How to programmatically reset a users password.?

Hello all, I have a small C++ app for my solaris admins. I need to set it up so they can reset a users password. The admin does not have the old password. How can I reset a users password to a temp password either using passwd or PAM? I need to do this from within my C++ app. I have searched... (3 Replies)
Discussion started by: ChickenPox
3 Replies

4. Solaris

Change password for users

I am on SunOS SolarisServer 5.11 11.1 i86pc i386 i86pc , I am trying to change password for a user,but I get the following message.I cannot find any google help on the matter.can anyone help? root@SolarisServer:~# passwd passwd: Changing password for stain Please try again Please try... (6 Replies)
Discussion started by: cbtshare
6 Replies

5. UNIX Desktop Questions & Answers

Too many users with root password

Hi there, I'm working with a Linux server and now I can get a daily Logwatch mail ... my question is:since there are too many users with root password (...in my opinion... :mad:) how could I prevent to delete information about "su" log? Thanks in advance, GB (3 Replies)
Discussion started by: Giordano Bruno
3 Replies

6. Shell Programming and Scripting

script to change password for all users

We have a server where we have a number of user ids and we also have the list of old passwords in a CSV file. Now we want to change the password of all the users and assign them a default password.Can we write a shell script to do that. I am planning to read the user name and corresponding... (7 Replies)
Discussion started by: dr46014
7 Replies

7. UNIX for Dummies Questions & Answers

Need to sort/change modify 2300 users ...

Ok, so here's my project I've been given and I know very little about this. I have an AIX unix box that has over 2300 local users. Any user with 4 or less characters in the username does not get changed. Any user with 5 or more needs to have the attribute shell=/bin/ksh changed to... (1 Reply)
Discussion started by: tresani
1 Replies

8. HP-UX

How to enforce users not to modify their command history.

As a system administrator. sometimes we see the users are trying some commands dangerous for the system health and remove them from their individual coomand history file. How it is possible to enforce that the normal usres will will not be able to modify the history. Thanks in advance. Partha (4 Replies)
Discussion started by: partha_bhunia
4 Replies

9. UNIX for Dummies Questions & Answers

Have users changed their password

How can I know users have changed their passwords ? I don't need their password (!) I have to know if they have changed their pass word and when ? Thank you in advance for any SIMPLE answer. (6 Replies)
Discussion started by: annemar
6 Replies

10. UNIX for Dummies Questions & Answers

Setting password restrictions for all users

I would like to change the password requirements for all our AIX 5.2 logins but am having trouble finding a place where I can set the rules for everyone at the same time. I know I can go user by user in smit passwords but is there a way to create rules for everyone at the same time? Thanks,... (2 Replies)
Discussion started by: drathbone
2 Replies
Login or Register to Ask a Question