挂载外置硬盘后新文件默认权限为777,如何修改为666?
Hey there! Let's tackle that permission problem you're facing with your external drive. The root cause lies in the mount options configured in /etc/fstab—the system file that handles automatic drive mounts. Here's a step-by-step solution to get those default file permissions down to a maximum of 666:
Step 1: Edit Your /etc/fstab File
First, open the fstab file with root privileges to modify your drive's mount entry:
sudo nano /etc/fstab
Locate the line for your external drive—it starts with UUID= followed by your drive's unique ID, like this example (replace with your actual UUID and mount path):
UUID=1234-ABCD /mnt/external_drive vfat defaults 0 0
Step 2: Adjust Mount Options Based on Your Filesystem
The exact options you'll add depend on whether your drive uses a Linux-native filesystem or a non-native one:
For Non-Native Filesystems (FAT32/NTFS)
These filesystems don't store Linux-style permissions natively, so we use fmask (for files) and dmask (for directories) to set defaults directly:
fmask=011: Calculates to777 - 011 = 666for files, giving yourw-rw-rwpermissions (exactly the "max 666" you want).dmask=000: Sets directories to777(rwxrwxrwx), which is required to navigate and modify directory contents. If you prefer more restrictive directory permissions, usedmask=022to get755(rwxr-xr-x).
Update your fstab line to look like this:
UUID=1234-ABCD /mnt/external_drive vfat defaults,fmask=011,dmask=000 0 0
For Linux-Native Filesystems (ext4, Btrfs, etc.)
These filesystems respect standard Unix permissions. Use the umask option to set a reverse mask:
umask=011: Results in files getting666(666 - 011 = 666) and directories getting777(777 - 011 = 777).
Your updated line will look like this:
UUID=abcd1234-efgh-ijkl-mnop-qrstuvwxyz1234 /mnt/external_drive ext4 defaults,umask=011 0 2
Step 3: Apply the Changes
Save the fstab file (in nano, press Ctrl+O, hit Enter, then Ctrl+X). Then unmount and remount the drive to activate the changes immediately:
sudo umount /mnt/external_drive sudo mount /mnt/external_drive
If you can't unmount the drive (e.g., it's in use), restart your system instead.
Verify the Fix
Create a test file to confirm permissions are now correct:
touch /mnt/external_drive/test_file ls -l /mnt/external_drive/test_file
You should see output showing permissions like -rw-rw-rw- (which corresponds to 666).
内容的提问来源于stack exchange,提问作者Luke W. Johnston

