如何在Linux上使用SSHFS挂载远程目录

时间:2020-01-09 10:39:32  来源:igfitidea点击:

如何在Linux基本系统上使用ssh挂载远程目录?在Ubuntu或Debian/RHEL/CentOS/Arch Linux系统上,如何使用SSHFS通过SSH挂载远程文件系统?

SSH是一种安全协议,您可以使用它在SSHF服务的帮助下在远程服务器或本地便携式计算机上安装目录。使用SSHFS,您可以将远程服务器文件系统挂载到Linux驱动的本地开发工作站/笔记本电脑上。

有关SSHFS的更多信息

sshfs是基于SSH文件传输协议的文件系统。
它在客户端系统上使用,即您需要在由CentOS/RHEL/Ubuntu/Debian/Arch Linux驱动的本地计算机/笔记本电脑上安装sshfs软件包。
无需在服务器(server1.theitroad.local)上安装任何软件。
您只需要在服务器端安装一个openssh服务器。

在Ubuntu/Debian/Mint Linux上安装SSHFS

输入以下apt-get命令:

apt-get update && apt-get upgrade
sudo apt-get install sshfs

输出示例:

[sudo] password for theitroad: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  sshfs
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 41.7 kB of archives.
After this operation, 138 kB of additional disk space will be used.
Get:1 http://mirror.ox.ac.uk/sites/archive.ubuntu.com/ubuntu/ trusty/main sshfs amd64 2.5-1ubuntu1 [41.7 kB]
Fetched 41.7 kB in 1s (27.8 kB/s)
Selecting previously unselected package sshfs.
(Reading database ... 247545 files and directories currently installed.)
Preparing to unpack .../sshfs_2.5-1ubuntu1_amd64.deb ...
Unpacking sshfs (2.5-1ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up sshfs (2.5-1ubuntu1) ...

在Arch Linux上安装SSHFS

执行以下命令:

sudo pacman -S sshfs fuse

确保将用户名为Hyman的用户添加到保险丝组:

## [ create fuse group if does not exist as per your distro ] ##
$ sudo groupadd fuse
$ sudo usermod -a -G fuse Hyman

在RHEL(Red Hat)/CentOS Linux上安装SSHFS

首先,打开EPEL repo,然后执行以下yum命令以安装FUSE-Filesystem,以在CentOS/RHEL上通过SSH访问远程文件系统:

sudo yum update
sudo yum install fuse-sshfs

输出示例:

Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
0 packages excluded due to repository protections
Resolving Dependencies
--> Running transaction check
---> Package fuse-sshfs.x86_64 0:2.4-1.el6 will be installed
--> Processing Dependency: fuse >= 2.2 for package: fuse-sshfs-2.4-1.el6.x86_64
--> Running transaction check
---> Package fuse.x86_64 0:2.8.3-4.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=======================================================================================================================
Package                   Arch                  Version                     Repository                           Size
=======================================================================================================================
Installing:
fuse-sshfs                x86_64                2.4-1.el6                   epel                                 52 k
Installing for dependencies:
fuse                      x86_64                2.8.3-4.el6                 rhel-x86_64-server-6                 71 k
 
Transaction Summary
=======================================================================================================================
Install       2 Package(s)
 
Total download size: 123 k
Installed size: 115 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): fuse-2.8.3-4.el6.x86_64.rpm                                                              |  71 kB     00:00     
(2/2): fuse-sshfs-2.4-1.el6.x86_64.rpm                                                          |  52 kB     00:00     
----------------------------------------------------------------------------------------------------------------------
Total                                                                                  173 kB/s | 123 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : fuse-2.8.3-4.el6.x86_64                                                                             1/2 
  Installing : fuse-sshfs-2.4-1.el6.x86_64                                                                         2/2 
  Verifying  : fuse-sshfs-2.4-1.el6.x86_64                                                                         1/2 
  Verifying  : fuse-2.8.3-4.el6.x86_64                                                                             2/2 
 
Installed:
  fuse-sshfs.x86_64 0:2.4-1.el6                                                                                        
 
Dependency Installed:
  fuse.x86_64 0:2.8.3-4.el6                                                                                            
 
Complete!

如何挂载远程文件系统?

语法是

sshfs user@server /path/to/mountpoint
sshfs user@server /path/to/mountpoint options

首先,使用mkdir命令创建目录:

sudo mkdir /mnt/server1

我要使用root用户挂载文件系统,并且需要在出现提示时输入root密码:

sudo sshfs [email protected]:/ /mnt/server1/
 
##  OR use ssh key based login ##
sudo sshfs -o IdentityFile=~/.ssh/keyfile /mnt/server1/

输出示例:

Password for root@freebsd10:

验证一下:

sudo df -h

输出示例:

Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/wks05-root  487G  114G  350G  25% /
none                    4.1k     0  4.1k   0% /sys/fs/cgroup
udev                     17G  4.1k   17G   1% /dev
tmpfs                   3.4G  1.9M  3.4G   1% /run
none                    5.3M     0  5.3M   0% /run/lock
none                     17G  160k   17G   1% /run/shm
none                    105M   50k  105M   1% /run/user
/dev/sda1               239M   89M  138M  40% /boot
[email protected]:/     20G   12G  6.8G  64% /mnt/server1

要访问和/或查看远程文件系统,请运行:

sudo -s
cd /mnt/server1
ls -l

处理权限被拒绝错误和建议的安装远程目录的过程

如果您收到一个错误消息,显示为无法访问server1:权限被拒绝,则将自己添加到名为fuse的组中:

$ sudo gpasswd -a "$USER" fuse
`Adding user theitroad to group fuse`

接下来,在您自己的主目录中创建一个安装点:

$ mkdir $HOME/server1
$ ls -ld !$

要挂载远程文件系统,请执行:

sshfs -o idmap=user  [email protected]:/ $HOME/server1
df 
ls -l $HOME/server1

在本地笔记本电脑/台式机上使用没有root访问权限的sshfs

如何卸载远程文件系统?

语法为:

sudo umount /mnt/server1
 
## 或者
fusermount -u /mnt/server1

验证一下:

df -h

如何通过更新/etc/fstab永久安装远程文件系统?

对于永久性挂载,您必须基于登录创建ssh密钥

$ ssh-keygen -t rsa
$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

现在,编辑/etc/fstab文件,执行:

sudo vi /etc/fstab

语法为:

userNameHere@FQDN_OR_IP_HERE:/path/to/source/  /local/mountdir/  fuse.sshfs  defaults,_netdev  0  0

例子

在文件底部添加以下条目:

sshfs#[email protected]:/ /mnt/server1

带有其他选项的另一个示例:

sshfs#[email protected]:/ /mnt/server1 fuse defaults,idmap=user,allow_other,reconnect,_netdev,users,IdentityFile=/path/to/.ssh/keyfile 0 0

如果使用systemd,建议按需安装选项:

[email protected]:/project/www/ /mnt/server1  fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/Hyman/.ssh/id_rsa,allow_other,reconnect 0 0

保存并关闭文件。
其中:

  • [email protected]:带有sshd的远程服务器
  • fuse:文件系统类型。
  • idmap = user:仅转换连接用户的UID。
  • allow_other:允许访问其他用户。
  • reconnect:重新连接到服务器。
  • _netdev:文件系统驻留在需要网络访问的设备上(用于防止系统尝试在系统上启用网络之前挂载这些文件系统)。
  • users:允许每个用户挂载和卸载文件系统。
  • IdentityFile =/path/to/.ssh/keyfileSSH密钥文件。