当前位置: 首页 > 工具软件 > OpenSSH > 使用案例 >

openssh服务

长孙兴德
2023-12-01

openssh

Openssh简介


OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。

知识延伸:
ssh协议有两个版本

v1:基于CRC-32 做MAC,不安全; (一般用于实现主机认证)
v2:基于协议协商选择双方都支持的最安全的MAC机制
基于DH做密钥交换,基于RSA或DSA实现身份认证,从而实现无需输入账号面
客户端通过检查服务器端的主机秘钥来判断是否能够继续通信;
 

ssh认证方式

  • 基于口令的认证
  • 基于密钥的认证

ssh的工作方式

openssh是基于C/S(客户端/服务器端)架构工作

服务器端    //sshd,配置文件在/etc/ssh/sshd_config
客户端     //ssh,配置文件在/etc/ssh/ssh_config   基本不会更改它
    ssh-keygen      //密钥生成器
    ssh-copy-id     //将公钥传输至远程服务器
    scp             //跨主机安全复制工具

客户端ssh的使用

sh的基本语法
ssh [OPTIONS] [user]@server [COMMAND]

-l user: 以指定用户身份连接至服务器;默认使用本地用户为远程登录时的用户;
ssh user@server
ssh -l user server

实例

[root@localhost ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendo>
   Active: active (running) since Sat 2021-09-25 21:52:18 CST; 9min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1118 (sshd)
    Tasks: 1 (limit: 23789)
   Memory: 8.3M
   CGroup: /system.slice/sshd.service
           └─1118 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,ch>


//连接主机192.168.75.143
[root@localhost ~]# ssh root@192.168.75.143   //指定root用户登录
The authenticity of host '192.168.75.143 (192.168.75.143)' can't be established.
ECDSA key fingerprint is SHA256:lC6LXM7tNoleYu/W/4y6661+LKk+K/eQPkTLxuZmvMQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes        //输入yes确认连接
Warning: Permanently added '192.168.75.143' (ECDSA) to the list of known hosts.
root@192.168.75.143's password:         //输入目标主机root密码
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: Sat Sep 25 21:59:40 2021 from 192.168.75.1
[root@mysql ~]# 

ssh密钥

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

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

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

//公钥信息存储在本地的~/.ssh/known_hosts中
[root@localhost ~]# cat .ssh/known_hosts
192.168.75.143 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFNR/Sku8SedH/1Y4qfhShsGzYRaDJA+Yz8AMf5L6ROJHNzfsnY/F4CoJK+e4Ob+ssizAa4YPSOdQkOXUNJJfpc=
[root@localhost ~]# 

//主机密钥存储在/etc/ssh/ssh_host_*_key中
[root@localhost ~]#  ls /etc/ssh/
moduli        ssh_host_ecdsa_key        ssh_host_rsa_key
ssh_config    ssh_host_ecdsa_key.pub    ssh_host_rsa_key.pub
ssh_config.d  ssh_host_ed25519_key
sshd_config   ssh_host_ed25519_key.pub

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

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

使用ssh-keygen命令生成密码。将会生成私钥 ~/.ssh/id_rsa 和公钥 ~/.ssh/id_rsa.pub 。

需要注意的是:

​ 生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。

​ 生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600和644。.ssh目录权限必须是700。
 

//生成密钥并用rsa算法加密
[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:PkiGMxxXUTu9cKDQlpz5AYeMjm3+7KlgIDCutekx5f8 root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|      .=+O+      |
|      .oXo.+     |
|o   .+....+.o    |
|o. ..++   .+ .   |
|..o *oo S   .    |
|.o * =.o         |
|. = + .oo        |
| . + o  oo       |
|  .   o+E        |
+----[SHA256]-----+

//将公钥传输到指定主机的位置
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.75.143
/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
root@192.168.75.143's password: 

Number of key(s) added: 1

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

//再连接便不需要密码
[root@localhost ~]# ls
anaconda-ks.cfg  apache  mail  mysql  yangyonghu.tar.gz
[root@localhost ~]# ssh root@192.168.75.143
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: Sat Sep 25 22:02:19 2021 from 192.168.75.142
[root@mysql ~]# 

scp

[root@localhost ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg
模板  图片  下载  桌面  initial-setup-ks.cfg
[root@client ~]# scp 192.168.220.9:/root/test/abc .
root@192.168.75.143's password:
yang                                                          100%   45    129KB/s   00:00
[root@localhost ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg          yang
模板  图片  下载  桌面  initial-setup-ks.cfg 

参数

//scp命令常用选项

-r //递归复制

-p //保持权限

-P //端口

-q //静默模式

-a //全部复制

自定义SSH服务配置

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

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

 类似资料: