OpenSSH简介
OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。
OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。
SSH认证方式
OpenSSH有两种认证方式:
- 基于口令认证
- 基于密钥认证
OpenSSH的工作模式
openssh是基于C/S架构工作的
服务端 #sshd,配置文件在/etc/ssh/sshd_config
客户端 #ssh,配置文件在/etc/ssh/ssh_config
ssh-keygen # 密钥生成器
ssh-copy-id # 将公钥传输至远程服务器
scp # 跨主机安全复制工具
Xshell示例
ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。
//以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@localhost ~]# ssh 192.168.8.131
root@172.16.12.138's password:
Last login: Tue Jul 10 07:34:03 2018 from 192.168.8.130
[root@localhost ~]# exit
logout
Connection to 192.168.8.131 closed.
//以其他用户身份(remoteuser)在选定主机(remotehost)上连接到远程`shell`
[root@localhost ~]# ssh tom@192.168.8.131
user1@172.16.12.138's password:
[user1@localhost ~]$ exit
logout
Connection to 192.168.8.131 closed.
SSH主机密钥
ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。
# 主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
[root@localhost ~]# cat ~/.ssh/known_hosts
192.168.8.130 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHpBYg+C0GDiBU9mHsy8S3ju31OdfTq6cr6oprIsE/MM8yZdTrRh4gum8IXiVFchUelPD5R9IuTjsy8Eqy8l+Lc=
# 主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中
[root@localhost ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_rsa_key.pub
配置基于SSH密钥的身份验证
用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
使用ssh-keygen命令生成密码。将会生成私钥/.ssh/id_rsa和公钥/.ssh/id_rsa.pub。
生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600和644。.ssh目录权限必须是700。
在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id完成这一操作
[root@localhost ~]# ssh-copy-id remoteuser@remotehost
//SSH密钥演示
//使用 ssh-keygen 创建公钥-私钥对,注意是在客户端上操作服务端
[root@localhost ~]# ls .ssh/
known_hosts
[root@localhost ~]#
[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:
SHA256:r7W+GRm+iENQiRK+5EJLVEqWSwib2oVXSqvEGXnlDJk root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|++=+o=o. |
|=B+=EBo |
|++Oo=.o |
|+*.=. |
|o.= . S . |
| . . o o |
| . * |
| .. + = |
| ..o.*. |
+----[SHA256]-----+
//使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@localhost ~]# ssh-copy-id root@192.168.8.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.8.131 (192.168.8.130)' can't be established.
ECDSA key fingerprint is SHA256:NnjR3bU+A025xWLGs+c20YgVhNG3IBRGLKdDdMrW/Zw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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@192.168.8.130's password:
Number of key(s) added: 1 //输入密码
Now try logging into the machine, with: "ssh 'root@192.168.8.130'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
# 使用 ssh 命令无命令登录远程主机
[root@localhost ~]# ssh root@192.168.8.131
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Tue Sep 14 15:46:18 2021
# 使用scp命令传输文件
[root@localhost ~]# scp test.sh root@192.168.8.131:/tmp
root@192.168.8.131's password:
test 100% 45 29.8KB/s 00:00
# 使用scp命令从远程主机上下载文件到本地
[root@localhost ~]# scp root@192.168.8.131:/tmp/test .
root@192.168.8.131's password:
test.sh 100% 45 39.1KB/s 00:00
[root@localhost ~]# ls
anaconda-ks.cfg test
# scp命令常用选项
-r #递归复制
-p #保持权限
-P #端口
-q #静默模式
-a #全部复制
SSH安全注意事项
- 密码复杂性
[root@localhost ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs //生成30位的密码
LYH9cbirdT6E_hbColMFjZNf9Kd6If
[root@localhost ~]# openssl rand 20 -base64
Di9ry+dyV40xVvBHirsc3XpBOzg= //生成20位随机密码
- 使用非默认端口
- 限制登录客户端地址
- 仅监听特定的IP地址
- 禁止管理员直接登录
- 仅允许有限制用户登录
- AllowUsers
- AllowGroups
- 使用基于密钥的认证
- 禁止使用空密码
- 禁止使用SSHv1版本
- 设定空闲会话超时时长
- 利用防火墙设置ssh访问策略
- 限制ssh的访问频度和并发在线数
- 做好日志的备份,经常分析(集中于某台服务器)
本文介绍OpenSSH的基本概念、工作原理及安全配置。涵盖SSH认证方式、密钥管理和基于密钥的身份验证步骤。同时强调了SSH的安全注意事项,如密码复杂性和使用非默认端口等。
865

被折叠的 条评论
为什么被折叠?



