Using the Passwd Command from Within a Shell Script

Using the passwd command from within a shell script

from "man 1 passwd":

   --stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.

So in your case

adduser "$1"
echo "$2" | passwd "$1" --stdin

[Update] a few issues were brought up in the comments:

Your passwd command may not have a --stdin option: use the chpasswd
utility instead, as suggested by ashawley.

If you use a shell other than bash, "echo" might not be a builtin command,
and the shell will call /bin/echo. This is insecure because the password
will show up in the process table and can be seen with tools like ps.

In this case, you should use another scripting language. Here is an example in Perl:

#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe

passwd command in shell script

Try expect(1).

How to provide password to a command that prompts for one in bash?

Take a look at autoexpect (decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.

How to get a password from a shell script without echoing

Here is another way to do it:

#!/bin/bash
# Read Password
echo -n Password:
read -s password
echo
# Run Command
echo $password

The read -s will turn off echo for you. Just replace the echo on the last line with the command you want to run.

how to pass a user password ,stored in config file to passwd command

Use chpasswd instead of passwd to set passwords in batch mode.

Example:

sudo chpasswd <<<"$username:$password"

Or, it you're using other shell (not bash of version >=3.0):

echo "$username:$password" | sudo chpasswd

How do I write a password using a bash script?

Assuming there's still no "useradd" on Pi, I'd suggest you start looking into adduser's command line parameters?

adduser <username> -p <password> ...

Useradd script in linux without using passwd

If you prefer to pipe the user's password from STDIN, use chpasswd utility which is quick and simple.
as suggested by @Ardit.

This script should work for your purpose, assuming you meet the following conditions-

  • You are interacting as the root user
  • You have an existing group created for the purpose of your new user
#!/bin/bash

echo "Name:"
read name

echo "Password:"
read password

echo "Group:" # group must exist
read group

# add new user, set group, create new home directory
useradd -G $group -m $name

# update new user password by piping from STDIN
echo ""$name":"$password"" | chpasswd

# change the default user shell to bash
chsh -s /bin/bash $name
  1. First we execute useradd command to create the new user and assign it to an existing group.
  2. Then we pipe the name and password into chpasswd. If you're wondering why wrap those variable expansions with double quotes, check this answer out .
  3. Finally chsh utility is used to update the user shell.

Why not execute everything in a single statement?

I prefer subdividing a problem into smaller tasks for easier understanding.



Related Topics



Leave a reply



Submit