ky818smKy818sm  2023-11-09 10:34 旷野小屋_123ppp资源网 隐藏边栏 |   抢沙发  26 
文章评分 0 次,平均分 0.0

Linux服务器之间挂载远程目录,前提条件是两台服务器能够通过SSH进行访问。

比如有两台服务器:
本机 192.168.0.1 /public
远程服务器 111.111.111.111 /share

1.服务器间免密登录准备

1.1 生成本机秘钥
在自己服务器192.168.0.1生成秘钥:
ssh-keygen -t rsa 一直默认回车
ssh-keygen -t rsa
然后在/root/.ssh/目录下会有如下文件:

authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥
id_rsa : 生成的私钥文件
id_rsa.pub : 生成的公钥文件
know_hosts : 已知的主机公钥清单

如果希望ssh公钥生效需满足至少下面两个条件:
.ssh目录的权限必须是700
.ssh/authorized_keys文件权限必须是600

1.2 SCP远程文件拷贝
进入本机.ssh文件下,执行SCP拷贝
# SSH端口:22
scp -P 22 id_rsa.pub [email protected]:/root/

1.3 目标主机秘钥整理

登录111.111.111.111,进入111主机的root根目录,执行:
cat id_rsa.pub >> .ssh/authorized_keys

1.4 SSH免密登录测试
在自己服务器测试 ssh -p 22 [email protected],如果不需要输入密码进入服务器,则测试成功

2. SSHFS远程目录挂载操作

2.1 SSHFS工具安装及挂载
将远程主机111.111.111.111的share目录挂载到本机的public下:

yum -y install fuse-sshfs

2.1 SSHFS挂载多用户权限解决
若主机中存在多个用户,root用户用上一节的挂载命令时,只能够root用户访问,而其他用户不具备权限,会出现无法访问的情况。即使赋权限777仍旧不行,所以如果需要其他他用户也能够访问该挂载目录,则需要重新挂载,并执行如下命令:
sshfs -o rw,allow_other [email protected]:/share /public

注意:当挂载路径下已经有这个同名路径时,为了避免冲突,会报这个信息,并且新的文件还挂载不上去:
fuse: mountpoint is not empty
可以看出只需要添加 -o nonempty 参数即可
sshfs -o rw,allow_other [email protected]:/share /public -o nonempty

如果需要开机自动挂载,可以在/etc/fstab文件中添加:


sshfs#[email protected]:/path /path fuse user,_netdev,exec,reconnect,uid=0,gid=0,idmap=user,allow_other,Port=22,nonempty  0 0

然后让修改后的设置生效:


sudo mount -a

卸载命令:
fusermount -u /public

3. sshfs 常用参数解释

1. -o allow_other:允许其他用户访问挂载点中的文件,默认情况下只有挂载该文件系统的用户才能访问。

2. -o reconnect:在断线后自动重新连接远程服务器。

3. -o cache_timeout=秒数:指定缓存的超时时间,单位为秒,默认为20秒。

4. -o follow_symlinks:跟踪符号链接,可以让sshfs在挂载远程文件系统时跟踪符号链接指向的文件。

5. -o ServerAliveInterval=秒数:指定SSH连接保持活动的时间间隔,单位为秒,默认为0,表示不发送保持活动的包。

6. -o IdentityFile=密钥文件路径:指定私钥文件的路径,用于SSH连接认证。

7. -o Port=端口号:指定远程服务器的SSH端口号,默认为22。

8. -o Ciphers=加密算法列表:指定使用的加密算法,多个算法之间用逗号分隔。

general options:
-o opt,[opt...] mount options
-h --help print help
-V --version print version
SSHFS options:
-p PORT equivalent to '-o port=PORT'
-C equivalent to '-o compression=yes' #启用压缩,建议配上
-F ssh_configfile specifies alternative ssh configuration file #使用非默认的ssh配置文件
-1 equivalent to '-o ssh_protocol=1' #不要用啊
-o reconnect reconnect to server #自动重连
-o delay_connect delay connection to server
-o sshfs_sync synchronous writes
-o no_readahead synchronous reads (no speculative readahead) #提前预读
-o sshfs_debug print some debugging information
-o cache=BOOL enable caching {yes,no} (default: yes) #能缓存目录结构之类的信息
-o cache_timeout=N sets timeout for caches in seconds (default: 20)
-o cache_X_timeout=N sets timeout for {stat,dir,link} cache
-o workaround=LIST colon separated list of workarounds
none no workarounds enabled
all all workarounds enabled
[no]rename fix renaming to existing file (default: off)
[no]nodelaysrv set nodelay tcp flag in sshd (default: off)
[no]truncate fix truncate for old servers (default: off)
[no]buflimit fix buffer fillup bug in server (default: on)
-o idmap=TYPE user/group ID mapping, possible types are: #文件权限uid/gid映射关系
none no translation of the ID space (default)
user only translate UID of connecting user
-o ssh_command=CMD execute CMD instead of 'ssh'
-o ssh_protocol=N ssh protocol to use (default: 2) #肯定要2的
-o sftp_server=SERV path to sftp server or subsystem (default: sftp)
-o directport=PORT directly connect to PORT bypassing ssh
-o transform_symlinks transform absolute symlinks to relative
-o follow_symlinks follow symlinks on the server
-o no_check_root don't check for existence of 'dir' on server
-o password_stdin read password from stdin (only for pam_mount)
-o SSHOPT=VAL ssh options (see man ssh_config)
Module options:
[subdir]
-o subdir=DIR prepend this directory to all paths (mandatory)
-o [no]rellinks transform absolute symlinks to relative
[iconv]
#字符集转换,对我这种UTF8控,默认已经是最好的
-o from_code=CHARSET original encoding of file names (default: UTF-8)
-o to_code=CHARSET new encoding of the file names (default: UTF-8)

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

声明:如果本站发布的内容侵犯到您的权益,请通过邮件【[email protected]】联系本站,我们将及时删除!

发表评论

表情 格式 链接 私密 签到