WebHostGear.com - the hosting resource for professionalshosting tutorials 
hosting howto webhost guide server managementFebruary 08, 2010
server management, apache tutorials, hosting tutorials, cpanel, server security
Home / Hosting Tutorials / cPanel Hosting / Securing Your /tmp Partition with Cpanel/WHM

Securing Your /tmp Partition with Cpanel/WHM



Printer Friendly Printer Friendly Send to a friend Send to a friend
By : ramprage Rating : Average Rating : 9.03 From 62 Voter(s)

Securing Your /tmp Partition with Cpanel/WHM

If you are renting a server then chances are everything is lumped in / and a small amount partitioned for /boot and some for swap. With this current setup, you have no room for making more partitions unless you have a second hard-drive. Learn how to create a secure /tmp partition even while your server is already up and running.
Recently, I found out it would be worthwhile to give /tmp it's own partition and mount it using noexec- This would protect your system from MANY local and remote exploits of rootkits being run from your /tmp folder.

What we are doing it creating a file that we will use to mount at /tmp. So log into SSH and SU to root so we may being!

code:
cd /dev

Create 100MB file for our /tmp partition. If you need more space, make count size larger.

code:
dd if=/dev/zero of=tmpMnt bs=1024 count=100000

Make an extended filesystem for our tmpMnt file

code:
/sbin/mke2fs /dev/tmpMnt

Backup your /tmp dir- I had mysql.sock file that I needed to recreate the symbolic link for. Other programs may use it to store cache files or whatever.



Article provided by WebHostGear.com

code:
cd /

code:
cp -R /tmp /tmp_backup

Mount the new /tmp filesystem with noexec

code:
mount -o loop,noexec,nosuid,rw /dev/tmpMnt /tmp

code:
chmod 1777 /tmp

Copy everything back to new /tmp and remove backup

code:
cp -R /tmp_backup/* /tmp/

code:
rm -rf /tmp_backup

Now we need to add this to fstab so it mounts automatically on reboots.

code:
pico -w /etc/fstab

You should see something like this:
code:
/dev/hda3               /                       ext3    defaults,usrquota        1 1
/dev/hda1               /boot                   ext3    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
none                    /proc                   proc    defaults        0 0
none                    /dev/shm                tmpfs   defaults        0 0
/dev/hda2               swap                    swap    defaults        0 0

At the bottom add
code:
/dev/tmpMnt             /tmp                    ext2    loop,noexec,nosuid,rw  0 0

(Each space is a tab)
Save it!
Ctrl + X and Y

Your done- /tmp is now mounted as noexec. You can sleep a little bit safer tonight. I created a hello world c++ and compiled it then moved it to /tmp. Upon trying to run it (even chmod +x'ed), it gives the following error:

code:
bash: ./a.out: Permission denied

Yay! /tmp no longer has execute permissions :-D

New! - Need server help? Hire an Expert

Get professional help with your configuration, script installation or server issue.
Learn how we can help you with any server problem and make your server run like new. Professional staff will contact you, after submitting a quote request, by phone or email.

Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent
Sources
To recieve full server management by the auther contact steve@swirldot.com

Related Articles


Official Cpanel Newbie Guide
Setting up Private Nameservers in Cpanel
Disable Direct Root Logins
Common SSH Commands - Linux Shell Commands


Discuss this article with others in our new hosting forums

Comments / Feedback

Thomas Dawson
mke2fs command is not recognized in my ssh. What do I do?
Ramprage
Use /sbin/mke2fs instead
kel
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
Don Holeman
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
simoa
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
Ramprage
The chkrootkit aliens found message is normal when creating a tmp from a file, no worries.
mazen
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'.)
cruz
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?
Mo
use: cp -pR /tmp /tmp_backup instead of : cp -R /tmp /tmp_backup add -p to preserve file permitions and ownership.
Shanlar
ok i made my tmp to big, how do i reverse this process? tmp keeps saying device is busy and wont umount...
Gary
remember though, this wont save you for losers who run scripts by perl php etc....those commands can still work in /tmp
Rona
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
ca
@Shanlar I suppose you should take it out of fstab, do a server reboot and create a tmp directory again.
Michael Curtis
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 ;)
C0NIk
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
GIGI
look this
------------
root@localhost [~]# sh ./Install_DigiChat.bin -i console
Preparing to install...
tail: `-1' option is obsolete; use `-n 1'
Try `tail --help' for more information.
./Install_DigiChat.bin: line 329: [: `)' expected, found -z
WARNING! The amount of /tmp disk space required and/or available
could not be determined. The installation will be attempted anyway.
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.3158/Linux/resource/jre/bin/java: /bin/sh: bad interpreter: Permission denied
-------
pliz help me....
Robert Greenwood
You will need to increase the size of the mount
Foow
can you tell me why you set the /tmp partition to ext2 and not ext3?
Mark
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?
JLChafardet
have any one tried this over Plesk Based webservers?

if yes, please let me know if there is any change.

regards,

JLChafardet
Sotek
change this:

code:
chmod 0777 /tmp

and put this:

chmod 1777 /tmp

beacuse you need to set the bit who lets everyone write on /tmp
kaveh
hi
how can i resize the partition ?
97% full !!!!
Big AL
you can't resize a partition, just move things to /home/tmp and link /tmp to /home/tmp via ln -s /home/tmp /tmp
Mateus
Hi, thanks for this tuto..
But, i already have this partition
LABEL=/tmp /tmp ext3 defaults 1 2

my server is in theplanet and is the default mount for them.. how can i modify this ? thank you!
Lechoad
how do you undo this?
netkinetics
Change defaults to say noexec,nosuid if you already have a seperate /tmp partition.

Then umount / mount it again.

Also do the same with the /dev/shm mount.

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.

rayan
hi
ist same steps for directadmin control panel?
Steve
Yeah it should be the same Rayan, it's not control panel reliant.
David
Can i add the "noexec" just by changing the fstab?
complex
Just in case anyone wants to do this with FreeBSD, see this page (example 16-5 or 16-7):

http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/disks-virtual.html
Kapil
If you have cpanel for freebsd, then just run the script /tmp/securetmp

Mateus
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!.. :/
M.S.
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.
xkasi
To increase the /tmp size just redo the tutorial with a bigger count and an different filename.
e.g:
dd if=/dev/zero of=tmpMnt2 bs=1024 count=2000000

Greetz
Vijay
Hi,

/scripts/securetmp
Would you like to secure /tmp & /var/tmp at boot time? (y/n) y
Would you like to secure /tmp & /var/tmp now? (y/n) y
Securing /tmp & /var/tmp
/tmp is already secure
/var/tmp is already secure
Process Complete.

But fstab does not show noexec,
# cat /etc/fstab
# This file is edited by fstab-sync - see 'man fstab-sync' for details
LABEL=/tmp /tmp ext3 defaults 1 2

I request others to verify whether following method is correct for CPanel 10.8. ?

1) Because cpanel installed with /tmp saperate partition. - we do not need to change/recreate partition & copy.
2) modify /etc/fstab to following.
LABEL=/tmp /tmp ext3 loop,noexec,nosuid,rw 0 0
3) reboot server.
steve
Use this to create a 512 MB /tmp parition

dd if=/dev/zero of=tmpMnt bs=1024 count=2097152
Daniel
Which way increase space folder / tmp?
I created folder / tmp with 100MB.
Now folder / tmp fills fast.
I need to increase 2GB.
Someone can help me?
Rick
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! :)
guy
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 Comment
Name
Email
Image Code
Refresh Image

Comments / Feedback



Our site offers free hosting tutorials, cpanel tutorial, web hosting news, shell commands, running a web hosting business, dedicated guides, linux tutorial, apache install, home web server, web server guide, ssh commands, dedicated servers, DNS nameservers, chkrootkit, apf firewall, exim configuration, server compromised, cron backup solution, ftp backup script

Server Tutorials


WebHostGear Reviewed by Ping Zine - Click here

Links:
Lunarpages Coupon