ssh密码登陆
ssh连接常用命令
ssh username@address
username表示远程登陆的用户
address表示远程登陆的主机
登陆前确保地址可达和sshd服务是否开启
ssh服务端的配置文件目录
[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# ls
moduli ssh_config.d ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
ssh_config sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
默认ssh服务器在Linux系统,有3对非对称加密密钥
第一对
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
第二对
ssh_host_ed25519_key
ssh_host_ed25519_key.pub
第三对
ssh_host_rsa_key
ssh_host_rsa_key.pub
sshd_config是ssh服务端的配置文件
ssh_config是ssh客户端的配置文件
ssh配置文件里参数
文件/etc/ssh/sshd_config
#Port 22 ssh访问端口号,可以修改
#AddressFamily any
#ListenAddress 0.0.0.0 ssh监听的地址,0.0.0.0表示监听所有的ipv4地址的22端口
#ListenAddress ::
PermitRootLogin yes 是yes就表示允许root通过ssh登陆,no就拒绝root通过ssh远程登陆
PubkeyAuthentication yes 是yes就表示支持基于公钥的认证
AuthorizedKeysFile .ssh/authorized_keys 基于公钥开启后,客户端公钥存放位置
PasswordAuthentication yes 如果yes支持密码认证
修改完端口ssh连接加p参数指定端口
/var/log/secure记录了ssh登陆的日志
修改ssh配置文件要重启sshd服务
免密登陆
确定配置文件/etc/ssh/sshd_config是否支持基于公钥的认证
即PubkeyAuthentication yes
好处:基于公钥认证方式,防止黑客暴力破解
1.将客户端的公钥传输到服务端
2.客户端访问服务端时采用基于公钥的认证方式
操作
客户端ip:10.0.0.135
服务端ip:10.0.0.132
1.检查客户端和服务端sshd服务是否开启
systemctl status sshd
2.在客户端通过ssh-keygen生成非对称加密密钥
ssh-keygen
-t指定算法
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’. #存放默认位置
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:
SHA256:klRillPc48msPw1tZNuYqe1G32bHNS5TgaUhKPQ361o root@localhost.localdomain #指纹
The key’s randomart image is:
±–[RSA 2048]----+
| ==o… |
| oo+o.o. . . |
| …= =. = |
| . . * =o . |
| o S. = * .|
| … o O .o.|
| . E .ooo|
| * +o…*|
| . +. oo.|
±—[SHA256]-----+
[root@localhost ~]# cd .ssh
[root@localhost .ssh]# ls
id_rsa id_rsa.pub #生成的公钥私钥
3.使用ssh-copy-id上传到服务端
p指定端口 默认22
i认证文件
ssh-copy-id root@10.0.0.132
这里要输入服务端的用户密码
只有将公钥传到具体的家目录下才可以实现基于公钥的认证方式
传送到服务端
AuthorizedKeysFile .ssh/authorized_keys 基于公钥开启后,客户端公钥存放位置