{"id":14264,"date":"2025-02-14T17:30:24","date_gmt":"2025-02-14T12:00:24","guid":{"rendered":"https:\/\/www.skynats.com\/?p=14264"},"modified":"2025-02-14T17:30:26","modified_gmt":"2025-02-14T12:00:26","slug":"secure-tmp-and-var-tmp-and-dev-shm-on-linux","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/secure-tmp-and-var-tmp-and-dev-shm-on-linux\/","title":{"rendered":"How to Secure \/tmp and \/var\/tmp and \/dev\/shm on Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In a Linux environment, it&#8217;s essential to secure temporary directories like \/tmp, \/var\/tmp, and \/dev\/shm, as they are vulnerable to malicious activity. Securing them helps prevent unauthorized access and the execution of harmful files. In this blog, we&#8217;ll guide you on how to secure \/tmp, \/var\/tmp, and \/dev\/shm on <a href=\"https:\/\/www.linux.org\/\" target=\"_blank\" rel=\"noopener\">Linux<\/a> by adjusting partition settings and modifying mount options.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-securing-tmp\" style=\"font-size:18px\"><strong>Securing \/tmp<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The \/tmp directory is often used by applications to store temporary files. Here&#8217;s how you can secure it:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Backup your \/etc\/fstab file to ensure you have a restore point.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/etc\/fstab \/etc\/fstab.back<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Create a new partition for \/tmp:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dd if=\/dev\/zero of=\/var\/tempFS bs=1024 count=3072000 \n\/sbin\/mkfs.ext3 \/var\/tempFS<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command creates a 3GB ext3 filesystem for \/tmp. Adjust the size based on your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: Backup your current \/tmp data.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp -Rpf \/tmp \/tmpbackup<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The command copies the entire \/tmp directory to \/tmpbackup, preserving permissions and overwriting existing files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: Mount the new partition with proper security settings.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount -o loop,noexec,nosuid,rw \/var\/tempFS \/tmp \nchmod 1777 \/tmp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5: Copy the old data back to the new \/tmp partition.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp -Rpf \/tmpbackup\/* \/tmp\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6: Edit \/etc\/fstab to make this change permanent.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano -w \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following line:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\/var\/tempFS \/tmp ext3 loop,nosuid,noexec,rw 0 0<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7: Remount \/tmp and verify.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount -o remount \/tmp \ndf -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check if \/tmp is properly mounted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-securing-var-tmp\" style=\"font-size:18px\"><strong>Securing \/var\/tmp<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can use \/tmp as \/var\/tmp for added security:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Move the existing \/var\/tmp directory.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mv \/var\/tmp \/var\/vartmp \nln -s \/tmp \/var\/tmp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Copy any existing data from \/var\/tmp to the new \/tmp.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/var\/vartmp\/* \/tmp\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-securing-dev-shm\" style=\"font-size:18px\"><strong>Securing \/dev\/shm<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The \/dev\/shm directory is used for shared memory, which could be vulnerable if misconfigured.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Edit \/etc\/fstab to secure \/dev\/shm.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano -w \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Locate the line for \/dev\/shm:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>none \/dev\/shm tmpfs defaults,rw 0 0<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Change it to:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>none \/dev\/shm tmpfs defaults,nosuid,noexec,rw 0 0<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Remount \/dev\/shm.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount -o remount \/dev\/shm<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion\" style=\"font-size:18px\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By following these steps, you can significantly improve the security of your system\u2019s temporary directories, protecting against unauthorized access and preventing the execution of malicious files in \/tmp, \/var\/tmp, and \/dev\/shm. Always remember to restart relevant services after making these changes for them to take effect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you need assistance to secure \/tmp, \/var\/tmp, and \/dev\/shm on Linux or require expert <a href=\"https:\/\/www.skynats.com\/linux-server-management\/\">Linux server management services<\/a>, feel free to contact us. Our team is ready to provide tailored solutions to enhance your system&#8217;s security and ensure optimal performance. Reach out today for professional support!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a Linux environment, it&#8217;s essential to secure temporary directories like \/tmp, \/var\/tmp, and \/dev\/shm, as they are vulnerable to malicious activity. Securing them helps prevent unauthorized access and the execution of harmful files. In this blog, we&#8217;ll guide you on how to secure \/tmp, \/var\/tmp, and \/dev\/shm on Linux by adjusting partition settings and [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[966],"class_list":["post-14264","post","type-post","status-publish","format-standard","hentry","category-blog","tag-secure-tmp-and-var-tmp-and-dev-shm-on-linux"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/14264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=14264"}],"version-history":[{"count":1,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/14264\/revisions"}],"predecessor-version":[{"id":14269,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/14264\/revisions\/14269"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=14264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=14264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=14264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}