LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2013, 05:05 PM   #1
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Rep: Reputation: Disabled
Shell script to add password to new user and run automatically.


I am not worried about security because this is just a basic beginners script that I am running. Using the simple and traditional commands to create a user and add to a group, then prompt for a password. I would like to be able to echo the password into the script so that it does it automatically when the script is run. So.....

useradd -N -g (group) (user)
passwd (user)
#This is where I would like to try to echo the password directly into the script to run automatically#

Simplicity is important because Im a beginner. Thank you!
 
Old 03-02-2013, 05:16 PM   #2
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Perhaps chpasswd would be easier to use?
http://linux.die.net/man/8/chpasswd
 
Old 03-02-2013, 05:27 PM   #3
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
I tried to add chpasswd (user)(password) after the line passwd (user) and I got a cursor that when clicked said chpasswd: Line 1 empty. Remember, I am totally new and just learning syntax and how to use options correctly. I guess I thought that there would be some kind of echo command to append to or make the passwd command read.
 
Old 03-02-2013, 05:30 PM   #4
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
You use chpasswd instead of passwd so you would use something like:
Code:
chpasswd newuser:newpassword
instead of your passwd line.
 
Old 03-02-2013, 05:41 PM   #5
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
That is what I did and it didnt do anything, just gave me a solid cursor and when I hit any button I got the error

chpasswd: Line 1: missing new password

Then I hit it again and it said:

chpasswd: Line 2: missing new password

and so on....

Thanks for your help, Im still googling and reading everything I can find to try to find a solution.
 
Old 03-02-2013, 05:48 PM   #6
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Oh, sorry, you mean you want to run your script like:
Code:
./myscript username password
and have it create the user and change the password?
In that case (for the password change at least) you would do something like:
Code:
#!/bin/bash
chpasswd $1:$2
 
Old 03-02-2013, 06:26 PM   #7
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Well its a fully functioning script adding users and groups and directories and also giving permissions and all of that. The password part is just something Im trying to do so that when the script is run you dont have to type the new passwords for the users. I want it all automated. Im sorry if you dont understand what I am trying to do but I dont know how else to explain it with my limited Linux vocabulary. The chpasswd thing just isnt working when I run the script. Thank you for trying to help.
 
Old 03-02-2013, 06:28 PM   #8
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
How about posting the script you are trying to run rather than just explaining what you're not doing? That way others can help.
 
Old 03-02-2013, 06:40 PM   #9
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
I thought I had, atleast the only part that is involved in this question.

useradd -N -g Sales public #This is creating the user public and adding it to Sales group
passwd public #This is prompting the script to ask for new password
????? #Trying to find code that automatically echos or inserts password so script doesn't ask for it.


This is the best I can do since I am new on these forums and still dont know how to copy and paste my vi screen.
 
Old 03-02-2013, 06:58 PM   #10
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
So are sale sand public in the script? If so then just using chpasswd public:password ought to do it.
The use of $1 and $2 I mentioned are so that you call the script with, for example, the username and password so that you don't have to modify it every time you want to create a new user.
I am confused how chpasswd username:password is is failing.

Last edited by 273; 03-04-2013 at 03:12 AM.
 
Old 03-03-2013, 08:06 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
chpasswd should work, but here's an alternative (given you're not worried about security)
Code:
for USER in john alex dave bryan sarah
do
    useradd $USER
    echo password | passwd --stdin $USER
done
You could also look at http://linux.die.net/man/8/newusers, which is designed to bulk create users.
 
Old 03-04-2013, 06:33 AM   #12
nevr2l899
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you but I had to submit my assignment already. The only question I have is, is does this have to be done inside of a function? That is the only way it can read do/while and if/else statements right? I will take note of this and make a new script to try it out. Again, thank you for responding.
 
Old 03-04-2013, 06:45 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could put it in a fn, but its not a requirement.
 
  


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
To run a shell script automatically on time A.Hariharasudhan Linux - Server 1 10-08-2010 01:20 AM
Run shell script as root automatically elempoimen Linux - Software 8 06-20-2010 01:24 AM
how to run a shell script automatically? ceantuco Linux - Newbie 4 12-16-2008 10:16 AM
Run shell script automatically after Login. hnshashi Linux - Newbie 14 09-26-2008 04:43 AM
How to run a shell script evey day automatically at a particular time krevathi1912 Programming 3 09-22-2007 01:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:33 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