openssh服务

openssh服务

1.OpenSSH 简介

OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。

2.SSH 主机密钥

ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。

当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的-/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比~/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。

这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。

[root@psr1 ~]# cat ~/.ssh/known_hosts
192.168.56.129 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHsTpLzcgsCrE7WewNJE0XGf4/8WH0qIKg6cObgOlc7U=
[root@psr1 ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key      /etc/ssh/ssh_host_ed25519_key      /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ecdsa_key.pub  /etc/ssh/ssh_host_ed25519_key.pub  /etc/ssh/ssh_host_rsa_key.pub
[root@psr1 ~]# 

3.配置基于 SSH 密钥的身份验证

用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。

第一步生成密钥

[root@psr1 ~]# 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:
SHA256:Qgzi4yEopNYNWMLAA2tIcO8L4cW16fIdzOIQYU8/RAM root@psr1
The key's randomart image is:
+---[RSA 2048]----+
|X+=o+ Eo+        |
|*Oo=oB = .       |
|Bo*.=.* o        |
|++ * + o .       |
|  + + + S        |
|   . * + .       |
|    . o .        |
|                 |
|                 |
+----[SHA256]-----+

执行完之后,/root/.ssh目录下会产生两个文件。如下:

[root@psr1 ~]# cd /root/.ssh/
[root@psr1 .ssh]# pwd
/root/.ssh
[root@psr1 .ssh]# ls -lrt
总用量 12
-rw-r--r--. 1 root root  176 1月   7 15:18 known_hosts
-rw-r--r--. 1 root root  391 1月   7 16:02 id_rsa.pub
-rw-------. 1 root root 1679 1月   7 16:02 id_rsa

第二步将将密钥拷贝至目标主机192.168.56.129

[root@psr1 .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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
[email protected]'s password:
 
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

此时再输入 ssh 192.168.56.129 就不需要密码了

[root@psr1 .ssh]# ssh 192.168.56.129
Last login: Mon Jan  7 15:25:52 2019 from 192.168.56.128[root@psr2 ~]#

4.自定义 SSH 服务配置

虽然OpenSSH服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config中修改OpenSSH服务器的各个方面。

PermitRootLogin {yes|no}    //是否允许root用户远程登录系统
PermitRootLogin without-password    //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no}     //是否启用密码身份验证,默认开启

5.SSH 安全注意事项

密码应该经常换且足够复杂

[root@psr1 ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs 
dsC0QAKcs4zM0sF2IeLLpAaDcNra5e
[root@psr1 ~]# openssl rand 20 -base64
2axRYsVsGQC+oXpwkkReyzVOi0w= /生成20位随机密码

6.如何限制IP和指定用户,通过SSH登陆linux服务器

方法一

在/etc/hosts.allow中添加允许ssh登陆的ip或者网段 (以下上面的表示允许192.168.56.129地址登录,下面的表示允许192.168.56.0/24整个地址段的IP地址登录)

sshd:192.168.56.129:allow 或者
sshd:192.168.56.129/24:allow
在/etc/hosts.deny添加不允许ssh登陆的IP
sshd:ALL #ALL表示除了上面允许的,其他的ip 都拒绝登陆ssh
方法二
使用iptables

[root@psr1 ~]# iptables -A INPUT -p tcp -s 192.168.56.129 --destination-port 22 -j ACCEPT
[root@psr1 ~]# iptables -A INPUT -p tcp --destination-port 22 -j DROP 
[root@psr1 ~]# 

方法三
修改ssh配置文件

[root@psr1 ~]# vi /etc/ssh/sshd_config
添加
allowusers [email protected]

猜你喜欢

转载自blog.csdn.net/weixin_44432958/article/details/86015196