8

使用 sshfs 挂载远程文件系统

 2 years ago
source link: https://xujinzh.github.io/2022/06/08/sshfs-mount-rfs-over-ssh/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在两台服务器间借助 SSH 连接使用 SFTP 或 SCP 可以互相传输文件,但这确不能共享整个文件系统。当我们想要像访问本地服务器的文件系统一样访问远程服务器的文件系统时,可以使用 sshfs 工具,它通过 SSH 文件传输协议 (SFTP) 挂载远程的文件系统。本篇以 ubuntu 系统为例,所有代码以普通用户运行,部分代码需要 sudo 权限。

sudo apt update
sudo apt install sshfs

挂载远程服务器文件系统

假设本地的用户 jinzhongxu 能够通过 SSH 连接到远程服务器 tencent.test.com

# 创建挂载点
sudo mkdir /mnt/tencent
sudo chown -R jinzhongxu:jinzhongxu /mnt/tencent

# 挂载远程文件系统到挂载点
sshfs [email protected]:/home/jinzhongxu /mnt/tencent -o allow_other

注意,挂载时因为指定了 allow_other,所以就像普通的磁盘挂载一样。sshfs 默认情况下会阻止这种情况,所以建议带上该参数,否则将导致除 root 用户外其他用户无法访问文件系统。同时,指定该参数,需要在配置文件中放开权限,方法如下:

sudo vim /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

当挂载成功后,访问 /mnt/tencent 即可访问远程服务器 tencent.test.com 里的 /home/jinzhongxu 文件。

查看挂载情况

df -hT
fusermount -u /mnt/tencent
# or
sudo umount /mnt/tencent

上面的挂载方法通过 SSH 来实现,当连接断开后则挂载失效。因此,SSHFS 可以作为一种灵活的、临时的挂载方法。但想要永久挂载,可以进行如下配置来实现。

sudo vim /etc/fstab

在最后一行添加如下内容

[email protected]:/home/jinzhongxu /mnt/tencent fuse.sshfs noauto,x-systemd.automount,_netdev,reconnect,identityfile=/home/jinzhongxu/.ssh/id_rsa,allow_other 0 0

更新 fstab 文件使修改生效

sudo mount -a

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK