我们使用windows共享习惯了,总希望在Linux上执行相同,或类似的操作。可喜的是Linux内核,提供ssh服务。所以,我们可以方便的借助于sshfs工具实现远程服务器的访问。本文中,使用简单的方法,实现了此过程。整个操作过程记录文档,可以参考资源:
http://download.csdn.net/detail/attagain/6847317
http://download.csdn.net/detail/attagain/7802929
1、 应用说明
服务端:指提供共享目录,用来为客户端提供访问服务
客户端:访问服务器端共享目录的终端
2、 客户端环境准备
客户端IP:192.168.30.206
1) 客户端sshfs-fuse工具安装
官网URL:http://sourceforge.net/projects/fuse/files/sshfs-fuse/
安装fuse的方法:
[TSP@tsp-rls-webservice01 ~]$ tar xvf fuse-2.9.3.tar.gz
[TSP@tsp-rls-webservice01 ~]$ cd fuse-2.9.3
[TSP@tsp-rls-webservice01 fuse-2.9.3]$ ./configure
[TSP@tsp-rls-webservice01 fuse-2.9.3]$ make
[TSP@tsp-rls-webservice01 fuse-2.9.3]$ sudo make install
安装sshfs-fuse的安装方法:
[TSP@tsp-rls-webservice01 ~]$ tar xvf sshfs-fuse-2.5.tar.gz
[TSP@tsp-rls-webservice01 ~]$ cd sshfs-fuse-2.5
[TSP@tsp-rls-webservice01 sshfs-fuse-2.5]$ ./configure
[TSP@tsp-rls-webservice01 sshfs-fuse-2.5]$ make
[TSP@tsp-rls-webservice01 sshfs-fuse-2.5]$ sudo make install
3、 配置ssh自动登陆
1) 在客户端使用普通用户生成密钥对
客户端IP:192.168.30.206
执行命令:ssh-keygen -d
执行结果如下:
[TSP@tsp-rls-webservice01 ~]$ ssh-keygen -d
Generating public/private dsa key pair.
Enter file in which to save the key (/home/TSP/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/TSP/.ssh/id_dsa.
Your public key has been saved in /home/TSP/.ssh/id_dsa.pub.
The key fingerprint is:
61:23:a4:22:8c:af:37:d9:9d:c8:c5:6e:e6:54:eb:a5 TSP@tsp-rls-webservice01
The key's randomart image is:
+--[ DSA 1024]----+
| . |
|o o |
|o.. . . + |
| o . . o o |
| . o S |
| . + = o . |
|. + + B . . |
| . . = . o |
| . E |
+-----------------+
查看生成的密钥对文件,执行命令:ll /home/TSP/.ssh
[TSP@tsp-rls-webservice01 ~]$ ll /home/TSP/.ssh
总用量 8
-rw-------. 1 TSP TSP 668 8月 22 10:14 id_dsa
-rw-r--r--. 1 TSP TSP 614 8月 22 10:14 id_dsa.pub
2) 通过客户端,将公钥上传至服务端
客户端IP:192.168.30.206
执行命令:ssh-copy-id -i ~/.ssh/id_dsa.pub 192.168.30.202
执行结果如下:
[TSP@tsp-rls-webservice01 ~]$ ssh-copy-id -i ~/.ssh/id_dsa.pub 192.168.30.202
The authenticity of host '192.168.30.202 (192.168.30.202)' can't be established.
RSA key fingerprint is 69:f8:f9:5a:3a:c7:7e:f2:ad:f3:85:79:43:41:99:3d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.30.202' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '192.168.30.202'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
注意:中间过程,需要输入服务端的用户密码
根据上述提示,在客户端使用ssh命令登陆服务端:
[TSP@tsp-rls-webservice01 ~]$ ssh 192.168.30.202
Last login: Fri Aug 22 09:31:06 2014 from 192.168.30.65
[TSP@tsp-rls-webservice ~]$ pwd
/home/TSP
[TSP@tsp-rls-webservice ~]$ ll .ssh/authorized_keys
-rw-------. 1 TSP TSP 614 8月 22 10:18 .ssh/authorized_keys
3) 确认服务端的公钥信息
服务端IP:192.168.30.202
使用服务端TSP用户登录,查看服务端.ssh目录下公钥信息,执行命令:
ll ~/.ssh/authorized_keys
执行结果如下:
[TSP@tsp-rls-webservice ~]$ ll ~/.ssh/authorized_keys
-rw-------. 1 TSP TSP 614 8月 22 10:18 /home/TSP/.ssh/authorized_keys
4) 客户端建立sshfs共享
客户端IP:192.168.30.202
执行命令:sshfs [email protected]:/tsp /tsp -o allow_other,nonempty
options重点关注下:
-C 压缩,或者-o compression=yes
-o reconnect 自动重连
-o transform_symlinks 表示转换绝对链接符号为相对链接符号
-o follow_symlinks 沿用服务器上的链接符号
-o cache=yes
-o allow_other 这个参数最重要,必须写,否则任何文件都是Permission Deny
如果想卸载当前sshfs挂载,使用如下命令:
[TSP@tsp-rls-webservice01 /]$ fusermount -u /tsp
5) 出现allow_other参数不正常的解决方法
错误信息如下:
[TSP@tsp-rls-webservice01 tsp]$ sshfs [email protected]:/tsp /tsp -o allow_other,nonempty
fusermount: option allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf
解决方法,安装提示,在/etc/fuse.conf中添加user_allow_other
[TSP@tsp-rls-webservice01 tsp]$ sudo vi /etc/fuse.conf
添加如下信息,并保存:
user_allow_other