15

I read description of these command from a book,
passwd:

Changes the password for an existing user.

chpasswd:

Reads a file of login name and password pairs, and updates the passwords.

It seems these command doing same jobs. Is there a difference between them?

EDIT:

I want to learn when we use them which file(s) change. Do they change same file or different file? If they change different file(s), what are they?

1
  • 2
    /etc/password contains user account information. /etc/shadow contains encrypted passwords. I believe those are the only files that are relevant. You could change a password by manually editing those files, but it would be very awkward.
    – SauceCode
    Commented Feb 16, 2017 at 13:33

4 Answers 4

20

From man chpasswd:

'This command is intended to be used in a large system environment where many accounts are created at a single time.'

passwd is (in my experience) normally used interactively for a single user.

0
8

In a nutshell:

  • passwd checks if the STDIN (file descriptor 0) is attached to the terminal, using isatty(0). If not, passwd would bail out i.e. you can only work with passwd interactively

  • chpasswd, on the other hand, is designed to read (username and) password from STDIN, and is called the batch mode (creates/updates multiple user credentials at once). It reads password(s) (in clear text by default), and username(s) from STDIN, given in the format user_name:password, with newline separating the entries

7

See man pages for both of them:

NAME

    chpasswd - update passwords in batch mode

SYNOPSIS

    chpasswd [options]

DESCRIPTION

    The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users.  Each line is of the format:

       user_name:password


NAME

    passwd - change user password

SYNOPSIS

    passwd [options] [LOGIN]

DESCRIPTION

    The passwd command changes passwords for user accounts.  A normal user may only change the password for his/her own account, while the superuser may change the password for any account.  passwd also changes the account or associated password validity period.

1

I found that although

echo user:password | chpasswd

works for some OSes, some such as debian9,10 will show an error can not connect to /var/run/nscd/socket since it uses /etc/nsswitch.conf to determine where it stores the password.

On the other side, the passwd command seems does not need interactive shell,

(echo user; echo password) | passwd

works for me both in physical host and container, for various OSes (ubuntu xenial-bionic, debian9-10, centos75-76, coreos1855-2135)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .