sftp之秘钥认证

您所在的位置:网站首页 密钥服务不可用什么意思 sftp之秘钥认证

sftp之秘钥认证

2024-07-13 08:10| 来源: 网络整理| 查看: 265

1、概述

密钥登录无需用户设置密码,通过rsa密钥对加解密验证,在客户端和服务器端建立安全的连接,简单地说,public key放在服务器端,即下面配置的authorized_keys,private key放在客户端,客户端发起请求连接,服务器根据请求用户名识别对应客户端公钥,sshd服务产生一个随机数,用public key进行加密后,发回到客户端,客户端用private key解密得到该随机数,客户端将解密后的随机数发回服务器端,服务端进行匹配,匹配成功认证通过,允许登录。这种方式避免了密码暴力破解尝试的危险,当然密钥认证因为有加解密和随机数传输验证的过程,连接耗时自然比密码方式长些。

2、实现秘钥认证登录

远程服务器IP:192.168.191.128,主机名为catyuan 本地客户端IP:192.168.191.131,主机名为localhost

2.1 在本地创建密钥对

[root@localhost ~]# ssh-keygen -t rsa #创建秘钥对 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: bc:fa:ba:a0:70:41:50:64:3a:a4:99:41:d7:a9:44:fe [email protected] The key's randomart image is: +--[ RSA 2048]----+ |+==o. . | |oBo. o | |* o.. | | o .. . | | . E S | | . . | |. . . . | | o . . . | | . ++. | +-----------------+

2.2 查看目录下生成的公钥(id_rsa.pub)和私钥(id_rsa)

[root@localhost ~]# ls /root/.ssh/ id_rsa id_rsa.pub known_hosts

2.3 把公钥拷贝到远程服务器的~/.ssh/authorized_keys文件中

[root@localhost ~]# ssh-copy-id [email protected] This service allows sftp connections only. #服务器只允许sftp连接 [root@localhost ~]# ssh-copy-id [email protected] #先传到root下,再cp [root@catyuan ~]# mkdir /data/sftp/test/.ssh [root@catyuan ~]# cp .ssh/authorized_keys /data/sftp/test/.ssh/

几种拷贝方法

1、将公钥通过scp拷贝到服务器上,然后追加到~/.ssh/authorized_keys文件中,这种方式比较麻烦。scp -P 22~/.ssh/id_rsa.pub user@host:~/。 2、通过ssh-copy-id程序,就是我演示的方法,ssh-copy-id user@ip即可 3、可以通过cat ~/.ssh/id_rsa.pub | ssh -p 22 user@host ‘cat >>~/.ssh/authorized_keys’,这个也是比较常用的方法,因为可以更改端口号。

2.4 修改权限

.ssh目录的权限为700,其下文件authorized_keys和私钥的权限为600。否则会因为权限问题导致无法免密码登录。我们可以看到登陆后会有known_hosts文件生成。

[root@catyuan ~]# chown -R test:sftp /data/sftp/test/.ssh/ [root@catyuan ~]# chmod 600 /data/sftp/test/.ssh/authorized_keys [root@catyuan ~]# chmod 700 /data/sftp/test/.ssh/

2.5 修改配置文件

如果只允许用户使用密钥认证登录,可以射置/etc/ssh/sshd_config PasswordAuthentication no 如果不改,密钥认证和密钥认证两种登录方式都允许。

[root@catyuan ~]# vim /etc/ssh/sshd_config PermitRootLogin yes #允许root用户登录 RSAAuthentication yes #允许 RSA 安全验证 PubkeyAuthentication yes #允许密钥登录 AuthorizedKeysFile /data/sftp/test/.ssh/authorized_keys #指出公钥的位置,这一项配置文件中是有的,把它注释掉。

RSA安全认证 RSA中公钥被被第三方知道后,第三方用公钥加密发送数据; 例如: A要给B写信: 1.B给A发送公钥 2.A拿到公钥加密信内容,发送内容给B 3.B拿到A的信后用私钥解密。 那么问题来了: 问题一:在B给A公钥时,被第三方C知道了,那么C是不是也可以给B写信了?? 问题二:A怎么知道这个钥匙是B给的?? 问题三:B怎么知道信是A写的??

2.6 重启服务

[root@catyuan ~]# systemctl restart sshd`

2.7 测试用密钥登录

[root@localhost ~]# sftp [email protected] Connected to 192.168.191.128. #此时登录就无需密码了,使用秘钥登录 sftp>


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3