ssh原理
ssh是一种协议标准,其目的是实现安全远程登录以及其它安全网络服务。
SSH仅仅是一协议标准,其具体的实现有很多,既有开源实现的OpenSSH,也有商业实现方案。使用范围最广泛的当然是开源实现OpenSSH。
加密的方式主要有两种:
- 对称加密(也称为秘钥加密)
- 非对称加密(也称公钥加密)
【对称加密】
【非对称加密】
原理请自行百度
【中间人攻击】
1、简单来说,就是黑客拦截了来自客户端的登录请求
2、将自己的公钥发送给客户端,客户并不知道这是假的公钥,于是用了此公钥对数据进行加密
3、客户端发送假的公钥进行加密的数据,黑客拿到数据后,再用自己的私钥进行解密
4、客户端的数据被黑客截取
如何确保数据发送给正确的server端?
【基于口令的验证】
SSH的公私钥都是基于本地生成的,没法公认,因此只能通过Client端自行对公钥确认。
C:\Users\XXX>ssh-keyscan 10.0.0.10
# 10.0.0.10:22 SSH-2.0-OpenSSH_7.4
10.0.0.10 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmjoqH/NzyH9ojAu31zISd5ZYmITMHcgYvIyqZkrq1HvNS1tn4mmgNuMU1+eQPWVrhxyh3L2M09K9VGdyR1PupYxn3Dpw3qbrysrYhEsFg48KjmV9FaxXx2suX78gIFAg+1DUCnJlavGkSEc+xolhmT1o5taKN/rdz/bVcUYwbln13vYf65vPxdaSJmgufa68Z+owcMslnW2NZqxIEMMVp5T6fUYZe7OPacTp5A35eWF15tLaK/maWtlYY1rlOXKepzYiPBhG437n8vww1Cgu6ZHc5IEsBEnEyZwrK4KEKIOeRPiOmRgbVXL+UzAT9W9Q0yMKzRaiJRqMrLHI6bK2P
# 10.0.0.10:22 SSH-2.0-OpenSSH_7.4
10.0.0.10 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMNECaIxl99bfBPlMFia6nTVmes823a5T+uSfuBr9pb35TczFcqI4do82vMHM/ATYAgf1RT+9Lf+lZM0jvPPqNo=
# 10.0.0.10:22 SSH-2.0-OpenSSH_7.4
10.0.0.10 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB5nB33Uv/9B6+lFZX0xNVpXVJDB3v5FA3TQhe3uP+ha
对比10.0.0.10本地机器的指纹信息
[root@junwu_server ~]# cat /etc/ssh/ssh_host_ecdsa_key.pub
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMNECaIxl99bfBPlMFia6nTVmes823a5T+uSfuBr9pb35TczFcqI4do82vMHM/ATYAgf1RT+9Lf+lZM0jvPPqNo=
确认server端身份后,输入yes连接
Are you sure you want to continue connecting (yes/no)? yes
系统此时应该会返回⼀句信息
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.10' (ECDSA) to the list o
root@10.0.0.10's password:
此时Client端确认Server的身份后,输入密码连接
【ssh配置文件$HMOE/.ssh/】
[root@junwu_server .ssh]# cd /$HOME/.ssh
[root@junwu_server .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:公钥⽂件
输⼊正确的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公钥认证
[root@localhost ~]# ssh-keygen -t rsa
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:jHi1iiJqg6yaNULUii3HJKo0dDx9jFCE6Jw7ehwuCeU root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ..+o |
| .o.o o |
|+oo= o o. |
|=O+ ...+ . |
|+B= . o S |
|==E o . |
|O+=.. . |
|*O+o |
|Oo. |
+----[SHA256]-----+
2.client发送⾃⼰的公私钥到server中
[root@localhost ~]# ssh-copy-id root@10.0.0.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.10 (10.0.0.10)' can't be established.
ECDSA key fingerprint is SHA256:/cKVB57qwusrra2xTm2LJ2jJm9eti6f3eENPL0pMEh0.
ECDSA key fingerprint is MD5:c7:0f:53:49:87:97:dc:c5:d2:ca:63:73:0f:7c:7d:fd.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
/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@10.0.0.10's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.10'"
and check to make sure that only the key(s) you wanted were added.
3.此时可以免密登录了
【SSH配置文件 /etc/ssh/sshd_config】
[root@junwu_server ~]# grep -Ev '^$|^[# ]' /etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
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
对于centos7系列,默认以及安装且启⽤了SSHD服务,可以直接使⽤ssh
命令登录服务器,对于⽣产环境,服务器⼀般禁⽌root管理员直接登录服务器,可以⼤⼤降
低被⿊客暴⼒破解的⼏率,需要修改sshd的主配置⽂件,修改如下参数
PermitRootLogin no #禁⽌root直接登录,使⽤普通⽤户登录安全性较⾼
注意的是,修改了配置⽂件,重启服务⽅可⽣效
systemctl restart sshd
systemctl enable sshd
以后直接⽤root登录会被禁⽌,应该使⽤普通⽤户登录后,sudo执⾏命令
C:\Users\罗俊武>ssh root@10.0.0.10
root@10.0.0.10's password:
Permission denied, please try again.
在此之前
[root@junwu_server ~]# useradd junwu
[root@junwu_server ~]# passwd junwu #设置用户密码
【SSH与服务器安全实战】注意:win10上无法直接使用ssh-copy-id命令
Linux强调⼀切皆⽂件,Linux系统更改各种软件的配置参数,也就是在修改⽂件内容⽽已。
[root@junwu_server .ssh]# grep -Ev '^$|^[# ]' /etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication no
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UseDNS no
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETAR
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMEN
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
在⽣产服务器下,运维⼈员⼀般会禁⽌root⽤户登录服务器,最⼤程度的保证服务器的安全,被⿊客攻击的⼏率,
以及修改ssh的远程连接端⼝
修改ssh的端⼝ ,Port 23354
禁⽌root登录,PermitRootLogin no
禁⽌⽤密码登录,只能⽤被信任的机器,⽤公私钥进⾏登录,
PasswordAuthentication no
修改/etc/ssh/sshd_config ⽂件的如下参数,此时⼀个安全的ssh服务器以及配
[root@junwu_server .ssh]# grep -Ev '^$|^[# ]' /etc/ssh/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_MONETAR
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMEN
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
注意了,此时别⽴即重启服务
配置⼀个普通⽤户的账号,且⽀持公钥登录的形式
1.登录服务器,创建普通⽤户,设置登录密码
useradd junwu
passwd junwu
2.在⾃⼰本地机器,⽣成⼀个普通⽤户的公私钥对
ssh-keygen -t rsa
3.发送公钥给服务器,配置公钥登录
ssh-copy-id junwu@10.0.0.10
4.在正确配置了公私钥登录之后,junwu这个⽤户就可以免密登录linux服务
ssh junwu@10.0.0.10
在linux机器上配置junwu⽤户⽀持sudo命令
1.使⽤root登录服务器,配置junwu⽤户⽀持sudo命令
vim /etc/sudoers⽂件
添加如下⾏
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
junwu ALL=(ALL) ALL
2.此时尝试⽤junwu⽤户登录,是否能够使⽤sudo命令
到了最后⼀步了,使⽤root账号,重启linux的sshd服务器,以后root⽤户
就⽆法使⽤密码登录了,只能⽤junwu这个⽤户进⾏免密登录,最⼤程度
保证服务器的安全了
1.使⽤root⽤户重启sshd服务
ssh root@192.168.178.142
2.重启sshd服务
systemctl restart sshd
3.此时机器已经禁⽌root登录,禁⽌密码登录,且修改了ssh端⼝为23354
4.此时只能使⽤配置好的junwu⽤户进⾏免密登录了
ssh junwu@10.0.0.10 -p 23354
标签:实战,LC,登录,ssh,key,服务器,root,junwu From: https://www.cnblogs.com/junwured/p/16917007.html