Internet & Networking

Password protecting a directory

It’s pretty simple. It can be done in 5 simple steps.

1: Create the directory area and set permissions to read the area

2: Create a file called .htaccess in the directory you want to protect
In that file, type in these lines:

AuthUserFile /your/directory/here/.htpasswd
AuthGroupFile /dev/null
AuthName “Secure Document”
AuthType Basic

require user username

For example, if you were going to protect the /usr4/test/private/ directory and you want the required name to be osuser, then your file would look like this:

AuthUserFile /usr4/test/private/.htpasswd
AuthGroupFile /dev/null
AuthName “Secure Document”
AuthType Basic

require user osuser

3: Create a file called .htpasswd in the same directory as .htaccess
You create this file by typing in the information below, at the system prompt, in the directory area to be protected:

htpasswd -c .htpasswd username

In the example above, the username is osuser so you would type:

htpasswd -c .htpasswd osuser

You will be prompted to enter the password you want. The .htpasswd file will be created in the current directory and will contain an encrypted version of the password.

4: Changing the password or username later
To later change the username, edit the .htaccess file and change the username.
If you want to later change the password, just retype the above line in step 3 and enter the new password at the prompt.

5: Don’t forget to change the permissions on the files.
After creating the password file (previous step) you will be sitting in the directory area which will be protected. Type chmod 755 .htpasswd and press Enter, and then type chmod 755 .htaccess and again press Enter. You have just made these two files readable.

Leave a Reply