/tmp 디렉토리 보안 설정 [CentOS 7]
ℹ️이 콘텐츠에는 광고가 포함되어,판매 발생 시 수익이 발생합니다.(네이버 쇼핑 커넥트, 아마존 어필리에이트, 애드센스 등)리눅스 웹 서버를 운영할 때, /tmp 디렉토리는 시스템과 여러 애플리케이션이 임시 파일을 저장하는 공간으로 자주 사용됩니다.
하지만 /tmp 디렉토리는 누구나 쓰기 가능한 특성을 가지고 있어, 적절한 보안 설정 없이 방치하면 악성 스크립트 실행과 같은 보안에 위험이 될 수 있습니다.
✅ /tmp 디렉토리 보안 설정 항목
ext4 또는 xfs로 /tmp 파티션 생성 시 고급 설정에서 noexec,nosuid,nodev 3가지 설정을 합니다.
| 
 옵션 35880_c56e11-6e> | 
 설명 35880_122732-de> | 
|---|---|
| 
 noexec 35880_032d01-11> | 
 실행 파일 실행 방지 35880_ad0cda-12> | 
| 
 nosuid 35880_b7812b-e6> | 
  | 
| 
 nodev 35880_73663b-40> | 
 장치 파일 사용 금지 35880_139471-28> | 
minimal 설치가 진행되는 경우 옵션 설정이 없을 수 있습니다.
✅ /etc/fstab 에서 /tmp 디렉토리 보안 설정
fstab은 리눅스에서 부팅 시 자동으로 마운트 할 파일 시스템 정보를 담고 있는 설정 파일입니다.
🔲 /etc/fstab에 /tmp 설정
VirtualBox에 설치한 CentOS 7 OS의 /tmp 디렉토리 설정입니다.
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 988M     0  988M   0% /dev
tmpfs                   1000M     0 1000M   0% /dev/shm
tmpfs                   1000M  8.6M  991M   1% /run
tmpfs                   1000M     0 1000M   0% /sys/fs/cgroup
/dev/mapper/centos-root  4.8G  2.7G  1.9G  59% /
/dev/mapper/centos-home  6.3G   30M  6.0G   1% /home
/dev/mapper/centos-tmp   3.9G   17M  3.6G   1% /tmp
/dev/sda2                497M  163M  335M  33% /boot
tmpfs                    200M     0  200M   0% /run/user/0이미 파티션 /dev/mapper/centos-tmp에 별도로 마운트되어 있는 상태입니다.
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    2M  0 part 
├─sda2            8:2    0  500M  0 part /boot
└─sda3            8:3    0 19.5G  0 part 
  ├─centos-root 253:0    0    5G  0 lvm  /
  ├─centos-swap 253:1    0    4G  0 lvm  [SWAP]
  ├─centos-tmp  253:2    0    4G  0 lvm  /tmp
  └─centos-home 253:3    0  6.5G  0 lvm  /home
sr0              11:0    1 1024M  0 rom  
~]# fdisk /dev/새로운 블록 스토리지 
...
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):
/etc/fstab vi로 열어 다음과 같이 설정합니다.
vi /etc/fstab#
# /etc/fstab
# Created by anaconda on Sun Jun 22 05:41:51 2025
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       ext4    defaults        1 1
UUID=674a0523-22b2-4755-9eeb-983294c12708 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   ext4    defaults        1 2
# /dev/mapper/centos-tmp  /tmp                  ext4    defaults        1 2
/dev/mapper/centos-tmp    /tmp                  ext4    defaults,noexec,nosuid,nodev    1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0기존 설정을 주석 처리한 후에 noexec, nosuid, nodev 설정을 적용합니다.
재 마운트를 진행합니다.
mount -o remount /tmp
mount | grep /tmp
/dev/mapper/centos-tmp on /tmp type ext4 (rw,nosuid,nodev,noexec,relatime,data=ordered)✅ /var/tmp 디렉토리 삭제 후 /tmp 디렉토리 심볼릭 링크 연결
통합적인 보안 관리를 위해 /var/tmp 디렉토리를 삭제 후 /tmp 디렉토리에 심볼릭링크로 연결을 진행합니다.
rm -rf /var/tmp
rm: cannot remove `/var/tmp/': 장치나 자원이 동작 중
ln -s /tmp /var/tmp장치에서 실행 중인 것으로 삭제가 진행되지 않아 싱글모드로 진입하여 작업을 진행합니다.
재부팅 후에 GRUB 부트로더에서 [e] 버튼으로 편집 모드로 진입 후
![/tmp 디렉토리 보안 설정 [CentOS 7] 7 CentOS 7 GRUB 부트로더](https://cdn.uknew.co/storage/2025/06/CentOS-7-GRUB-부트로더-1024x568.png)
싱글모드 설정을 진행합니다.
/var/tmp 디렉토리 삭제 후 싱글모드에서 심볼릭링크를 같이 생성합니다.
ln -s /tmp /var/tmp
이후에 재부팅 시 설정했던 싱글 모드는 일회성이기 때문에 single 텍스트는 자동으로 지워집니다.
✅ /var/tmp 디렉토리 별도 보안
RHEL 7,8,9 의 기본 설정을 그대로 사용할 수도 있습니다.
 ~]# ll /var/tmp -d
drwxrwxrwt. 9 root root 4096  6월 28 22:45 /var/tmp[t] 설정은 sticky bit설정으로 이미 소유자만 삭제 및 수정이 가능한 상태여서 별도의 수정이 필요한 경우는 거의 없습니다. /tmp 디렉토리 역시 거의 동일한 설정을 따릅니다.
 ~]# cat /usr/lib/tmpfiles.d/tmp.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
q /tmp 1777 root root 10d
q /var/tmp 1777 root root 30d ~]# systemctl status systemd-tmpfiles-clean.service
○ systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
     Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static)
     Active: inactive (dead) since Sat 2025-06-28 16:18:43 KST; 7h ago
TriggeredBy: ● systemd-tmpfiles-clean.timer
       Docs: man:tmpfiles.d(5)
             man:systemd-tmpfiles(8)
    Process: 86720 ExecStart=systemd-tmpfiles --clean (code=exited, status=0/SUCCESS)
   Main PID: 86720 (code=exited, status=0/SUCCESS)
        CPU: 14ms
 6월 28 16:18:43 ip-172-26-10-157.ap-northeast-2.compute.internal systemd[1]: Starting Cleanup of Temporary Directo>
 6월 28 16:18:43 ip-172-26-10-157.ap-northeast-2.compute.internal systemd[1]: systemd-tmpfiles-clean.service: Deact>
 6월 28 16:18:43 ip-172-26-10-157.ap-northeast-2.compute.internal systemd[1]: Finished Cleanup of Temporary Directo>
lines 1-13/13 (END)/tmp 파일에 대해 /etc/fstab에 연결하여 noexec, nosuid, nodev 등의 마운트 옵션으로 보안 정책을 적용하고, /var/tmp에 대해서는 기본 권한과 systemd-tmpfiles의 자동 정리 정책을 그대로 유지하는 방식을 권장합니다.
이렇게 하면 /tmp는 실행 파일 차단 등 엄격한 보안 조치를 적용하여 임시 실행 공간으로 안전하게 관리하고, /var/tmp는 장기 임시 저장소로서의 기능을 유지하면서도 불필요한 파일은 자동으로 정리되어 시스템 자원을 효율적으로 관리할 수 있습니다.
필요 시 SELinux 정책을 추가로 적용해 보안을 강화해서 사용할 수 있습니다.