使用sshfs挂载远程文件系统

什么是sshfs

ssfhs是一个客户端,可以让我们通过SFTP(SSH文件传输协议)挂载远程的文件系统,从而实现本机和远程的文件系统的交互

安装sshfs

yum -y install epel-release
yum -y install sshfs

有密码挂载远程文件系统

[root@localhost opt]# sshfs root@192.168.0.126:/opt share/   #将远程的/opt目录挂载在本机的share目录下,注意本机的share要是空目录
root@192.168.0.126's password:     #这种情况下要输入密码

无密码挂载远程文件系统

1.生成rsa公钥

[root@localhost opt]# ssh-keygen -t rsa    #一路回车生成id_rsa.pub公钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.   #公钥路径
The key fingerprint is:
SHA256:rQhwm//R5d70BBiyZ/fm8Z2y++4aa9AQhjkKnK07sKY root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|    . o   o      |
|     + . + o     |
|  . . o . + o    |
|   + + . . + o   |
|    * . S o B o  |
|   o = . o * o o |
|  o   + o . o...+|
| E     . . . =o==|
|        .   o=X==|
+----[SHA256]-----+

2.将公钥复制到服务器上,期间会输入密码一次

[root@localhost opt]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.126   
usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.126's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.0.126'"
and check to make sure that only the key(s) you wanted were added.

3.然后再挂载,就不会输入密码

[root@localhost share]# sshfs root@192.168.0.126:/opt/pyexecnetcache/APP_AutoTest/allure_reports ../share/

查看挂载点

[root@localhost share]# df -hT
Filesystem                                                         Type        Size  Used Avail Use% Mounted on
devtmpfs                                                           devtmpfs    899M     0  899M   0% /dev
tmpfs                                                              tmpfs       910M   24K  910M   1% /dev/shm
tmpfs                                                              tmpfs       910M  9.5M  901M   2% /run
tmpfs                                                              tmpfs       910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root                                            xfs          50G   12G   39G  23% /
/dev/sda1                                                          xfs        1014M  169M  846M  17% /boot
/dev/mapper/centos-home                                            xfs          27G   33M   27G   1% /home
tmpfs                                                              tmpfs       182M     0  182M   0% /run/user/0
root@192.168.0.126:/opt/pyexecnetcache/APP_AutoTest/allure_reports fuse.sshfs   50G   12G   39G  23% /opt/share

卸载挂载点

[root@localhost opt]# umount share/

参考文章

《sshfs远程挂载》
《使用 SSHFS挂载远程的Linux文件系统或目录》
《使用SSHFS文件系统通过SSH远程挂在目录》

posted @ 2020-04-26 14:21  cnhkzyy  阅读(981)  评论(0编辑  收藏  举报