
In Apache, directory listing is a default behavior that displays the contents of a directory if there is no default index file such as index.php or index.html. In a production environment, enabling directory browsing is not recommended since it may lead to information leakage and help attackers to determine how a website or web application is structured and increase the attack surface. In this guide, you'll test and disable Apache directory browsing on Ubuntu 20.04.
Before you begin, ensure you have the following:
SSH to your server and create a test directory in the root of your website.
Create two sub-directories in the test directory.
Add two files to the test directory.
Open a web browser and visit the URL below. Replace example.com with the domain name or IP address of your server.
If Apache directory listing is enabled, you should see a list of all files and sub-directories that you've created.
Open the /etc/apache2/apache2.conf file.
Find the content below.
Change the line:
to:
When finished, it should look like this.
By default, the Indexes option forces Apache to prepare and show a list of files if no default index file is detected in the /var/www directory, by prefixing the Indexes options with a hyphen, you've instructed Apache to disable this behavior.
Save and close the file. Then, restart the Apache webserver.
Try to access the URL of the test directory. Replace example.com with the domain name or the public IP address of your server.
This time, your access will be forbidden. Please note, this is a system-wide setting. If you need more control, you can disable directory browsing by editing the virtual hosts file.
If you have several websites hosted on the same Apache web server, you can disable directory listing by editing the virtual host configuration file of each website independently.
List the sites available on your Apache web server by running the command below.
The output below may be different depending on the websites you've hosted on your server.
Get the configuration filename of the website that you want to edit for instance 000-default.conf and open the file.
The content of your file will be somehow similar to the one shown below.
Edit the file by adding the <Directory /var/www/>...</Directory> options as shown below:
Save and close the file. Then, restart the Apache webserver.
You've successfully disabled directly browsing on that site. You can repeat the procedure if you'd like to disable directory listing for the rest of the websites.
If directory browsing is still enabled after performing these steps, look for additional sections in your control files that may override your settings. For example, if you've set Options -Index in the <Directory /var/www/> section, look for conflicting sections such as <Directory /var/www/html/>.
In this guide, you've tested and disabled Apache directory listing on Ubuntu 20.04 server. This is a major step to safeguard your webserver.
0 Comments
Be the first to comment and share your perspective with the community.