Linux的ssh学习与配置(SSH的登录)

远程连接服务器(VMware)

        远程连接服务器是通过文字或图形接口方式来远程登录系统,在远程终端控制窗口登录linux主机以取得可操作主机接口(shell),而远程连接服务器拥有分享主机的运算能力的功能。

        远程连接服务器的类型(以登录的连接界面来分类),有文字接口和图形接口。文字接口又分为:明文传输(Telnet、RSH等,目前非常少用)和(加密传输:SSH为主,已经取代明文传输)。而我们使用的xshell则是基于:SSH(Secure Shell Protocol,安全的壳程序协议)它可以通过数据包加密技术将等待传输的数据包加密后再传输到网络上。

        ssh协议本身提供两个服务器功能:一个是类似telnet的远程连接使用shell的服务器;另一个就是类似ftp服务的sftp-server,提供更安全的ftp服务。

        目前常见的网络数据包加密技术通常是通过“非对称密钥系统”来处理的。主要通过两把不一样的公钥与私钥来进行加密与解密的过程。

         公钥(public key):提供给远程主机进行数据加密的行为,所有人都可获得你的公钥来将数据加密。(公开,用来加密)

          私钥(private key):远程主机使用你的公钥加密的数据,在本地端就能够使用私钥来进行解密。而私钥只有自己拥有。(私密,用来解密)

 SSH的工作流程:

​       ①版本号协商:

         ssh1还是ssh2(SSH有ssh1和ssh2两个版本)

        此时客户端向服务端发送TCP请求连接(三次握手),在连接建立以后。服务器发送一个支持的版本报文。客户端收到后,解析并回复自己可以支持的版本报文 。协商成功,则进入密钥算法协商环节,否则则断开连接。

​       ②密钥和算法协商:

        ssh支持多种加密算法,在本地端和shell端支持的加密算法可能不同,所以协商一种双方都支持的加密算法
         服务端和客户端分别发送算法协商报文给对方,报文中包含自己支持的公钥算法列表、加密算法列表、消息验证码算法列表、压缩算法列表等。
        服务端和客户端根据对方和自己支持的算法得出最终使用的算法。
        服务端和客户端利用DH交换算法主机密钥对等参数,生成会话密钥和会话ID
假设:c公—>客户端公钥;c密—>客户端密钥;s公—>服务端公钥;s密—>服务端密钥
        在版本号协商阶段完成后:服务端将 s公 发送给客户端。服务端生成会话ID ,设为 id ,发送给客户端。客户端生成会话密钥,设为 key ,并计算 res = id 异或 key。客户端将 res 用 s公 进行加密,将结果发送给服务端。服务端用 s密 进行解密,得到 res。服务器计算 res 异或 id,得到 key。
        至此服务端和客户端都知道了会话密钥和会话ID,以后的数据传输都使用会话密钥进行加密和解密。

​      ③认证阶段:

        客户端请求服务器认证服务器对客户端进行认证

        SSH提供基于口令的认证(password认证)和基于密钥认证(publickey认证

        口令认证(用户名和密码认证):

                客户端将用户名和密码加密发送给服务端,服务端通过解密得到的信息和设备上保存的信息进行比对,返回认证成功/失败信息)

        密钥认证:

                客户端产生一对公共密钥,将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorized_keys文件中,认证时,客户端将公钥传给服务器。服务器收到后进行比对(与本地该账号家目录下的authorized_keys中的公钥进行对比)不相同则认证失败,相同则服务端生成一段随机字符串,并先后用客户端公钥和会话密钥对其加密,发送给客户端。客户端收到后将解密后的随机字符串用会话密钥发送给服务器。如果发回的字符串与服务器端之前生成的一样,则认证通过,否则,认证失败。

​      ④会话请求:

        认证通过后,客户端向服务器端发送会话请求

​      ⑤交互会话:

        会话请求通过后,服务器端和客户端进行信息的交互

公钥密钥信息:(配对过程)

配置文件:

//密钥的登录信任文件在~/.ssh下(这个文档内是可信任文件,登录过的用户)
[root@localhost .ssh]# find  / -name known*
/root/.ssh/known_hosts

//如果第一次登录则会去询问是否加入已知密钥(之后不会询问次ip):
[root@localhost ssh]# ssh root@192.168.220.234
The authenticity of host '192.168.220.234 (192.168.220.234)' can't be established.
ECDSA key fingerprint is SHA256:GHgk+fS5qfsLIwLedFAoPM4kMJrT8gaJEJGFWPWLIRM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.234' (ECDSA) to the list of known hosts.
root@192.168.220.234's password: 
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 Dec 21 06:13:05 2021 from 192.168.220.1
[root@lxb ~]# exit

//允许密钥的配置产生文件是在/etc/ssh/下
[root@lxb ssh]# ll
总用量 600
-rw-r--r--. 1 root root     577388 1月   8 2020 moduli
-rw-r--r--. 1 root root       1716 1月   8 2020 ssh_config
drwxr-xr-x. 2 root root         28 12月 14 14:57 ssh_config.d
-rw-------. 1 root root       4425 1月   8 2020 sshd_config
-rw-r-----. 1 root ssh_keys    480 12月 14 15:10 ssh_host_ecdsa_key
-rw-r--r--. 1 root root        162 12月 14 15:10 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys    387 12月 14 15:10 ssh_host_ed25519_key
-rw-r--r--. 1 root root         82 12月 14 15:10 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys   2578 12月 14 15:10 ssh_host_rsa_key
-rw-r--r--. 1 root root        554 12月 14 15:10 ssh_host_rsa_key.pub

//非对称密钥加密有三种方式,分别是rsa,ecdsa,ed25519
//而主要配置文件则是在/etc/ssh/sshd_config 下
[root@lxb ~]# cat /etc/ssh/sshd_config 

#Port 22                       监听端口,默认监听22端口    (可以修改)
#AddressFamily any             IPV4和IPV6协议家族用哪个,any表示二者均有
#ListenAddress 0.0.0.0         指明监控的地址,0.0.0.0表示本机的所有地址  【默认可修改】
#ListenAddress ::              指明监听的IPV6的所有地址格式
 
#Protocol 2                       使用SSH第二版本
 
# HostKey for protocol version 1     第一版的SSH支持的秘钥形式
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2    第二版本SSH秘钥,支持以下四种秘钥认证的存放位置
HostKey /etc/ssh/ssh_host_rsa_key               rsa私钥认证 (默认)
#HostKey /etc/ssh/ssh_host_dsa_key              dsa私钥认证
HostKey /etc/ssh/ssh_host_ecdsa_key             ecdsa私钥认证
HostKey /etc/ssh/ssh_host_ed25519_key           ed25519私钥认证
 
#ServerKeyBits 1024                             主机秘钥长度
       
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV       当使用ssh登录系统的时候,SSH会记录信息在/var/log/secure里面
#LogLevel INFO                日志的等级
 
# Authentication:
#LoginGraceTime 2m            登录的有效时间,超过则自动断开连接
#PermitRootLogin yes          是否允许管理员远程登录,'yes'表示允许
#StrictModes yes              是否让sshd去检查用户主目录或相关文件的权限数据
#MaxAuthTries 6               最大认证尝试次数,之后需要等待某段时间后才能再次输入密码
#MaxSessions 10      允许的最大会话数
AuthorizedKeysFile .ssh/authorized_keys    
    选择密钥验证时,客户端生成一对公私钥之后,会将公钥放到.ssh/authorizd_keys里面
PasswordAuthentication yes    是否允许支持基于口令的认证
ChallengeResponseAuthentication no     
    是否允许使用键盘输入身份验证,也就是xshell的第三个登录方式
#UseDNS yes    是否反解DNS,如果想让客户端连接服务器端快一些,这个可以改为no
Subsystem sftp /usr/libexec/openssh/sftp-server        
    支持 SFTP ,如果注释掉,则不支持sftp连接
AllowUsers user1 user2       
    登录白名单(需要自己手动添加),允许远程登录的用户
    如果名单中没有的用户,则提示拒绝登录
  • 设置端口登录:

//在客户端上配置允许登录的端口号
[root@lxb ~]# vim /etc/ssh/sshd_config 
Port 11111

//关闭防火墙,查看是否信任,然后重启服务
[root@lxb ~]# systemctl stop firewalld
[root@lxb ~]# getenforce 
Permissive
[root@lxb ~]# systemctl restart sshd

//然后选择另一个虚拟机,登录客户虚拟机
[root@localhost ssh]# ssh root@192.168.220.234
ssh: connect to host 192.168.220.234 port 22: Connection refused
//此时发现登录默认的22端口无法成功,显示拒绝服务
[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password: 
//带上11111端口后,发现可以登录这个虚拟机
  • 服务器解决root登录:

//修改配置文件,设置为no
[root@lxb ~]# vim /etc/ssh/sshd_config 
PermitRootLogin no

//重启服务
[root@lxb ~]# systemctl restart sshd

//此时选择登录,发现输入密码后,无法登录
[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password: 
Permission denied, please try again.
root@192.168.220.234's password: 
Permission denied, please try again.
root@192.168.220.234's password: 

//但是使用普通用户还是可以登录
[root@localhost ssh]# ssh lxb@192.168.220.234 -p 11111
lxb@192.168.220.234's password: 
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

[lxb@lxb ~]$ 
  • 允许服务器(特定用户)登录:

//修改配置文件,增加允许等录
[root@lxb ~]# vim /etc/ssh/sshd_config 
allowusers lsp

//重启服务器
[root@lxb ~]# systemctl restart sshd

//此时发现root和lxb用户都无法登录,只有lsp可以登录
[root@localhost ssh]# ssh lxb@192.168.220.234 -p 11111
lxb@192.168.220.234's password: 
Permission denied, please try again.
lxb@192.168.220.234's password: 

[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password: 
Permission denied, please try again.
root@192.168.220.234's password: 

[root@localhost ssh]# ssh lsp@192.168.220.234 -p 11111
lsp@192.168.220.234's password: 
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

[lsp@lxb ~]$ 
  • 免密登录(root用户):

//设置一个客户端公钥(使用rsa加密形式)
[root@lxb /]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /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:QPoBN4rwsgLvzM5IecRjsUkWtFxNaD+IuE/ga1wskGw root@lxb
The key's randomart image is:
+---[RSA 3072]----+
|. .o..B.         |
| o..+O o         |
|+.+*= =          |
|+E+.+o =         |
|* =B  . S        |
|.B++.            |
|.oX.             |
|.B..             |
|o o              |
+----[SHA256]-----+
[root@lxb /]# 
//此时产生一对公私密钥
//公钥存放在/root/.ssh/id_rsa.pub
//私钥存放在/root/.ssh/id_rsa

//此时将公钥发送到服务器
[root@lxb /]# scp /root/.ssh/id_rsa.pub   root@192.168.220.131:/root/.ssh 
The authenticity of host '192.168.220.131 (192.168.220.131)' can't be established.
ECDSA key fingerprint is SHA256:jy0EtFt7IuRKPmUUE7QudvMPCukUlKiFgbnddo1oR34.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.131' (ECDSA) to the list of known hosts.
root@192.168.220.131's password: 
id_rsa.pub                                                          100%  562   619.0KB/s   00:00    

//此时去服务器查看是否可以登录客户端
[root@localhost ssh]# ssh root@192.168.220.234
ssh: connect to host 192.168.220.234 port 22: Connection refused
[root@localhost ssh]# 

//实现服务端免密登录

//此时在服务端将文件 id_rsa.pub 名字修改为authorized_keys
[root@localhost .ssh]# mv id_rsa.pub authorized_keys

//此时去客户端去登录服务端
[root@lxb /]# ssh   root@192.168.220.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 Dec 21 08:36:09 2021 from 192.168.220.234
[root@localhost ~]# exit

//发现已经实现客户端免密登录
  • 免密登录(非root用户):

//在客户端产生密钥id_rsa_miyao
[root@lxb /]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_miyao
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa_miyao.
Your public key has been saved in /root/.ssh/id_rsa_miyao.pub.
The key fingerprint is:
SHA256:Di1uCwMI3OCH1nRIC2sOVS1lVh/Bbll/b1tRtR4we24 root@lxb
The key's randomart image is:
+---[RSA 3072]----+
| oo+o++...o. o  o|
|o.O.+o.  .... + o|
|oB = .   ..o o +.|
|*..    .  +   =.o|
|...   o S.     Eo|
|   . . +      . +|
|    o o .      .o|
|     + .       . |
|      .          |
+----[SHA256]-----+

//此时将密钥发送到服务端的lxb用户上
[root@lxb /]# scp /root/.ssh/id_rsa_miyao.pub   root@192.168.220.131:/home/lxb/.ssh 
id_rsa_miyao.pub                                                    100%  562   627.1KB/s   00:00 

//此时发现在192.168.220.131的lxb用户下产生了一个.ssh 文件,但是无法实现免密登录lxb
//此时需要重新在lxb文件下创建文件夹.ssh
//然后重新进行scp传输,并修改lxb的文件为authorized_keys
[root@localhost .ssh]# mv id_rsa_miyao.pub authorized_keys
//此时需要给lxb用户下的.ssh文件夹和authorized_keys设置用户名和用户组为lxb

//此时在客户端下登录lxb用户
[root@lxb .ssh]# ssh   lxb@192.168.220.131 -i  id_rsa_miyao

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 Dec 21 09:15:20 2021 from 192.168.220.234

[lxb@localhost ~]$ 
[lxb@localhost ~]$ exit

//  !!!此时需要注意,客户端登录需要使用-i 并用设置的给lxb的密钥/root/.ssh/id_rsa_miyao
//如果scp传输给服务端的用户是root,需要修改目标用户的chown用户和组
//而且用户下没有.ssh的话,直接scp可能会无法读取,需要在用户下mkdir一个.ssh文件
  • Xshell免密登录:

Xshell产生密钥
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
autosar参考资料,Secure相关概念汇总 AES 加密算法,由NIST制定的标准,例如AES-128, AES-192 AsymmetricCryptography 使用两个不同值进行加密和解密的加密算法。非对称算法是基于很大数量的,并且很耗时。 Authenticity 确认数据(例如固件)的真实性 Car-to-X 车辆与其他组件(例如其他汽车,交通标志等)的通信方案 Cipher 实现加密算法的模块(硬件或软件) Chain-of-Trust 作为安全启动的增强变体,固件检查分为多个子块,以减少启动时间。 ECU 电子控制单元,一个由TIER1开发的组件 Elliptic Curve Algorithm 在80年代开发的非对称密码算法,使用短密钥(<512位) EVITA 欧盟资助的项目,旨在为不同用例指定安全模块 Glitch Attack 电压毛刺使设备处于未指定的状态和行为。 HASH 一种基于任意输入数据计算值的算法,该值可用于验证输入数据 HSM 硬件安全模块;集成了密码和内核的加密模块,有时用户可对其进行编程 Integrity 涉及维护数据的一致性,准确性和可信赖性 Key / Crypto Key 加密算法用作输入参数的值。 Key Management 在生产和现场设备中处理和分发密码密钥 NIST National Institute of Standards and Technology,即国家标准技术局 OTA Over-The-Air的缩写,即云端升级,通过无线连接进行固件更新 Physical Attack 一种攻击方法,使用了超出规格的设备(例如,电压过高/过低/温度或时钟,强光等)。 Replay Attacks 记录并重播加密消息或图像。在这种情况下,攻击者无需知道安全信息(例如密钥)。 RNG / TRNG / PRNG 随机数生成器–真正的随机数生成器根据随机物理效应生成数字; PRNG根据数学算法生成数字 RSA 70年代开发的非对称密码算法使用长密钥(> 1500位) Secure-Boot 一种在硬件模块启动时检查设备固件是否已修改的方法 Secure Memory 一种存储安全信息(例如,加密密钥)并具有严格访问限制的存储器。 SHA 由NIST指定的哈希算法系列,例如 SHA-1,SHA-2,SHA-2 SHE 安全标准;由HIS组中的德国汽车OEM指定。 Side-Channel Attacks 一种攻击方法,攻击者在其中测量加密模块/软件的各个方面。根据这些测量,攻击者可以推导/猜测安全信息,例如: key。 Signature 用于证明数字消息或文档真实性的值 Symmetric Cryptography 使用相同值进行加密和解密的加密算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值