This guide explains how to create additional web users and protect selected directories with a username and password.
This can be useful if you want to give another person HTTP access to a specific folder without sharing your main service login details.
Before you begin, log in to your service via SSH.
Step 1 – Create a Password File
Create a password file and add the first user:
htpasswd -c ~/.config/passfile username
Replace username with the username you want to create.
You will be asked to enter the password twice. The password will not be visible while typing. This is normal.
Step 2 – Create the .htaccess File
Create a .htaccess file in the directory you want to protect.
For example, to protect your downloads directory, use:
nano ~/downloads/.htaccess
Add the following content:
AuthName 'Private' AuthType Basic AuthBasicProvider file AuthUserFile '/path/to/password/file' Require valid-user
In the AuthUserFile line, provide the absolute path to the password file created in the previous step.
For example:
AuthUserFile '/home5/myuser/.config/passfile'
Save the file by pressing F3, press Enter to confirm and then press F2 to exit.
The created user should now have access to the directory where the .htaccess file was created.
Adding More Users
If you want to add more users with access to the same directory, use:
htpasswd ~/.config/passfile username
Replace username with the new username you want to add.
When adding additional users to an existing password file, do not use the -c option.
The -c option creates a new password file and will remove all users that were previously added to that file.
Protecting a Different Directory
You can give different users access to different folders by creating a separate password file and a separate .htaccess file.
For example, to give another user access only to:
~/downloads/share
create a new password file:
htpasswd -c ~/.config/passfile2 myanotheruser
Then create a new .htaccess file inside the protected directory:
nano ~/downloads/share/.htaccess
Add the following content:
AuthName 'Private' AuthType Basic AuthBasicProvider file AuthUserFile '/home5/myuser/.config/passfile2' Require valid-user
After saving the file, myanotheruser will have access to the ~/downloads/share directory.
Removing a User
To remove a user from a password file, use the -D option:
htpasswd -D /path/to/pass/file username
For example, to remove user2 from passfile2, use:
htpasswd -D ~/.config/passfile2 user2
Notes
- Create the .htaccess file inside the directory you want to protect.
- Use the absolute path in the AuthUserFile line.
- Use htpasswd -c only when creating a new password file.
- Use htpasswd without -c when adding more users to an existing password file.
- Use a separate password file if you want different users to access different directories.