1and1 Help Centre Categories

print article

Manually Password Protect Files/Folders for Linux

For Linux packages with SSH access

Follow the steps below to manually password protect your website files or folders to keep them from being accessed without a password.

This functionality is only available with packages that have SSH access. To obtain SSH access, you will need to upgrade your package.

While it is possible to restrict access to the contents of a folder by using the protected directories section of the 1&1 Control Panel, this can also be done manually.

The advantages of manually configuring restrictions include:

  • You can restrict not only contents of a folder, but individual files as well.
  • You can restrict access to multiple files using a separate username/password for each.
  • The configuration already present in a .htaccess file will not be completely overwritten by changes via the Protected Directories section of the 1&1 Control Panel.

Step 1
Make sure to change directories to the folder (or create a new one) where you want to password protect files or folders. This could be the root of your webspace or a subfolder. In this example, will we be password protecting a subfolder named 1and1help/protectedDir/ (the folder 1and1help/ is used to hold the files for 1and1help.co.uk).
Step 2
We will now create a username and password that is required to access the folder named "protectedDir/".
Type htpasswd -c .htpasswd john (but replace "john" with a username of your choice) and press ENTER. The -c option specifies to create a new .htpasswd file that will contain a list of usernames and passwords.
Enter a password and press ENTER. As you type, nothing will be displayed on the screen so that onlookers can not see your password or guess at the length. Repeat the password and press ENTER. Create Username and Password
Create Username and Password
Step 3
Now that we have created a file containing the username and encrypted password, we must edit the .htaccess file to specify what folder/files should be password protected, and the location of the file that contains the username and password.
  • Type pwd and press ENTER to print working directory (print out the full folder structure to the current location).
  • Highlight the folder structure by clicking and dragging the mouse over the text to copy it.
Protected Folder
Protected Folder
Step 4
Type nano .htaccess and press Enter. This will open up the Nano text editor and create the .htaccess file or edit an existing one. Nano Text Editor
Nano Text Editor
Step 5
Right-click the window with your mouse to paste the folder structure into the Nano text editor.
  • Add a forward slash(/) to the end of the line followed by .htpasswd
  • Move the cursor to the beginning of the line and type AuthUserFile and then a SPACE.
  • Move down to a new line and type AuthType Basic
  • Start another new line and type AuthName "Protected Folder"
Reference the image below against your own text in the Nano editor. Yours should be the same aside from the folder structure. Edit Structure
Edit Structure

If everything looks correct, press the Ctrl and x at the same time to exit and save the file.
Step 6
Open up your browser and navigate to the password protected URL such as http://www.1and1help.co.uk/protectedDir/ and you should be prompted for the username and password you chose earlier before you can view anything in the folder.
Step 7
Scroll to the bottom of the page for other examples of how to create more users and password protect specific files.

Creating additional usernames and passwords
Make sure you are in the same directory as the .htpasswd file (1and1help/protectedDir/) and type htpasswd .htpasswd oliver (replacing "oliver" with a username of your choice). Enter the password twice and press ENTER, then repeat the password and press ENTER.

Restricting access to specific files
View the example below for more help on specifying individual files that should be password protected as well as specifying which users that are allowed to access a file.
AuthUserFile /homepages/30/d123456789/htdocs/1and1help/protectedDir/.htpasswd
AuthType Basic
AuthName "Protected Content"

<Files "protectedPage.html">
Require user john
</Files>

<Files "protectedPage2.html">
Require user oliver
</Files>

<Files "protectedImage.jpg">
Require user john
Require user becky
</Files>