使用SSH挂载远程目录

如何使用SSH挂载远程目录,就好像它是本地目录一样?

首先安装模块:

 sudo apt-get install sshfs 

将其加载到内核

 sudo modprobe fuse 

设置权限

 sudo adduser $USER fuse sudo chown root:fuse /dev/fuse sudo chmod +x /dev/fusermount 

现在我们将创建一个目录来安装远程文件夹。

我选择在我的主目录中创建它并将其命名为remoteDir。

 mkdir ~/remoteDir 

现在我运行命令来安装它(mount在home上)。

 sshfs maythux@192.168.xx.xx:/home/maythuxServ/Mounted ~/remoteDir 

现在应该安装它

 cd ~/remoteDir ls -l 

请享用

配置基于ssh密钥的身份validation

在本地主机上生成密钥对。

 $ ssh-keygen -t rsa 

使用回车键接受所有sugestions。

将公钥复制到远程主机:

 $ ssh-copy-id -i .ssh/id_rsa.pub user@host 

安装sshfs

 $ sudo apt install sshfs 

挂载远程目录

 $ sshfs user@host:/remote_directory /local_directory 

不要尝试将远程fs添加到/ etc / fstab

或者不要尝试通过/etc/rc.local挂载共享。

在这两种情况下,当init读取/ etc / fstab时,网络不可用,它将无法工作。

安装AutoFS

 $ sudo apt install autofs 

编辑/etc/auto.master

注释掉以下几行

 #+/etc/auto.master.d #+/etc/auto.master 

添加新行

 /- /etc/auto.sshfs --timeout=30 

保存并退出

编辑/etc/auto.sshfs

添加新行

 /local_directory -fstype=fuse,allow_other,IdentityFile=/local_private_key :sshfs\#user@remote_host\:/remote_directory 

远程用户名是强制性的。

保存并退出

在调试模式下启动autofs

 $ sudo service autofs stop $ sudo automount -vf 

观察远程ssh服务器的日志

 $ ssh user@remote_server $ sudo tailf /var/log/secure 

检查本地目录的内容

您应该看到远程目录的内容

在正常模式下启动autofs

使用CTRL-C停止在调试模式下运行AutoFS。

在正常模式下启动AutoFS

 $ sudo service autofs start 

请享用

(在Ubuntu 14.04上测试过)

安装sshfs

sudo apt-get install sshfs

添加到fstab:

 @:  fuse.sshfs delay_connect,_netdev,user,idmap=user,transform_symlinks,identityfile=/home//.ssh/id_rsa,allow_other,default_permissions,rw,nosuid,nodev,uid=1000,gid=1000,nonempty 0 0