[SOLVED] Shell script to change password for current user
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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!
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.