1.验证ssh远程登录,未作免密处理的两台机器,登录时,是需要输入密码的
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ssh root@192.168.0.88
root@192.168.0.88's password:
Last failed login: Sun Sep 29 10:24:16 CST 2019 from 192.168.0.6 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Sun Sep 29 10:13:06 2019 from 192.168.0.6
[root@gateway ~]#
2.本地系统执行 ssh-keygen -t rsa 命令,生成密钥文件。一直按回车键,会出现如上图,也会有你的公私钥文件的路径,比如我的:/home/lx-yf/.ssh/id_rsa.pub
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yanzuoguang/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yanzuoguang/.ssh/id_rsa.
Your public key has been saved in /home/yanzuoguang/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:S57t6DKJl0T1oyBzxi/dduJaPCNKt1skReilf2q76+c yanzuoguang@yanzuoguang-linux-1
The key's randomart image is:
+---[RSA 2048]----+
| .. |
| o.. |
| . o +. |
| o * o.o |
| * +S+.. |
| oo+B= o |
| o.+*oB= |
| ..*o B+o. |
| ..+*=**E |
+----[SHA256]-----+
3.在相应的目录下/home/lx-yf/.ssh/ 查看生成的密钥文件,其中:id_rsa为私钥文件,id_rsa.pub为公钥文件
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ls
id_rsa id_rsa.pub known_hosts
4.本地机器执行命令如: ssh-copy-id -i id_rsa.pub root@192.168.0.88, 将公钥文件传输的远程机器,并生效,期间会让你再次输一次你的密码,如下图,输入一下 就好
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ssh-copy-id -i id_rsa.pub root@192.168.0.88
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "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.88's password:
# 完成标志如下,本行为注释
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.0.88'"
and check to make sure that only the key(s) you wanted were added.
widdows下等效命令,当前用户.ssh目录
cd .ssh
type id_rsa.pub | ssh root@192.168.0.88 “cat >> .ssh/authorized_keys”
5.再次使用已经做免密处理的用户登录远程机器,已经不需要密码了,免密登录处理完成。
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ssh root@192.168.0.88
Last login: Sun Sep 29 10:24:27 2019 from 192.168.0.6
[root@gateway ~]#
7.查看远程机器的authorized_keys文件,可以看到对应的变化:本地机器的公钥已经增加到远程机器的配置文件中了
yanzuoguang@yanzuoguang-linux-1:~/.ssh$ ssh root@192.168.0.88
Last login: Sun Sep 29 10:24:27 2019 from 192.168.0.6
[root@gateway ~]# cd .ssh
[root@gateway .ssh]# ls
authorized_keys known_hosts
[root@gateway .ssh]# pwd
/root/.ssh
[root@gateway .ssh]# ls
authorized_keys known_hosts
[root@gateway .ssh]# cat authorized_keys
# authorized_keys的文件内容
8.注意事项
- 免密码登录的处理是用户对用户的,切换其他用户后,仍然需要输入密码。
- 公钥传到远程机器并生效的操作,可用其他方式实现,如scp后修改authorized_keys,
- 远程机器的.ssh目录需要700权限,authorized_keys文件需要600权限
9.ssh 与远程机器保持心跳(linux)
文件/etc/ssh/ssh_config末尾加上,注意前面的空格:
TCPKeepAlive yes
ServerAliveInterval 30
10.sshfs基于ssh挂载远程目录
为了像本地一样访问远程主机上的目录,通常我们会在远程主机上使用nfs来导出目录,并在本地主机上mount这个nfs文件系统。如果是windows系统,则使用cifs或samba的方式来访问。但可能我们忽略了一个远程连接最通用的工具:ssh。其实很多和远程有关的行为,基于ssh都能完成,即使是实现像NFS一样的功能。如何通过ssh来挂载远程目录?需要安装fuse-sshfs
包,这个包在epel中提供。使用fuse-sshfs包提供的sshfs工具可以基于ssh直接挂载远程目录,不用像NFS一样还要export。
在Ubuntu上安装sshfs
SSHFS是基于Linux的软件,需要先安装在本地计算机上才能进行挂载,在基于Ubuntu和Debian的系统上,可以使用apt-get软件包管理器进行安装:
$ sudo apt-get install sshfs
无需配置,安装后你将设置为开始使用sshfs。
在Arch上安装sshfs
在Arch上安装SSHFS是使用pacman包管理器或AUR帮助程序,如yaourt和pacaur。
$ sudo pacman -S sshfs
参考:安装及使用Arch Linux/Manjaro系统中的AUR助手yay。
在Fedora和CentOS上安装sshfs
在基于Red Hat的系统上,sshfs软件包在默认存储库中不可用,你必须首先安装epel然后从它安装sshfs包:
$ sudo yum -y install epel-release
$ sudo yum makecache fast
$ sudo dnf -y install sshfs
对于CentOS系统:
$ sudo yum -y install sshfs
使用sshfs在ssh上挂载远程文件系统
为了能够在本地在远程服务器上安装目录,SSH用户需要能够访问它,参考在Linux系统中允许或拒绝SSH访问特定用户或组的方法,确认用户可以访问远程系统后,使用以下语法完成文件系统的安装:
$ sshfs [user@]host:[dir] mountpoint [options]
一些常用的选项是:
- -p PORT等效于'-o port=PORT' - >指定远程系统的ssh端口号。
- -C相当于'-o compression=yes' - >启用压缩。
- -F ssh_configfile指定备用ssh配置文件 - >使用与默认配置文件不同的ssh配置文件,-o用于指定安装选项。
因此,让我们考虑一个带有ssh端口2022的服务器,以及用于挂载/data/backups的远程位置,要在打开压缩的情况下安装它,我们将使用:
$ mkdir /home/jmutai/backups
$ sshfs outboundmx-02:/data/backups -p 2022 -C /home/jmutai/backups
确认是否成功安装:
$ df -hT | grep fuse.sshfs
outboundmx-02:/data/backups fuse.sshfs 17G 5.7G 12G 33% /home/jmutai/backups
现在,你可以开始使用计算机上的文件,就好像它是你实际连接的远程服务器一样,如果导航到/home/jmutai/backups目录,则可以在本地创建文件或目录,它将显示在远程服务器上,你复制到安装目录的任何文件也将自动上载到远程服务器:
$ cd /home/jmutai/backups
$ touch testfile{1..3}.txt
$ ls
testfile1.txt testfile2.txt testfile3.txt
检查文件是否已上载到远程服务器:
$ ssh outboundmx-02 -p 2022 "ls /data/backups"
testfile1.txt
testfile2.txt
testfile3.txt
永久安装远程文件系统
SSHFS允许你将永久挂载点设置为远程文件系统,该系统通过系统重新启动(本地/远程)持续存在,要设置永久挂载点,需要修改/etc/fstab文件以在每次引导系统时自动挂载文件系统,语法是:
USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs defaults,_netdev 0 0
所以在我们的例子中,如:
jmutai@outboundmx-02:/data/backups /home/jmutai/backups use.sshfs defaults,_netdev 0 0
相关选项摘要:
- allow_other - 允许除安装程序(即root)之外的其他用户访问共享。
- default_permissions - 允许内核检查权限,即使用远程文件系统上的实际权限,这允许禁止访问allow_other批准的所有人。
- uid,gid - 将文件的所有权报告给定值,uid是用户的数字用户ID,gid是用户的数字组ID。
要测试此设置,请先卸载/home/jmutai/backups,然后通过/etc/fstab文件挂载:
$ sudo umount /home/jmutai/backups
$ sudo mount -a
卸载
要卸载远程系统:
$ fusermount3 -u mountpoint
要么:
$ sudo umount mountpoint
结语
以上就是安装及使用sshfs的方法,本地安装的文件系统的行为类似于远程存储,你可以对其执行所有文件系统操作,包括文件创建、删除、编辑、移动、复制、压缩、解压缩等,这在处理需要定期更新/批量的项目时很有用,复制到远程服务器,它比每次需要执行数据同步时运行scp/sftp更有效率。
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2022/08/28 14:37