fat32 usb 드라이브에 유저 권한으로 접근

Myeongwon Choi·2021년 6월 6일
0
post-thumbnail

fat32 파일 시스템을 사용하는 usb를 마운트시키고 유저 권한으로 쓰기를 하려니 권한이 없다며 오류가 발생했다. 파일 시스템에서 posix permissions는 fat32가 지원하지 않는다. 그래서 chmod나 chown 따위로 파일의 소유자를 변경하거나 권한을 변경하려고 하면 다음과 같은 오류가 발생한다.

➜  ~ sudo chmod 777 USBDrive 
chmod: changing permissions of 'USBDrive': Operation not permitted

아니 무슨 슈퍼유저 권한으로 실행했는데 권한이 변경이 안 되나 했는데, 이건 사실 당연한 거였다.

해결

이럴 때는 당황하지 말고 다른 플래그를 줘서 디스크를 마운트하면 된다.

➜  ~ lsblk -f
NAME        FSTYPE LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1      vfat          F50C-62ED                              57.3G     0% /home/pi/USBDrive
mmcblk0
├─mmcblk0p1 vfat   boot   8D64-2011                             203.5M    19% /boot
└─mmcblk0p2 ext4   rootfs 3857a514-b0f4-49ce-8430-34762068bb6f   49.6G    11% /

/dev/sda1이 내 마운트되어 있는 usb 드라이브의 파티션이다. 이 파티션의 uuid를 잘 기억해 두고,

➜  ~ sudo vi /etc/fstab

proc            /proc           proc    defaults          0       0
PARTUUID=dd55f14b-01  /boot           vfat    defaults          0       2
PARTUUID=dd55f14b-02  /               ext4    defaults,noatime  0       1
PARTUUID=<your-uuid>  /<your-mount-point> vfat  rw,umask=0000   0       0
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

이따위 파일이 나올텐데, 이 파일에서 아까 기억해둔 uuid 를 잘 찾아서 플래그 부분을 rw,umask=0000와 같이 수정해 주면 된다.

해설

/etc/fstab은 한번에 자동으로 마운트할 파티션들의 정보가 담겨 있는 파일이고, posix 매뉴얼에서도 확인할 수 있다.

➜  ~ man fstab


이와 같이 옵션을 주어서 마운트 시에 드라이브의 권한을 조정할 수 있는 것이다. rw,user와 같이 플래그를 주라는 문서도 있는데, 이렇게 하면 유저가 마운트를 해야지만 아마 유저에게 퍼미션이 생길 것이다.

       user   Allow an ordinary user to mount the filesystem.  The name
              of the mounting user is written to the mtab file (or to
              the private libmount file in /run/mount on systems without
              a regular mtab) so that this same user can unmount the
              filesystem again.  This option implies the options noexec,
              nosuid, and nodev (unless overridden by subsequent
              options, as in the option line user,exec,dev,suid).
              
       umask=value
              Set the umask (the bitmask of the permissions that are not
              present).  The default is the umask of the current
              process.  The value is given in octal.

그래서 우리는 그냥 umask 플래그를 줘서 권한을 통째로 777로 줘버리면 된다. 물론 이러면 보안에 취약해진다. 근데 chmod를 할 때는 줄 권한에 대한 마스크를 줘서 모든 권한을 주려면 777을 줘야 하는데, 여기서 umask 플래그를 적용할 때는 뺏을 권한에 대한 마스크이기 때문에 000을 주어야 한다. 이게 설계상 왜 이런지는 궁금하긴 한데 나중에 한번 알아봐야겠다.

여튼 매뉴얼을 잘 읽는 습관을 들이자!

profile
잡식 개발자

0개의 댓글