Linux: Create /tmp And Mount as Partition File With the noexec, nosuid, And nodev

The /tmp directory in Linux is used for temporary files and is typically stored on the root file system. However, for security reasons, it is recommended to create a separate partition for /tmp and mount it with the noexec, nosuid, and nodev options to prevent the execution of binaries, privilege escalation, and device access.

Here are the steps to create and mount a separate /tmp partition with the required options:

  1. Create a new partition using the fdisk or parted command. For example, to create a new partition on /dev/sdb, run:
fdisk /dev/sdb
  1. Create a file system on the new partition. For example, to create an ext4 file system, run:
mkfs.ext4 /dev/sdb1
  1. Create a mount point for the new partition. For example, to create a mount point at /mnt/tmp, run:
mkdir /mnt/tmp
  1. Add an entry to /etc/fstab to mount the new partition at boot time with the required options. For example, add the following line to /etc/fstab:
/dev/sdb1 /mnt/tmp ext4 noexec,nosuid,nodev 0 0
  1. Mount the new partition using the mount command:
mount /mnt/tmp
  1. Move the contents of the original /tmp directory to the new partition. For example, to move the contents to the new partition using the rsync command:
rsync -avz /tmp/ /mnt/tmp/
  1. Remove the contents of the original /tmp directory:
rm -rf /tmp/*
  1. Create a symbolic link from the original /tmp directory to the new partition:
ln -s /mnt/tmp /tmp

With these steps, you have successfully created and mounted a separate /tmp partition with the required options. Note that the noexec, nosuid, and nodev options are recommended for security reasons, but may impact the functionality of some applications that require the execution of binaries or device access. Therefore, it is important to thoroughly test the system after making these changes.

Leave a Comment