LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-02-2009, 08:19 AM   #16
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40

Quote:
Originally Posted by Disillusionist View Post
Sorry, I realised my mistake whilst you were composing your reply.
No problem.

Quote:
Originally Posted by Disillusionist View Post
You could use usermod -p $ENCPW $UN so long as you have a valid encrypted password but you're going to need to create that encrypted password somewhere.
Yeah, and this is where I'm at: Where can I create an encryptet password without having to create/modify an existing user? I essence, what I'd like is access to the same encryption function that "passwd" uses.
 
Old 01-02-2009, 11:49 PM   #17
sandeeprhce5
LQ Newbie
 
Registered: Jan 2009
Location: Bhiwani Haryana India
Distribution: RedHat and Linux
Posts: 20

Rep: Reputation: 1
Angry

Quote:
Originally Posted by rweaver View Post
Sure, mkpasswd... a script to do what the original poster asked from a csv file would look like this...

new-user.sh
Code:
#!/bin/bash
for i in `cat unpw.csv`; do
  UN=`echo $i | cut -f1 -d','`
  PW=`echo $i | cut -f2 -d','`
  ENCPW=`echo $PW | mkpasswd -s`
  echo useradd -p $ENCPW -m $UN
done
unpw.csv
Code:
user1,password1
user2,password2
user3,password3
You might want to add --hash=md5 or something similar to mkpasswd to get a better encryption scheme than the default.

It currently will echo what it will output to the system when you run it... to do the process for real remove the echo.

Good luck.
Very useless script its not working properly it damage my system passwd file please do not try at home
error
what do you mean by mkpasswd
 
Old 01-03-2009, 04:49 AM   #18
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by sandeeprhce5 View Post
Very useless script its not working properly it damage my system passwd file please do not try at home
error
what do you mean by mkpasswd
Did you run it with the echo in first?

The point of the echo statement was that you could do a dry run first to ensure it was going to do what you expected it to do.

What problems has this caused for your /etc/passwd /etc/shadow files? This should only have attempted to create new users not modify existing entries.
 
Old 01-03-2009, 06:37 AM   #19
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by kenneho View Post
Yeah, and this is where I'm at: Where can I create an encryptet password without having to create/modify an existing user? I essence, what I'd like is access to the same encryption function that "passwd" uses.
passwd uses crypt but with a random salt begining $1$

For example, create a user testuser2 with the password Testing
Then tail /etc/shadow
On my system I get:
Code:
testuser2:$1$5ObqRtny$PusK9ewRa6sKA7olrf7pC1:14246:0:99999:7:::
If I use crypt with the first part as my SALT:
Code:
#!/usr/bin/perl
use strict;
my $SALT='$1$5ObqRtny';
my $pass="Testing";
my $ENC=crypt($pass, $SALT);

print("Password : $pass\n");
print("Encrypted: $ENC\n");
I get:
Code:
Password : Testing
Encrypted: $1$5ObqRtny$PusK9ewRa6sKA7olrf7pC1
Therefore you can tell what SALT crypt used by taking the encrypted password prior to the third $ symbol

Last edited by Disillusionist; 01-03-2009 at 06:58 AM.
 
Old 01-03-2009, 10:03 AM   #20
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
OK, I think I've finally finished tweaking the code!

/usr/local/bin/new_user.pl
Code:
#!/usr/bin/perl
use strict;
use String::Random qw(random_string);
## String::Random can be found: http://search.cpan.org/~steve/String-Random-0.20/Random.pm

my ($USER, $pass)=@ARGV;
my $SALT=random_string("ssssssssss");

my $PASS_SALT=("\$1\$$SALT");
my $ENC=crypt($pass, $PASS_SALT);

system("useradd -p '$ENC' -m $USER");
/usr/local/bin/bulk_useradd.pl
Code:
#!/usr/bin/perl
use strict;

my ($list)=@ARGV;
my $line;

open(LIST, $list) or die "Couldn't open $list\n";
my @userlist=<LIST>;
close(LIST);

foreach $line (@userlist) {
   my ($USER,$PASS)=split(',',$line);
   system("perl /usr/local/bin/new_user.pl $USER $PASS");
}
Syntax bulk_user.pl {file}
EG:
bulk_user.pl /tmp/Users

Sample /tmp/Users:
Quote:
testuser1,Password
testuser2,Hello
 
Old 01-05-2009, 01:58 AM   #21
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
Quote:
Originally Posted by Disillusionist View Post
passwd uses crypt but with a random salt begining $1$

For example, create a user testuser2 with the password Testing
Then tail /etc/shadow
On my system I get:
Code:
testuser2:$1$5ObqRtny$PusK9ewRa6sKA7olrf7pC1:14246:0:99999:7:::
But AFAIK the "passwd" modifies an existing user. I don't want to modify or create a user at all, just create an encrypted password. Am I missing something here?
 
Old 01-05-2009, 02:40 AM   #22
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
To create an encrypted password:

Code:
#!/usr/bin/perl
##
## Script: encrypt.pl
use strict;
use String::Random qw(random_string);
## String::Random can be found: http://search.cpan.org/~steve/String-Random-0.20/Random.pm

my ($pass)=@ARGV;
my $SALT=("\$1\$" . random_string("ssssssss") );
##
## If you cant get String::Random then just use your own SALT eg:
##  my $SALT=("\$1\$GSx8s72d");

my $ENC=crypt($pass, $SALT);

print("Encrypted Pass: $ENC\n");
This expects the unencyrpted password to be passed as an argument:

encrypt.pl Password
 
Old 01-05-2009, 02:54 AM   #23
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
Quote:
Originally Posted by Disillusionist View Post
To create an encrypted password:

Code:
#!/usr/bin/perl
##
## Script: encrypt.pl
use strict;
use String::Random qw(random_string);
## String::Random can be found: http://search.cpan.org/~steve/String-Random-0.20/Random.pm

my ($pass)=@ARGV;
my $SALT=("\$1\$" . random_string("ssssssss") );
##
## If you cant get String::Random then just use your own SALT eg:
##  my $SALT=("\$1\$GSx8s72d");

my $ENC=crypt($pass, $SALT);

print("Encrypted Pass: $ENC\n");
This expects the unencyrpted password to be passed as an argument:

encrypt.pl Password
Thanks, but this is perl. I'd like a (bash) shell equivalent of this if possible. I mentioned this a few posts back, but I'm sure it's not easy to keep track of what everyone has written.
Anyways: If there is a (bash) shell equivalent for this, in which I can create an encrypted password that _may_ be added to /etc/shadow later on, please let me know. The reason for doing it this way is that for some users I define them in our administration program (Puppet), and distribute the encrypted password to the the servers in our network. So the encrypted password are added to a text file and distributed to the /etc/shadow file of our servers.
 
Old 01-05-2009, 08:13 AM   #24
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by sandeeprhce5 View Post
Very useless script its not working properly it damage my system passwd file please do not try at home
error
what do you mean by mkpasswd
As written the script doesn't touch your passwd or shadow files, it does nothing but echo information to the screen (should be in the format of "useradd -p yXI6rY4uwosLQ username"). If you remove the echo it will simply run the useradd command with whatever command line was echoed to the screen in the dry run.

Even if fed incorrect information (badly formatted csv) it can only pass that bad information to the useradd program which shouldn't do any damage to your passwd or shadow files. The useradd program has no destructive flags, so even if you try to make a user named various random flags instead of a name it would just make a non-usable user.

Based on the information at the bottom of your post it looks like your system doesn't have mkpasswd installed by default or it's not in your path. Which means, largely, that you would get the unencrypted password added to the password file, which still wouldn't allow login.

Last edited by rweaver; 01-05-2009 at 08:24 AM. Reason: extra information at bottom of post
 
Old 01-05-2009, 02:27 PM   #25
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by kenneho View Post
Thanks, but this is perl. I'd like a (bash) shell equivalent of this if possible. I mentioned this a few posts back, but I'm sure it's not easy to keep track of what everyone has written.
Anyways: If there is a (bash) shell equivalent for this, in which I can create an encrypted password that _may_ be added to /etc/shadow later on, please let me know. The reason for doing it this way is that for some users I define them in our administration program (Puppet), and distribute the encrypted password to the the servers in our network. So the encrypted password are added to a text file and distributed to the /etc/shadow file of our servers.
To the best of my knowledge (and numerous Google searches) there is no way to do this directly from bash.

mkpasswd uses crypt, however it only allows 2 character SALT's and therefore will not give the type of password that you are after.

I must admit that I don't see an issue with calling a perl script from bash (unless you don't have perl installed).

On my system I have moved the encrypt.pl file:
Code:
mv encrypt.pl /usr/local/bin/crypt
chmod 555 /usr/local/bin/crypt
Then I tested calling it from within bash, example:
Code:
#!/bin/bash
Password="Testing"
###
### Create encrypted password
###
l_encrypted_pass=$(crypt $Password)

echo "$l_encrypted_pass"
 
Old 01-05-2009, 02:48 PM   #26
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Disillusionist View Post
To the best of my knowledge (and numerous Google searches) there is no way to do this directly from bash.

mkpasswd uses crypt, however it only allows 2 character SALT's and therefore will not give the type of password that you are after.

I must admit that I don't see an issue with calling a perl script from bash (unless you don't have perl installed).
Code:
#!/bin/bash
for i in `cat unpw.csv`; do
  UN=`echo $i | cut -f1 -d','`
  PW=`echo $i | cut -f2 -d','`
  ENCPW=`echo $PW|mkpasswd -s --hash=md5`
  echo useradd -p $ENCPW -m $UN
done
Like I suggested in my original post, you probably want to use a stronger alg like md5. Just gotta add a --hash=alg line, see above.

Last edited by rweaver; 01-05-2009 at 02:52 PM.
 
Old 01-05-2009, 04:09 PM   #27
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
It just goes to show that the best of my knowledge is not always right
 
Old 01-05-2009, 04:48 PM   #28
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Disillusionist View Post
It just goes to show that the best of my knowledge is not always right
Pobody's Nerfect.
 
Old 01-06-2009, 01:55 AM   #29
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
[QUOTE=Disillusionist;3398100]To the best of my knowledge (and numerous Google searches) there is no way to do this directly from bash.
QUOTE]

Thanks, that's all I needed to know. I was hoping there would be some way of doing this directly from bash, but going for the perl method is not a problem.
 
Old 01-06-2009, 08:23 AM   #30
haariseshu
Member
 
Registered: Jan 2008
Location: Noida, India
Distribution: RHEL
Posts: 81

Original Poster
Rep: Reputation: 15
Thumbs up Thanks a lot for all my guys:

Dear Guys,
After my tour I just sit with my office and gone through this thread and lot of informations and knowledgeI I gained. And my heartly thanks for all shared their knowledge here.

And I hope the tail of this post will extend.

--
With Thanks & Regards,
Hari.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
samba - add user script - User account does not exist itzamecwp Linux - Server 2 01-18-2007 10:52 PM
Add bulk users at a same time mudasar Linux - Networking 1 11-20-2005 01:56 PM
add user script satinet Linux - General 2 10-21-2005 02:48 AM
What add user script you use for Samba 3.0.3-5? subaruwrx Linux - Networking 3 07-19-2004 11:19 AM
add user script? ezra143 Linux - Software 2 10-21-2003 11:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration