How to fix htpasswd cannot open file htpasswd for read/write access


You are trying to add/change a password in a htpasswd file using a command like

htpasswd auth.htpasswd myuser

but you see this error message:

htpasswd: cannot open file htpasswd for read/write access


htpasswd is trying to tell you that it got Permission denied when accessing the htpasswd file and hence can’t read it or it can’t write the file (auth.htpasswd in this example).

There are multiple ways to fix this:

  1. Run htpasswd using sudo, e.g.
    sudo htpasswd auth.htpasswd myuser
  2. Fix the permissions of the file, e.g. using
    sudo chown $USER auth.htpasswd
    chmod o+rw auth.htpasswd

    Note that this permanently modifies the permissions for the htpasswd file and hence might create additional security risks!

  3. Use a different htpasswd file to which you have access. You need to change the configuration on your webserver etc (i.e. the software that is using the htpasswd file). This option isn’t always available – e.g. if you can’t change the webserver’s configuration, you just have the option of asking the system administrator nicely.