LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-12-2010, 07:42 AM   #1
treznik
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Rep: Reputation: 0
Shell script to change password for current user


Hi guys,

I'm building a bash script where I need to change the current user's password, so it's not a script that's being run by root only, but by any user alone.

After searching a while on the net I found two main options:

Code:
echo -e "password" | (passwd --stdin $username)
Code:
echo "user:password" | chpasswd
Unfortunately the first doesn't work for me because I'm on ubuntu and passwd does not have the --stdin option (apparently) and the second only works when I'm logged in as root; I doesn't seem to have the required privileges to change a user's password when logged in with it.

Now I wouldn't necessarily have a problem with prompting the password from passwd directly, but I would need a way to catch that password because I'm using it in other parts of the script as well.

Any ideas? Thanks!

PS: This is my first post here.
 
Old 09-12-2010, 11:02 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by treznik View Post
Hi guys,

I'm building a bash script where I need to change the current user's password, so it's not a script that's being run by root only, but by any user alone.

After searching a while on the net I found two main options:

Code:
echo -e "password" | (passwd --stdin $username)
Code:
echo "user:password" | chpasswd
Unfortunately the first doesn't work for me because I'm on ubuntu and passwd does not have the --stdin option (apparently) and the second only works when I'm logged in as root; I doesn't seem to have the required privileges to change a user's password when logged in with it.

Now I wouldn't necessarily have a problem with prompting the password from passwd directly, but I would need a way to catch that password because I'm using it in other parts of the script as well.

Any ideas? Thanks!
PS: This is my first post here.
Welcome. And it's a good idea to search the forums here first for such things, as they've probably been asked/answered before. This thread:

http://www.linuxquestions.org/questi...a-user-830507/

has a script I wrote to do command-line password changing. It may work for you.
 
Old 09-12-2010, 12:30 PM   #3
treznik
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for answering TB0ne.

As a matter of fact I actually saw a few topics (including from this forum) before asking this question. The code you posted in your liked thread is for adding, I have that. What I want is to edit an already existent user's password.

And I even mentioned that topic's solution in my question, I can't use "passwd --stdin" because my version of linux/passwd does not support --stdin.

So I'm back at square one.
 
Old 09-12-2010, 01:54 PM   #4
treznik
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Since chpasswd requires root access, just passwd is left from what I know.

So I run passwd normally in my script, and the user is prompted normally from it, there are two scenarios that would seem logical:

- If there was a way to catch the prompted value, something like "the last user input" or so...
- If there was a way to catch the prompting itself and fetch it a previously inputted password (from a script variable), without the user actually seeing the prompt.

Is any of this possible? Or something else? Thanks!
 
Old 09-12-2010, 02:37 PM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You can try the following hack. Set the suid bit on chpasswd:
Code:
chmod 4755 /usr/sbin/chpasswd
so it can be run by a regular user (just as the normal passwd) and use your 2nd echo command.
FYI this worked on a Suse box, but it didn't on Slackware, so I don't know what happens with Ubuntu.
 
1 members found this post helpful.
Old 09-12-2010, 03:02 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by treznik View Post
Thanks for answering TB0ne.

As a matter of fact I actually saw a few topics (including from this forum) before asking this question. The code you posted in your liked thread is for adding, I have that. What I want is to edit an already existent user's password.

And I even mentioned that topic's solution in my question, I can't use "passwd --stdin" because my version of linux/passwd does not support --stdin.

So I'm back at square one.
How do you figure that??

Just slightly modify the script. Since my script takes the password variable from command line, and uses the standard "useradd" command to set/modify the password at creation time, just change the command to be "usermod" instead, and set it with the right parameters, which you can find from the man page on usermod. Not alot to it.

Last edited by TB0ne; 09-12-2010 at 03:04 PM.
 
1 members found this post helpful.
Old 09-12-2010, 04:06 PM   #7
treznik
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hmm, yes I haven't thought about usermod.

I tried it and however I get this error: "usermod: cannot lock /etc/passwd; try again later.".

Keep in mind that I want this script to be run by the user itself and not by the root user.

@bathory: If this doesn't work I'll try your little hack, thanks.
 
Old 09-12-2010, 04:17 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by treznik View Post
Hmm, yes I haven't thought about usermod.

I tried it and however I get this error: "usermod: cannot lock /etc/passwd; try again later.".

Keep in mind that I want this script to be run by the user itself and not by the root user.

@bathory: If this doesn't work I'll try your little hack, thanks.
Do both...the usermod command will have to be set that way to work from a 'regular' user account too.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
shell or perl script to change ftp user password vivek rai Linux - Newbie 4 03-15-2009 01:09 PM
Shell script to change password kamal_aitin Linux - General 6 07-25-2007 12:09 AM
Can my shell script change the password of user ? prabhatsoni Linux - Software 1 05-27-2006 02:06 AM
how to assign password for a user in shell script mtest Programming 10 10-29-2003 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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