How can I change root password with one line command on NetBSD? On FreeBSD, it's something like this
echo "password" | pw mod user root -h 0 ;
How can I change root password with one line command on NetBSD? On FreeBSD, it's something like this
echo "password" | pw mod user root -h 0 ;
NetBSD does not support the pw
command in its default install. You can:
pw
command for NetBSD and install it on your systemsNote that depending on your script you should probably be using -H
(and supplying an appropriately-encrypted password) instead of -h
.
Passing unencrypted passwords around the system (especially if you're doing something like echo "password"
) is a Bad Idea as it can result in exposing your password to any logged in user (or service).
Use usermod
with -p
switch. It's included with base system, no need to build pw
.
We encountered a similar use-case: automatically creating demo users with awful passswords, bypassing all password policies. Without further ado, here's a nasty-but-works hack based on the mailing list suggestion:
# example: set_password_insecure sybil magic
# $1: username
# $2: password
set_password_insecure() {
if [ -z "$1" ]; then
echo 'Missing username' >&2
return 1
fi
( PASSWORD_HASH="$(/usr/bin/pwhash "$2" | /usr/bin/sed 's@[\\$/]@\\&@g')"
/usr/bin/env EDITOR="in_place_sed() { /usr/bin/sed \"\$1\" \"\$2\" > \"\$2.bak.\$\$\" && /bin/mv \"\$2.bak.\$\$\" \"\$2\" ;}; in_place_sed 's/^$1:[^:]*:/$1:$PASSWORD_HASH:/' " \
/usr/sbin/vipw
)
}
( USERNAME='mallory' PASSWORD='sex'; \
PASSWORD_HASH="$(/usr/bin/pwhash "$PASSWORD" | /usr/bin/sed 's@[\\$/]@\\&@g')"
/usr/bin/env EDITOR="in_place_sed() { /usr/bin/sed \"\$1\" \"\$2\" > \"\$2.bak.\$\$\" && /bin/mv \"\$2.bak.\$\$\" \"\$2\" ;}; in_place_sed 's/^$USERNAME:[^:]*:/$USERNAME:$PASSWORD_HASH:/' " \
/usr/sbin/vipw )
sh
and bash
(with shells/bash
installed), and probably zsh
too (untested).sudo
(with security/sudo
installed) or su - root -c
before /usr/bin/env
.sed
/mv
hack is due to EDITOR
limitations + NetBSD's sed
+ requirement of no other dependencies / unnecessary temporaries / environment pollution.