SSH是一套网络协议,目的在于安全的网络服务与加密远程登录。
实现SSH协议的最主流的开源软件是OpenSSH
Secure Shell是Linux系统首选的登录方式,数据加密,安全传输,主要方式有两种:
对称加密(秘钥加密)只有一个秘钥
非对称加密(公钥加密)有一对秘钥
非对称加密分为:公钥(Public Key)与私钥(Private Key)
使用公钥加密后的密文,只能使用对应的私钥才能解开,破解的可能性很低
此种方式是公钥私钥放在Server端,即使client在登录的时候,传输数据被窃取,黑客也没有私钥进行解密,因此保证了数据安全。
公钥理解为一把锁,私钥是其对应的钥匙,公钥针对密码加密,私钥放在Server端
登录Linux服务器的形式
①基于口令,账号密码的登录形式
②基于公钥,实现免密登录
一、【基于口令验证】
在用xshell初次登录某虚拟机时:ssh root@192.168.178.110(可以直接输密码登录,也可以如下步骤验证远程秘钥之后再输入密码登录)
会提示
The authenticity of host 'pyyuc (192.168.178.110)' can't be established. ECDSA key fingerprint is SHA256:CVwhwfUiD53HpLPrretR4pGltYRL6QB+5lyI. Are you sure you want to continue connecting (yes/no)? yes
这一段ssh连接的信息,表述的是无法确认192.168.178.110这台机器的真实性
,但是知道了这台机器的指纹,SHA256:CVwhwfUiD53HpLPrretR4pGltYRL6QB+5lyI
,让用户自行确认是否登录
这时候就要验证远程 主机指纹是否正确
远程扫描server指纹信息
$ssh-keyscan -t ecdsa 192.168.178.110 | ssh-keygen -lf -
#192.168.178.110 SSH-2.0-OpenSSH_7.4 256
SHA256:CVwhwfUkaLPrretR4pGltYRL6QB+5lyI 123.206.16.61 (ECDSA)
系统此时应该会返回一句信息
Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.178.110' (ECDSA) to the list of known hosts. root@192.168.178.110's password:
此时表示Client已经确认Server的身份,准备开始连接,输入Server机器的密码
,如果密码正确,则可以登录。
二、【基于公钥认证】--免密登录
公钥登录流程如下
- client发送自己的
公钥
给server,写入server的authorized_keys
中 - server端接收到client的连接请求后,在自己的
authorized_keys
文件中匹配client的公钥信息pubkey
,并且生成一个随机数R
,使用client的公钥pibkey
针对该随机数R
进行加密,得到一个加密后的随机数pubkey(R) - client通过私钥进行解密得到
随机数R
,再对随机数R和当前会话的sessionkey采用MD5生成摘要Digest1,再发送给server端 - server端会对随机数R和当前client的sessionkey用同样摘要算法生成Digest2
- 结果比较client发来的Digest1与Digest2是否一致,正确则完成认证
配置SSH公钥认证
1.client本地生成公私钥
[root@linux .ssh]# ssh-keygen -t rsa #指定rsa密钥类型,默认一路回车 #会生成如下的公私钥 Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub.
2.client发送自己的公私钥到server中
#发送自己的公钥,写入到远端server的authorized_keys中 $ssh-copy-id root@192.168.178.110 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/yuchao/.ssh/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.178.1101's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.178.110'" and check to make sure that only the key(s) you wanted were added.
3.此时可以免密登录了
$ssh root@192.168.178.110 #直接输入登录命令即可 Last failed login: Fri Jan 3 16:54:46 CST 2020 from 189.39.13.1 on ssh:notty There were 3 failed login attempts since the last successful login. Last login: Fri Jan 3 16:14:59 2020 from 222.35.146.118 [root@linux ~]# [root@linux ~]# cat ~/.ssh/authorized_keys #检查server中的秘钥 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbkHjqnRe31HCteHc0BSfovvs9GqutyBfWvAJQy51Std1Ir3qBnHs29aKjwPL/jm/xH
[root@linux ~]# exit #登出
linux$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbkHjqnRe31HCteHc0BSfovvs9GqutyBfWvAJQy51Std1Ir3qBnHs29aKjwPL/jm/xH
【ssh配置文件】
和Linux用户有关ssh的配置文件,存放在此路径:$HOME/.ssh/
(没有/root/.ssh的话配置一次免密登录就可以
)
[root@ .ssh]# pwd
/root/.ssh
[root@ .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
- Known_hosts:当Client接收Server的公钥以后,Server的公钥信息会放在Client
$HOME/.ssh/known_hosts文件中
,下次再次连接的时候,系统能够识别出Server的公钥已经存在了本地,因此可以跳过警告部分,直接提示输入密码了 - authorized_keys:Server远程主机将
用户的公钥
,保存在已登录用户的$HOME/.ssh/authorized_keys
文件中。(用于客户端免密登录使用) - id_rsa:私钥文件
- id_rsa.pub:公钥文件
三、ssh与服务器安全实战
和Linux用户有关ssh的配置文件,存放在此路径:$HOME/.ssh/
ssh服务的配置文件存放于/etc/ssh/sshd_config
# grep -Ev '^$|^[# ]' /etc/ssh/sshd_config(config内容太多,用grep过滤一下)
在生产服务器下,运维人员一般会禁止root用户登录服务器,最大程度的保证服务器的安全,被黑客攻击的几率,以及修改ssh的远程连接端口
修改ssh的端口 ,Port 23354
禁止root登录,PermitRootLogin no
禁止用密码登录,只能用被信任的机器,用公私钥进行登录,PasswordAuthentication no
1、vim 编辑sshd_config文件以下内容
Port 23354
AddressFamily any
ListenAddress 0.0.0.0
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
PermitRootLogin no
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
ChallengeResponseAuthentication no
GSSAPIAuthentication no
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UseDNS no
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
注意了,此时别立即重启服务
2、配置一个普通用户的账号,且支持公钥登录的形式
①登录服务器,创建普通用户,设置登录密码
useradd yu
passwd yu
②在自己本地机器,生成一个普通用户的公私钥对
ssh-keygen -t rsa
③发送公钥给服务器,配置公钥登录
ssh-copy-id yu@192.168.178.110
④在正确配置了公私钥登录之后,yu这个用户就可以免密登录linux服务器了
ssh yu@192.168.178.110
3、在linux机器上配置yu用户支持sudo命令
①使用root登录服务器,配置yu用户支持sudo命令
vim /etc/sudoers文件
添加如下行
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
yu ALL=(ALL) ALL
②此时尝试用yu用户登录,是否能够使用sudo命令
sudu ls /root
4、使用root账号,重启linux的sshd服务器
以后root用户就无法使用密码登录了,只能用yuchao这个用户进行免密登录,最大程度保证服务器的安全了
①使用root用户重启sshd服务
ssh root@192.168.178.142
②重启sshd服务
systemctl restart sshd
③此时机器已经禁止root登录,禁止密码登录,且修改了ssh端口为23354
④此时只能使用配置好的yu用户进行免密登录了
ssh yu@192.168.178.142 -p 23354