Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 ;
share|improve this question
This could be the way of doing – nwildner Jun 14 '13 at 14:15

3 Answers 3

NetBSD does not support the pw command in its default install. You can:

  1. Compile the pw command for NetBSD and install it on your systems
  2. Write a utility that performs the equivalent functionality
  3. Update the password by manipulating the shadow file directly

Note 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).

share|improve this answer

Use usermod with -p switch. It's included with base system, no need to build pw.

share|improve this answer

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:

Bourn shell function

# example: set_password_insecure sybil magic
# $1: username
# $2: password
set_password_insecure() {
  if [ -z "$1" ]; then
    echo 'Missing username' >&2
    return 1
  ( 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:/' " \

Bourn shell command

( 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 )
  1. Works on both sh and bash (with shells/bash installed), and probably zsh too (untested).
  2. Assume running as root, if not add sudo (with security/sudo installed) or su - root -c before /usr/bin/env.
  3. sed/mv hack is due to EDITOR limitations + NetBSD's sed + requirement of no other dependencies / unnecessary temporaries / environment pollution.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.