Securing Your /tmp Partition with Cpanel/WHM Published: Nov 07, 2003
  • Rating

    5/5

Apache's .htaccess is a great way to take control of your website by password protecting directories, creating custom error pages and controling user access.

Introduction

In this tutorial you will find out about the .htaccess file and the power it has to improve your website. Although .htaccess is only a file, it can change settings on the servers and allow you to do many different things, the most popular being able to have your own custom 404 error pages. .htaccess isn't difficult to use and is really just made up of a few simple instructions in a text file.

Will My Host Support It?

This is probably the hardest question to give a simple answer to. Many hosts support .htaccess but don't actually publicise it and many other hosts have the capability but do not allow their users to have a .htaccess file. As a general rule, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess, although your host may not allow you to use it.

A good sign of whether your host allows .htaccess files is if they support password protection of folders. To do this they will need to offer .htaccess (although in a few cases they will offer password protection but not let you use .htaccess). The best thing to do if you are unsure is to either upload your own .htaccess file and see if it works or e-mail your web host and ask them.

What Can I Do?

You may be wondering what .htaccess can do, or you may have read about some of its uses but don't realise how many things you can actually do with it.

There is a huge range of things .htaccess can do including: password protecting folders, redirecting users automatically, custom error pages, changing your file extensions, banning users with certian IP addresses, only allowing users with certain IP addresses, stopping directory listings and using a different file as the index file.

Creating A .htaccess File

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems (e.g. Windows 3.1). With most operating systems, though, all you need to do is to save the file by entering the name as:

".htaccess"

(including the quotes). If this doesn't work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program.

Warning

Before beginning using .htaccess, I should give you one warning. Although using .htaccess on your server is extremely unlikely to cause you any problems (if something is wrong it simply won't work), you should be wary if you are using the Microsoft FrontPage Extensions. The FrontPage extensions use the .htaccess file so you should not really edit it to add your own information. If you do want to (this is not recommended, but possible) you should download the .htaccess file from your server first (if it exists) and then add your code to the beginning.

Custom Error Pages

The first use of the .htaccess file which I will cover is custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root directory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

Part 2

In part 2 I will show you how to use some of the other .htaccess functions to improve your website.
[pagebreak title='.htaccess Commands']
Introduction

In the last part I introduced you to .htaccess and some of its useful features. In this part I will show you how to use the .htaccess file to implement some of these.

Stop A Directory Index From Being Shown

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

To prevent against this (without creating lots of new 'index' files, you can enter a command into your .htaccess file to stop the directory list from being shown:

Options -Indexes

Deny/Allow Certian IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.

Alternative Index Files

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to 'index' files though. Using .htaccess you can set foofoo.blah to be your index file if you want to!

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists, if none of them exisit it will display a directory listing (unless, of course, you have turned this off).

DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm

Redirection

One of the most useful functions of the .htaccess file is to redirect requests to different files, either on the same server, or on a completely different web site. It can be extremely useful if you change the name of one of your files but allow users to still find it. Another use (which I find very useful) is to redirect to a longer URL, for example in my newsletters I can use a very short URL for my affiliate links. The following can be done to redirect a specific file:

Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz

In this above example, a file in the root directory called oldfile.html would be entered as:

/oldfile.html

and a file in the old subdirectory would be entered as:

/old/oldfile.html

You can also redirect whole directoires of your site using the .htaccess file, for example if you had a directory called olddirectory on your site and you had set up the same files on a new site at: http://www.newsite.com/newdirectory/ you could redirect all the files in that directory without having to specify each one:

Redirect /olddirectory http://www.newsite.com/newdirectory

Then, any request to your site below /olddirectory will bee redirected to the new site, with the extra information in the URL added on, for example if someone typed in:

http://www.youroldsite.com/olddirecotry/oldfiles/images/image.gif

They would be redirected to:

http://www.newsite.com/newdirectory/oldfiles/images/image.gif

This can prove to be extremely powerful if used correctly.

Part 3

In part 3 I will cover a few other uses of the .htaccess file including password protection.
[pagebreak title='Password Protection']
Introduction

Although there are many uses of the .htaccess file, by far the most popular, and probably most useful, is being able to relaibly password protect directories on websites. Although JavaScript etc. can also be used to do this, only .htaccess has total security (as someone must know the password to get into the directory, there are no 'back doors')

The .htaccess File

Adding password protection to a directory using .htaccess takes two stages. The first part is to add the appropriate lines to your .htaccess file in the directory you would like to protect. Everything below this directory will be password protected:

AuthName "Section Name"
AuthType Basic
AuthUserFile /full/path/to/.htpasswd
Require valid-user

There are a few parts of this which you will need to change for your site. You should replace "Section Name" with the name of the part of the site you are protecting e.g. "Members Area".

The /full/parth/to/.htpasswd should be changed to reflect the full server path to the .htpasswd file (more on this later). If you do not know what the full path to your webspace is, contact your system administrator for details.

The .htpasswd File

Password protecting a directory takes a little more work than any of the other .htaccess functions because you must also create a file to contain the usernames and passwords which are allowed to access the site. These should be placed in a file which (by default) should be called .htpasswd. Like the .htaccess file, this is a file with no name and an 8 letter extension. This can be placed anywhere within you website (as the passwords are encrypted) but it is advisable to store it outside the web root so that it is impossible to access it from the web.

Entering Usernames And Passwords

Once you have created your .htpasswd file (you can do this in a standard text editor) you must enter the usernames and passwords to access the site. They should be entered as follows:

username:password

where the password is the encrypted format of the password. To encrypt the password you will either need to use one of the premade scripts available on the web or write your own. There is a good username/password service at the KxS site which will allow you to enter the user name and password and will output it in the correct format.

For multiple users, just add extra lines to your .htpasswd file in the same format as the first. There are even scripts available for free which will manage the .htpasswd file and will allow automatic adding/removing of users etc.

Accessing The Site

When you try to access a site which has been protected by .htaccess your browser will pop up a standard username/password dialog box. If you don't like this, there are certain scripts available which allow you to embed a username/password box in a website to do the authentication. You can also send the username and password (unencrypted) in the URL as follows:

http://username:password@www.website.com/directory/

Summary

.htaccess is one of the most useful files a webmaster can use. There are a wide variety of different uses for it which can save time and increase security on your website.

Thanks to freewebmasterhelp.com for providing this article.

 

 

  • Rating

    5/5

Related Articles

Comments (39)

  • Gravatar - Thomas Dawson
    Thomas Dawson 13:19, November 12, 2003
    mke2fs command is not recognized in my ssh. What do I do?
  • Gravatar - Ramprage
    Ramprage 15:22, November 14, 2003
    Use /sbin/mke2fs instead
  • Gravatar - kel
    kel 22:58, November 15, 2003
    How to increase the /tmp space?

    I got this error:

    Warning: Unknown(): write failed: No space left on device (28) in Unknown on line 0

    Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

    Please help as soon.

    thanks
  • Gravatar - Don Holeman
    Don Holeman 22:18, November 16, 2003
    I just followed these directions, pretty much flawlessly. Got one warning about it not being a block special device when I went to create the partition.

    Thanks
  • Gravatar - simoa
    simoa 02:10, November 17, 2003
    I run a chkrootkit and found this after I secured the /tmp partition
    Checking `aliens'...
    /dev/tmpMnt

    is this normal or I have to worry about?? PLease reply as soon thanks
  • Gravatar - Ramprage
    Ramprage 14:35, November 18, 2003
    The chkrootkit aliens found message is normal when creating a tmp from a file, no worries.
  • Gravatar - mazen
    mazen 23:42, January 6, 2004
    I make the step by step
    when I type mount command I get this message

    root@server [/]# mount -o loop,noexec,nosuid,rw /dev/tmpMnt /tmp
    mount: Could not find any loop device, and, according to /proc/devices,
    this kernel does not know about the loop device.
    (If so, then recompile or `insmod loop.o'.)
  • Gravatar - cruz
    cruz 20:37, January 9, 2004
    3 things.
    Does this break cpanel in its normal use and in updates?
    If I want to revert back to the way it was before doing this modification, how is it done?
    Do I have to reboot the server once this I implement the changes?
  • Gravatar - Mo
    Mo 12:42, January 16, 2004
    use: cp -pR /tmp /tmp_backup
    instead of : cp -R /tmp /tmp_backup

    add -p to preserve file permitions and ownership.
  • Gravatar - Shanlar
    Shanlar 09:50, April 3, 2004
    ok i made my tmp to big, how do i reverse this process? tmp keeps saying device is busy and wont umount...
  • Gravatar - Gary
    Gary 12:15, May 16, 2004
    remember though, this wont save you for losers who run scripts by perl php etc....those commands can still work in /tmp
  • Gravatar - Rona
    Rona 11:32, May 22, 2004
    Can anybody tell me now how to unsecure /tmp because when i install DigiChat it's give me this error
    root@server [/home]# sh ./Install_DigiChat.bin -i console /home
    Preparing to install...
    Extracting the JRE from the installer archive...
    Unpacking the JRE...
    Extracting the installation resources from the installer archive...
    Configuring the installer for this system's environment...

    Launching installer...

    ./Install_DigiChat.bin: /tmp/install.dir.9155/Linux/resource/jre/bin/java: /bin/sh: bad interpreter: Permission denied


    Hope to get help ... :)
    Regards
  • Gravatar - ca
    ca 18:14, May 23, 2004
    @Shanlar
    I suppose you should take it out of fstab, do a server reboot and create a tmp directory again.
  • Gravatar - Michael Curtis
    Michael Curtis 16:39, May 24, 2004
    Instructions worked perfect for me other than that one warning about a block device.
    One tip, any path which exists and is owned by 'nobody'... create the directory under /tmp and make a symlink... this can be used to make any path noexec, anywhere on the server... just make sure /tmp has enough space ;)
  • Gravatar - C0NIk
    C0NIk 20:32, June 9, 2004
    well there is a better way for secure the tmp

    root@server [/]# cd scripts ; ./securetmp

    thats all and it well secure the /var/tmp as well

    bye
  • Gravatar - GIGI
    GIGI 04:38, June 17, 2004
    look this<br />
    ------------<br />
    root@localhost [~]# sh ./Install_DigiChat.bin -i console<br />
    Preparing to install...<br />
    tail: `-1' option is obsolete; use `-n 1'<br />
    Try `tail --help' for more information.<br />
    ./Install_DigiChat.bin: line 329: [: `)' expected, found -z<br />
    WARNING! The amount of /tmp disk space required and/or available<br />
    could not be determined. The installation will be attempted anyway.<br />
    Extracting the JRE from the installer archive...<br />
    Unpacking the JRE...<br />
    Extracting the installation resources from the installer archive...<br />
    Configuring the installer for this system's environment...<br />
    <br />
    Launching installer...<br />
    <br />
    ./Install_DigiChat.bin: /tmp/install.dir.3158/Linux/resource/jre/bin/java: /bin/sh: bad interpreter: Permission denied<br />
    -------<br />
    pliz help me....
  • Gravatar - Robert Greenwood
    Robert Greenwood 12:14, July 9, 2004
    You will need to increase the size of the mount
  • Gravatar - Foow
    Foow 18:15, August 23, 2004
    can you tell me why you set the /tmp partition to ext2 and not ext3?
  • Gravatar - Mark
    Mark 10:18, October 21, 2004
    i already run /scripts/securetmp from cpanel, but it seem that /tmp still not secured, can i use this tutorial while i already run /scripts/securetmp?
  • Gravatar - JLChafardet
    JLChafardet 08:10, November 3, 2004
    have any one tried this over Plesk Based webservers?<br />
    <br />
    if yes, please let me know if there is any change.<br />
    <br />
    regards,<br />
    <br />
    JLChafardet
  • Gravatar - Sotek
    Sotek 15:58, November 10, 2004
    change this:<br />
    <br />
    code:<br />
    chmod 0777 /tmp<br />
    <br />
    and put this:<br />
    <br />
    chmod 1777 /tmp<br />
    <br />
    beacuse you need to set the bit who lets everyone write on /tmp
  • Gravatar - kaveh
    kaveh 16:29, December 17, 2004
    hi<br />
    how can i resize the partition ? <br />
    97% full !!!!
  • Gravatar - Big AL
    Big AL 03:11, February 25, 2005
    you can't resize a partition, just move things to /home/tmp and link /tmp to /home/tmp via ln -s /home/tmp /tmp
  • Gravatar - Mateus
    Mateus 20:45, March 7, 2005
    Hi, thanks for this tuto..<br />
    But, i already have this partition <br />
    LABEL=/tmp /tmp ext3 defaults 1 2<br />
    <br />
    my server is in theplanet and is the default mount for them.. how can i modify this ? thank you!
  • Gravatar - Lechoad
    Lechoad 07:11, April 19, 2005
    how do you undo this?
  • Gravatar - netkinetics
    netkinetics 20:19, July 21, 2005
    Change defaults to say noexec,nosuid if you already have a seperate /tmp partition.<br />
    <br />
    Then umount / mount it again. <br />
    <br />
    Also do the same with the /dev/shm mount.<br />
    <br />
    Don't forget to secure /usr/local/apache/proxy, its 777 and owned by nobdoy after every apache build on cpanel servers. Change it to 0400 and owned by root.root , or safely remove it. If you use mod_proxy you should setup another loop device like in this tutorial and mount it to /usr/local/apache/proxy to run your proxy safely. <br />
    <br />
  • Gravatar - rayan
    rayan 12:14, July 25, 2005
    hi<br />
    ist same steps for directadmin control panel?<br />
  • Gravatar - Steve
    Steve 18:22, July 26, 2005
    Yeah it should be the same Rayan, it's not control panel reliant.<br />
  • Gravatar - David
    David 21:55, July 31, 2005
    Can i add the "noexec" just by changing the fstab?
  • Gravatar - complex
    complex 05:02, December 25, 2005
    Just in case anyone wants to do this with FreeBSD, see this page (example 16-5 or 16-7):<br />
    <br />
    http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/disks-virtual.html
  • Gravatar - Kapil
    Kapil 20:23, April 1, 2006
    If you have cpanel for freebsd, then just run the script /tmp/securetmp<br />
    <br />
  • Gravatar - Mateus
    Mateus 05:18, May 16, 2006
    Securetmp from cpanel does not work well, it will not protect you /tmp dir, and, this won't protect your tmp from executing Perl Scripts!.. :/
  • Gravatar - M.S.
    M.S. 03:54, June 6, 2006
    Would you whiners stop bitching and pick up a manual? This guy was kind enough to offer you a solution. So STFU. As for the perl/php errors, just edit your php.ini and make sure safe mode is on, sql safe mode is on, and make sure that url_fopen and file uploads are off. you'll be good to go.
  • Gravatar - xkasi
    xkasi 14:27, June 28, 2006
    To increase the /tmp size just redo the tutorial with a bigger count and an different filename.<br />
    e.g: <br />
    dd if=/dev/zero of=tmpMnt2 bs=1024 count=2000000<br />
    <br />
    Greetz
  • Gravatar - Vijay
    Vijay 13:13, October 13, 2006
    Hi,<br />
    <br />
    /scripts/securetmp<br />
    Would you like to secure /tmp & /var/tmp at boot time? (y/n) y<br />
    Would you like to secure /tmp & /var/tmp now? (y/n) y<br />
    Securing /tmp & /var/tmp<br />
    /tmp is already secure<br />
    /var/tmp is already secure<br />
    Process Complete.<br />
    <br />
    But fstab does not show noexec,<br />
    # cat /etc/fstab<br />
    # This file is edited by fstab-sync - see 'man fstab-sync' for details<br />
    LABEL=/tmp /tmp ext3 defaults 1 2<br />
    <br />
    I request others to verify whether following method is correct for CPanel 10.8. ?<br />
    <br />
    1) Because cpanel installed with /tmp saperate partition. - we do not need to change/recreate partition & copy.<br />
    2) modify /etc/fstab to following.<br />
    LABEL=/tmp /tmp ext3 loop,noexec,nosuid,rw 0 0<br />
    3) reboot server.<br />
  • Gravatar - steve
    steve 23:40, January 4, 2007
    Use this to create a 512 MB /tmp parition<br />
    <br />
    dd if=/dev/zero of=tmpMnt bs=1024 count=2097152
  • Gravatar - Daniel
    Daniel 16:56, March 8, 2008
    Which way increase space folder / tmp? <br />
    I created folder / tmp with 100MB. <br />
    Now folder / tmp fills fast. <br />
    I need to increase 2GB. <br />
    Someone can help me?
  • Gravatar - Rick
    Rick 03:51, June 7, 2008
    I just wanted to say thanks for this tutorial! My /tmp partition was created too small, and my /home partition is huge, so I used your tutorial to mount a new tmp partition inside my /home directory. Worked like a charm, thanks a bunch! :)
  • Gravatar - guy
    guy 00:41, September 10, 2008
    so does this just create another tmp folder in /dev rather than /var/tmpDSK or something? how does everything forward to this folder rather than the old just /tmp? I don't quite understand, thanks.

Add Your Thoughts

WebHostGear.com is a hosting directory, not a web host.

Copyright © 1998-2019 WebHostGear.com