OpenSSH

 

1. OpenSSH简介

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

常见的远程登录工具有: telnet ssh dropbear telnet //远程登录协议,23/TCP 认证明文 数据传输明文 ssh //Secure SHell,应用层协议,22/TCP 通信过程及认证过程是加密的,主机认证 用户认证过程加密 数据传输过程加密 dropbear //嵌入式系统专用的SSH服务器端和客户端工具

2. 为什么使用SSH?

使用 OpenSSH工具将会增进你的系统安全性。所有使用OpenSSH工具的通讯,包 括口令, 都会被加密。telnet 和ftp 使用纯文本口令,并被明文发送。这些信息可能会被截取,口令可能会被检索,然后未经授权的人员可能会使用截取的口令登录进你的系统而对你的系统造成危害。你应该尽可能地使用OpenSSH的工具集合来避免这些安全问题。

另一个使用 OpenSSH的原因是,它自动把DISPLAY 变量转发给客户机器。 换一句 话说, 如果你在本地机器上运行X窗口系统,并且使用ssh命令登录到了远程机器上,当你在远程机器上执行一一个需要X的程序时,它会显示在你的本地机器上。如果你偏爱图形化系统管理工具,却不能够总是亲身访间该服务器,这就会为你的工作大开方便之门。

3. OpenSSH的作用

OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据,并由此来代替原来的类似服务。 OpenSSH是使用SSH透过计算机网络加密通讯的实现。

4. OpenSSH的工作模式

openSSH是基于C/S架构工作的。一台客户端,一台服务端。

cs:客户端加service服务器 bs:浏览器加service服务器 服务器端——sshd,配置文件在/etc/ssh/sshd_config (可以优化服务端的配置) 客户端——ssh,配置文件在/etc/ssh/ssh_config (这个配置文件不需要修改) ssh-keygen //密钥生成器, ssh-copy-id //将公钥传输至远程服务器 scp //跨主机安全复制工具,传输工具 ssh-keygen是客户端生成密钥,不是服务端

5. OpenSSH免密配置

5.1 创建公钥

 [root@node1 ~]# 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:edHQvFtTiOS/VFcE3y3/t+cdpTjhXG5kUVMqbCs+/d8 root@mashuangle
 The key's randomart image is:
 +---[RSA 3072]----+
 |         .+...=*|
 |           =+ .+*|
 |         . *.+.*|
 |         . o.+o=.|
 |       S o oo*.o|
 |         o =.O oo|
 |         o * =.o|
 |           . + B|
 |             .+E|
 +----[SHA256]-----+

5.2 查看私钥和公钥

 [root@node1 ~]# ls .ssh/
 id_rsa id_rsa.pub known_hosts
 [root@node1 ~]# ll .ssh/
 total 12
 -rw-------. 1 root root 2602 Dec 22 22:36 id_rsa
 -rw-r--r--. 1 root root  569 Dec 22 22:36 id_rsa.pub
 -rw-r--r--. 1 root root  176 Dec 22 22:31 known_hosts

5.3 将公钥传给对方

 [root@node1 ~]# ssh-copy-id root@192.168.59.128
 /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.59.128's password:
 Permission denied, please try again.
 root@192.168.59.128's password:
 
 Number of key(s) added: 1
 
 Now try logging into the machine, with:   "ssh 'root@192.168.59.128'"
 and check to make sure that only the key(s) you wanted were added.

5.4 查看公钥

 [root@node2 ~]# ll .ssh/
 total 4
 -rw------- 1 root root 569 Dec 22 22:40 authorized_keys

5.5 尝试免密登录

 [root@node1 ~]# ssh 192.168.59.128
 Last failed login: Thu Dec 22 22:40:39 CST 2022 from 192.168.59.129 on ssh:notty
 There was 1 failed login attempt since the last successful login.
 Last login: Thu Dec 22 22:40:10 2022 from 192.168.59.1
 [root@msl ~]#

6. 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的访问频度和并发在线数

  • 做好日志的备份,经常分析(集中于某台服务器)

posted @ 2022-12-25 16:28  Thespace  阅读(102)  评论(0编辑  收藏  举报