Useradd issue

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Useradd issue
# 1  
Old 04-05-2010
Useradd issue

Hi all,

I'm using Red Hat Enterprise Linux Server release 5 (Tikanga)

I have a query regarding adding users, I have a requirement of creating multiple users at one go.
In order to do so I have written a shell script which would create a user and set the password at one go.
Now the problem is

1] Setting password.

I have tried two approcahes

Code:
password=`echo "PASSWORDSTRING"|md5sum`

useradd -p $password $username

and

Code:
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

useradd -p $pass $username

In both the cases I'm able to create user without a problem.How ever when i try to login as the user Im unabel to as the password is in correct.

Following is the script

Code:
if [ $(id -u) -eq 0 ]
then
cat /root/newlis|while read IN
do
        username=`echo $IN|awk -F ":" '{print $2}'`
        password=`echo $IN|awk -F ":" '{print $3}'`
        egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]
then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
grep "$username" /etc/passwd >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "User $username added" >> /root/user.log
else
echo "Problem adding $username"
fi
fi
done
else
echo "Only root may add a user to the system"
        exit 2
fi


Please suggest.

Thanks and Regards
Syed

---------- Post updated at 03:53 PM ---------- Previous update was at 03:13 PM ----------

Well ,the intresting thing is the passwords are valid as I m doing as follows

I m logged in as root so I am

Code:
su - newuser

passwd

it asks me for old pasword and then lets me change the pasword without any errors.Which means the password assigned by the script is OK ,its only that iam unable to login for some reason.

Last edited by pludi; 04-05-2010 at 08:20 AM.. Reason: code tags, please...
# 2  
Old 04-05-2010
I thought the encrypted password was generated using crypt(3), see:
Man Page for shadow (Linux Section 5) - The UNIX and Linux Forums
# 3  
Old 04-05-2010
MySQL

Code:
 
password=`echo "PASSWORDSTRING"|md5sum`

this is create md5 checksum key for `PASSWORDSTRING`
this is not user password..

and
password algorithm mechanizm must be below
char *crypt(const char *key, const char *salt); //syscall
# 4  
Old 04-05-2010
Yes the

Code:
password=`echo "PASSWORDSTRING"|md5sum`

is not the user password,but iam trying to generate a md5sum encrypt password string which i could use in the following command

Code:
useradd -p $password $username

The "PASSWORDSTRING" is my desired password. which iam piping to md5sum to generate a encyprted password to feed in the command useradd using the -p switch.

I may be wrong.Ygemici,could you please elobarate on
Code:
char *crypt(const char *key, const char *salt); //syscall

any refrence/guidance would be highly aprreciated.

Thankds and Regards,
Syed

Last edited by pludi; 04-05-2010 at 08:20 AM.. Reason: code tags, please...
# 5  
Old 04-06-2010
Perl crypt()

See perldoc -f crypt.
Quote:
crypt PLAINTEXT, SALT
Where PLAINTEXT is the password in this case
Where SALT is a two character string, matches regexp /[./0-9A-Za-z]{2}/
Many people use seconds to come up with a SALT string but in a tight loop you may pull the the same second. Do something with this...
Code:
sub fractime () {
  use Time::HiRes qw ( time );
  my $now = time;
  $now -= int($now);
  return $now;
}

That will give you a factional time where you can use string operators to suck out two digits at a time and the modulo them to the set of 64 characters available for the SALT with...
Code:
sub randsalt($) {
  my ($fractime) = @_;

  my $saltset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
  my $salt = "";
  for (my $i = 0; $i < 2; ++$i) {
    my $s = substr($fractime, ($i * 2) + 2, 2) % length($saltset);
    $salt .= substr($saltset, $s, 1);
  }
  return $salt;
}

So we call these two functions in Perl and return the password.
Code:
my $ft = fractime();
#print $ft, "\n";
my $salt = randsalt($ft);
#print $salt, "\n";
print crypt($ARGV[0], $salt), "\n";

See attachment for Perl code and remember to 'chmod' it. We can now get the proper password in BASH with...
Code:
#!/bin/bash
PASS=$(./test8.pl somepassword)

I tested the 'useradd' command and it worked correctly with adding the account under Fedora12. Sorry for the hedge bet but the documentation says the default '-p' option is to disable the account? This sounds strange.

Good Luck.
# 6  
Old 04-06-2010
Thanks a million mx1ram

Hi,

I actually fixed the issue an hour ago , actually i had this system configured for kerbores authentication ; I did the basic thing which i was dumb not to do initially i checked with /var/log/secure ( it was an issue with pam authentication )

b) Had a look at my /etc/nsswitch.conf file following were the entries

passwd: files winbind
shadow: files winbind
group: files winbind


I changed it to

passwd: files
shadow: files
group: files

This worked.

Your code ( i did test it on a different machine) works neatly Smilie.

I will have to explore Kerberos.

Again Thanks a Lot for you time and effort appreciate it.
# 7  
Old 04-06-2010
MySQL

Sorry for later answer..

crypt() is a C function for encrpyt data for use to passwd,login vs commands..
I suppose for work to crypt() must be include some headers and some librarys.Instead of easy way use to openssl or php or perl..

Actually your problem is already solved..

And additional m1xram informations

i can try to a basic script only diffrence from you i am preference manually give password to users..

Code:
#!/bin/bash
usernamex="$1"
#read -p "Please to create a username " usernamex -- if you manuel entry for username
echo "user $1 for password ... "
read -sre passwordx
echo "password setting up ... "
echo ""
sleep 2

function salt ()
{
        characterset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
        saltkeylength=8 #it is random character length give what you want to which length
        local i
        i=0
                while [ $i -ne "$saltkeylength" ]
                do
                let indis=$RANDOM%${#characterset}
                saltkey+="${characterset:$indis:1}"                
                let i+=1
                done
}
salt ""

echo "md5 hashing password is get...ok!"
sleep 2

userpasswordx=`openssl passwd -1 -salt $saltkey $passwordx`
#openssl passwd -1 $passwordx -- if you encrypt password without saltkey
useradd -p $userpasswordx $usernamex

# and can add your error control code
if [ $? -eq 0 ]
        then
        echo "User $usernamex successfully added...ok!"
                else
                echo "Problem adding $usernamex"
                fi


Code:
root@rhnserver include]# ./useraddx.sh testuser
user testuser for password ...
password setting up ...

md5 hashing password is get...ok!
User testuser successfully added...ok!

[root@rhnserver include]# cat /etc/shadow | grep testuser
testuser:$1$Kl$SLUJfKKAMALM5fDPFXweR0:14695:0:99999:7:::



And for kerberos authentication first you install samba and winbind services packages..

and configure your /etc/samba.conf
add to realm option
for example
Code:
 
realm=yourdomain.com

and then you must start the samba service..


and test your pdc connection controller
Code:
nmblookup yourdc1 



if test is ok you can join the domain

Code:
smbpasswd –j yourdomain.com yourdc1 –U administrator



/etc/nsswitch.conf is your say so for example

Code:
 
passwd:     files winbind
shadow:     files winbind
group:      files winbind
hosts:      files dns winbind
bootparams: files
ethers:     files
protocols:  files winbind
services:   files winbind
netgroup:   files winbind
publickey:  files
automount:  files winbind


after you must start winbind service..
after than you can test to try view domain users list

Code:
wbinfo –u


and you can make a directory for domain users
Code:
mkdir /home/YOURDOMAIN


and pam authentication is must be setup
I m very clearly for details but my conf files il below

Code:
“ /etc/pam.d/login “
***************************************************
auth       required     pam_securetty.so
auth       required     pam_stack.so service=system-auth
auth       required     pam_nologin.so

account    required     pam_stack.so service=system-auth

password   required     pam_stack.so service=system-auth

session    required     pam_selinux.so close
session    required     pam_stack.so service=system-auth
session    required     pam_loginuid.so
session    optional     pam_console.so
session    required     pam_selinux.so open
 
“ /etc/pam.d//system-auth “
***************************************************

#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.

auth        required      /lib/security/pam_env.so
auth        sufficient    /lib/security/pam_winbind.so
auth        sufficient    /lib/security/pam_unix.so likeauth nullok use_first_pass
auth        required      /lib/security/pam_deny.so

account     required      /lib/security/pam_unix.so
account     sufficient    /lib/security/pam_succeed_if.so uid < 100 quiet
account     required      /lib/security/pam_permit.so

password    requisite     /lib/security/pam_cracklib.so retry=3
password    sufficient    /lib/security/pam_unix.so nullok use_authtok md5 shadow
password    required      /lib/security/pam_deny.so

session     required      /lib/security/pam_limits.so
session     required      /lib/security/pam_unix.so
session     required      /lib/security/pam_mkhomedir.so skel=/etc/skel umask=0022
***************************************************



So you can explore on google i m sure you can find a lot of details and documents..

Regards
Yucel Gemici @ygemici



Last edited by ygemici; 04-07-2010 at 10:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

useradd

I want to creat a 27 logins in solaris.Can anyone tell me how to write a script for that so that i create at a time for all 27 people. Thanks to guide me. (6 Replies)
Discussion started by: kkalyan
6 Replies

2. Solaris

useradd problem

:wall:i want to create a user in solaris whose password expires after every 30 minutes and he has to change his password after evry thirty minutes.How can we do that?:confused: thanx and regards, shekhar (17 Replies)
Discussion started by: shekhar_4_u
17 Replies

3. Solaris

useradd

if useradd command is deleted in solaris how do we add user (3 Replies)
Discussion started by: vivek_ng
3 Replies

4. Shell Programming and Scripting

Help with useradd script

Ok Im trying too make this shell script create users from my text file, I also want to type in a password for the new users. So thay can make a uniq one themself after first logon. #!/bin/sh # Sebastian schmidt clear echo "*************************************************************"... (3 Replies)
Discussion started by: chipmunken
3 Replies

5. UNIX for Advanced & Expert Users

useradd?

Hi Experts, when using useradd command, what are the necessary options/arguments to be included? Please advice. (4 Replies)
Discussion started by: etcpasswd
4 Replies

6. Shell Programming and Scripting

useradd

Gurus, I need to add a user to all the machines. I need a script to do this. I did one but it does not allow me to su to root within a ssh session i open. It exists saying su: Sorry. Please let me know how i can do it. I do not have the freedom of using sudo either. Regards (4 Replies)
Discussion started by: earlysame55
4 Replies

7. UNIX for Dummies Questions & Answers

useradd question

The man pages for useradd show the -k flag as a option, problem is I don't know what the description means. Could someone explain what "an alternative skel directory" is? Is skel an acronym? Thanks From the man page: -k, --skel skeldir Specify an alternative skel... (1 Reply)
Discussion started by: thumper
1 Replies

8. Solaris

useradd

Hi, I need to add a new user who will only be able to access one single folder on my Solaris 9 system. Can this be achieved by using just useradd or do i need to fiddle with auth_attr table? TIA, Selma (4 Replies)
Discussion started by: Selma
4 Replies

9. UNIX for Advanced & Expert Users

useradd

Hi. due to some needs i gave a user the premission to use useradd command with sudo. i want to know if there is a way to let him set the initial password, without giving him the premission to use passwd command as root (sudo). maybe a way to set a default password for all the new users that... (2 Replies)
Discussion started by: dorilevy
2 Replies

10. UNIX for Dummies Questions & Answers

useradd

I work on some hp ux 11.00 Servers. i have to add an user. i use the useradd command like follows: useradd -u 72022 -g 71095 -c " comment " -d /PACKAGE_NAME/home/username -s /usr/bin/sh username The command returns with error 3. The manpage means value number 3: Invalid argument supplied to an... (6 Replies)
Discussion started by: ortsvorsteher
6 Replies
Login or Register to Ask a Question