使用 sshfs 挂载和写入文件权限

vka*_*ats 14 linux fuse sshfs

我尝试 sshfs 挂载远程目录,但挂载的文件不可写。我已经没有想法或方法来调试这个了。有什么我应该在远程服务器上检查的吗?

我使用的是 Xubuntu 14.04。我挂载了 14.04 Ubuntu 的远程目录。

local $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty
Run Code Online (Sandbox Code Playgroud)

我改变了/etc/fuse.conf

local $ sudo cat /etc/fuse.conf
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)

# Set the maximum number of FUSE mounts allowed to non-root users.
# The default is 1000.
#mount_max = 1000

# Allow non-root users to specify the allow_other or allow_root mount options.
user_allow_other
Run Code Online (Sandbox Code Playgroud)

我的用户在保险丝组中

local $ sudo grep fuse /etc/group
fuse:x:105:MY_LOACL_USERNAME
Run Code Online (Sandbox Code Playgroud)

我安装远程目录(尝试使用/不使用 sudo、default_permissions、allow_other 的组合):

local $sudo sshfs -o allow_other,default_permissions -o IdentityFile=/path/to/ssh_key  REMOTE_USERNAME@REMOTE_HOST:/remote/dir/path/  /mnt/LOCAL_DIR_NAME/
Run Code Online (Sandbox Code Playgroud)

REMOTE_USERNAME具有写权限目录/文件(在远程服务器上)。

我在没有 sudo、default_permissions 的情况下尝试了上述命令,在所有情况下我都得到:

local $ ls -al /mnt/LOCAL_DIR_NAME/a_file
-rw-rw-r-- 1 699 699 1513 Aug 12 16:08 /mnt/LOCAL_DIR_NAME/a_file
local $ test -w /mnt/LOCAL_DIR_NAME/a_file && echo "Writable" || echo "Not Writable"
Not Writable
Run Code Online (Sandbox Code Playgroud)

说明 0

回应 user3188445 的评论:

$ whoami
LOCAL_USER
$ cd
$ mkdir test_mnt
$ sshfs -o allow_other,default_permissions -o IdentityFile=/path/to/ssh_key  REMOTE_USERNAME@REMOTE_HOST:/remote/dir/path/ test_mnt/

$ ls test_mnt/
I see the contents of the dir correctly

$ ls -al test_mnt/
total 216
drwxr-xr-x  1 699 699  4096 Aug 12 16:42 .
drwxr----- 58 LOCAL_USER LOCAL_USER  4096 Aug 17 15:46 ..
-rw-r--r--  1 699 699  2557 Jul 30 16:48 sample_file
drwxr-xr-x  1 699 699  4096 Aug 11 17:25 sample_dir


$ touch test_mnt/new_file 
touch: cannot touch ‘test_mnt/new_file’: Permission denied

# extra info: SSH to the remote host and check file permissions
$ ssh REMOTE_USERNAME@REMOTE_HOST
# on remote host
$ ls -al /remote/dir/path/
lrwxrwxrwx 1 root root 18 Jul 30 13:48 /remote/dir/path/ -> /srv/path/path/path/
$ cd /remote/dir/path/
$ ls -al
total 216
drwxr-xr-x 26 REMOTE_USERNAME  REMOTE_USERNAME   4096 Aug 12 13:42 .
drwxr-xr-x  4 root root  4096 Jul 30 14:37 ..
-rw-r--r--  1 REMOTE_USERNAME  REMOTE_USERNAME   2557 Jul 30 13:48 sample_file
drwxr-xr-x  2 REMOTE_USERNAME  REMOTE_USERNAME   4096 Aug 11 14:25 sample_dir
Run Code Online (Sandbox Code Playgroud)

vka*_*ats 20

这个问题在一个linux 邮件列表中得到了回答;为了完整起见,我在这里发布了翻译后的答案。

解决方案

解决方案不是同时使用选项default_permissionsallow_other(我在最初的实验中没有尝试过)。

解释

问题似乎很简单。当您default_permissions在 fusermount 中提供选项时,fuse 对 fuse 安装的权限控制由内核处理,而不是由fuse 处理。这意味着 REMOTE_USER 的 uid/gid 没有映射到 LOCAL_USER (sshfs.c IDMAP_NONE)。它的工作方式与没有映射的简单 nfs fs 相同。

因此,如果 uid/gid 数字不匹配,则禁止访问是有意义的。

如果你有这个选项,allow_other那么这个目录只能由具有 uid 699 的本地用户写入,如果它存在的话。

来自保险丝的男人:

'default_permissions'

   By default FUSE doesn't check file access permissions, the
   filesystem is free to implement its access policy or leave it to
   the underlying file access mechanism (e.g. in case of network
   filesystems).  This option enables permission checking, restricting
   access based on file mode.  It is usually useful together with the
   'allow_other' mount option.

'allow_other'

   This option overrides the security measure restricting file access
   to the user mounting the filesystem.  This option is by default only
   allowed to root, but this restriction can be removed with a
   (userspace) configuration option.
Run Code Online (Sandbox Code Playgroud)

  • 只是一个澄清,因为我发现解释有点不清楚:它通过使用“allow_other”但没有“default_permissions”调用 sshfs 对我有用 (6认同)

use*_*445 7

不要用 sudo 运行 sshfs。如果这样做,ssh 会认为文件系统属于 root。自己运行它,然后您将能够写入文件。

澄清

在没有 sudo 的情况下运行时,您需要挂载到您自己的目录中,因为您可能无法写入 /mnt。因此,这是在将 user_allow_other 添加到 /etc/fuse.conf 后如何使用 sshfs 的示例:

$ cd                      # make sure you are in home directory
$ mkdir mnt               # create empty directory
$ sshfs server.com: mnt   # mount my home directory on server.com on ./mnt
$ ls mnt
[contents of home directory on server]
$ touch mnt/new_file      # no problem creating a new file
$ fusermount -u mnt       # unmount file system
$ rmdir mnt
Run Code Online (Sandbox Code Playgroud)