侧边栏壁纸
博主头像
运维匠-运维工程师知识分享经验和最佳实践博主等级

生活百般滋味,人生需要笑对

  • 累计撰写 60 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

ssh服务

运维匠
2022-09-12 / 0 评论 / 0 点赞 / 7 阅读 / 8933 字
温馨提示:
本文最后更新于 2024-07-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

ssh服务

ssh服务介绍

ssh: secure shell protocol, 22/tcp, 安全的远程登录,实现加密通信,代替传统的 telnet 协议
具体的软件实现:

  • OpenSSH:ssh协议的开源实现,CentOS 默认安装
  • dropbear:另一个ssh协议的开源项目的实现

SSH 协议版本

  • v1:基于CRC-32做MAC,不安全;man-in-middle
  • v2:双方主机协议选择安全的MAC方式,基于DH算法做密钥交换,基于RSA或DSA实现身份认证

openssh 服务

OpenSSH是SSH (Secure SHell) 协议的免费开源实现,一般在各种Linux版本中会默认安装,基于C/S结构
Openssh软件相关包:

  • openssh
  • openssh-clients
  • openssh-server

客户端 ssh命令

ssh命令是ssh客户端,允许实现对远程系统经验证地加密安全访问
当用户远程连接ssh服务器时,会复制ssh服务器/etc/ssh/ssh_host*key.pub文件中的公钥到客户机的~/.ssh/know_hosts中。下次连接时,会自动匹配相对应的私钥,不能匹配,将拒绝连接
ssh客户端配置文件: /etc/ssh/ssh_config
主要配置:

#StrictHostKeyChecking ask
#首次登录不显示检查提示
StrictHostKeyChecking no
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22

范例:禁止首次连接的询问过程

[root@centos7 ~]#sed -i.bak '/StrictHostKeyChecking/s/.*/StrictHostKeyChecking
no/' /etc/ssh/ssh_config

格式

ssh [user@]host [COMMAND]
ssh [-l user] host [COMMAND]

常用选项

-p port #远程服务器监听的端口
-b #指定连接的源IP
-v #调试模式
-C #压缩方式
-X #支持x11转发
-t #强制伪tty分配,如:ssh -t remoteserver1 ssh -t remoteserver2 ssh
remoteserver3
-o option 如:-o StrictHostKeyChecking=no
-i <file> #指定私钥文件路径,实现基于key验证,默认使用文件: ~/.ssh/id_dsa,
~/.ssh/id_ecdsa, ~/.ssh/id_ed25519,~/.ssh/id_rsa等

在远程主机执行本地脚本

[root@rocky ~]# cat test.sh 
#!/bin/bash
hostname -I
[root@rocky ~]# ssh root@192.168.179.145 /bin/bash <test.sh 
root@192.168.179.145's password: 
192.168.179.145 
[root@rocky ~]# 

其他ssh客户端工具

scp命令

scp [options] SRC... DEST/

方式:

scp [options] [user@]host:/sourcefile /destpath
scp [options] /sourcefile [user@]host:/destpath
scp [options] [user@]host1:/sourcetpath [user@]host2:/destpath

常用选项

-C 压缩数据流
-r 递归复制
-p 保持原文件的属性信息
-q 静默模式
-P PORT 指明remote host的监听的端口
```bash
**范例:从远程机器复制文件到本地目录**

```TEXT
scp root@10.10.10.10:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/ 

范例: 上传本地文件到远程机器指定目录*

scp /opt/soft/nginx-0.5.38.tar.gz root@10.10.10.10:/opt/soft/scptest

注:复制目录加上选项-r

rsync命令

rsync工具可以基于ssh和rsync协议实现高效率的远程系统之间复制文件,使用安全的shell连接做为传输方式,比scp更快,基于增量数据同步,即只复制两方不同的文件,此工具来自于rsync包
注意:通信两端主机都需要安装 rsync 软件

rsync -av /etc server1:/tmp/ #复制目录和目录下文件
rsync -av /etc/ server1:/tmp/ #只复制目录下文件

常用选型

-n 模拟复制过程
-v 显示详细过程
-r 递归复制目录树
-p 保留权限
-t 保留修改时间戳
-g 保留组信息
-o 保留所有者信息
-l 将软链接文件本身进行复制(默认)
-L 将软链接文件指向的文件复制
-u 如果接收者的文件比发送者的文件较新,将忽略同步
-z 压缩,节约网络带宽
-a 存档,相当于-rlptgoD,但不保留ACL(-A)和SELinux属性(-X)
--delete 源数据删除,目标数据也自动同步删除
--progress 显示进度
--bwlimit=5120 #限速以KB为单位,5120表示5MB

范例:复制文件到远程主机目录下

rsync ./test.sh root@192.168.179.145:/root

自动登录 ssh工具 sshpass

由EPEL源提供,ssh登陆不能在命令行中指定密码。sshpass的出现,解决了这一问题。sshpass用于非
交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码(本机known_hosts文件中有的主机才能
生效)。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环
境变量中读取。
格式:

sshpass [option] command parameters

常用选项

-p password #后跟密码它允许你用 -p 参数指定明文密码,然后直接登录远程服务器
-f filename #后跟保存密码的文件名,密码是文件内容的第一行
-e #将环境变量SSHPASS作为密码

范例:登录远程主机执行指定命令

# 检测sshpass是否安装
[root@rocky ~]# rpm -q sshpass
package sshpass is not installed
# 安装sshpass
[root@rocky ~]# yum install sshpass
# 登录远程主机,首次登录不显示检查提示,执行hostname -I命令
[root@rocky ~]# sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@192.168.179.145 hostname -I
192.168.179.145 
[root@rocky ~]# 

实现基于密钥登录方式验证

在客户端生成密钥对

ssh-keygen -t rsa [-P 'password'] [-f “~/.ssh/id_rsa"]

把公钥文件传输至远程服务器对应用户的家目录

ssh-copy-id [-i [identity_file]] [user@]host

重设私钥口令:

ssh-keygen -p

在SecureCRT或Xshell实现基于key验证
在SecureCRT工具—>创建公钥—>生成Identity.pub文件
转化为openssh兼容格式(适合SecureCRT,Xshell不需要转化格式),并复制到需登录主机上相应文
件authorized_keys中,注意权限必须为600,在需登录的ssh主机上执行:

ssh-keygen -i -f Identity.pub >> .ssh/authorized_keys

范例:实现基于 key 验证

[root@centos8 ~]#ssh-keygen
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:vpPtmqRv1llmoSvqT2Lx5C0LPGTE0pvdAqhDqlR5jLY root@centos8.wangxiaochun.com
The key's randomart image is:
+---[RSA 3072]----+
| |
| ++ |
| .=oo= |
| oo.oo = . . |
|..oE * S .. . |
|o . + * o. + |
|. * B+.* |
| . B*== |
| .+*B=. |
+----[SHA256]-----+
[root@centos8 ~]#ll .ssh/
total 8
-rw------- 1 root root 2622 May 22 09:51 id_rsa
-rw-r--r-- 1 root root 583 May 22 09:51 id_rsa.pub
[root@centos8 ~]#

# 将本主机的公钥复制到远程主机的authorized_keys中
[root@centos8 ~]#ssh-copy-id root@10.0.0.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:s//WMgPVXmOjqfOg3f3X0nmaPZF+Fj5vPdWCnAzDcpU.
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@10.0.0.7's password: #输入远程用户的密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.0.7'"
and check to make sure that only the key(s) you wanted were added.

#对私钥加密
[root@centos8 ~]#ssh-keygen -p

ssh服务配置

服务器端:sshd
服务器端的配置文件: /etc/ssh/sshd_config
服务器端的配置文件帮助:man 5 sshd_config
常用参数:

Port 22 #生产建议修改
ListenAddress ip
LoginGraceTime 2m
PermitRootLogin yes #默认ubuntu不允许root远程ssh登录
StrictModes yes #检查.ssh/文件的所有者,权限等
MaxAuthTries 6 #pecifies the maximum number of authentication
attempts permitted per connection. Once the number of failures reaches half this
value, additional failures are logged. The default is 6.
MaxSessions 10 #同一个连接最大会话
PubkeyAuthentication yes #基于key验证
PermitEmptyPasswords no #空密码连接
PasswordAuthentication yes #基于用户名和密码连接
GatewayPorts no
ClientAliveInterval 10 #单位:秒
ClientAliveCountMax 3 #默认3
UseDNS yes #提高速度可改为no
GSSAPIAuthentication yes #提高速度可改为no
MaxStartups #未认证连接最大值,默认值10
Banner /path/file
#以下可以限制可登录用户的办法:
AllowUsers user1 user2 user3
DenyUsers user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2

范例:设置 ssh 空闲60s 自动注销

Vim /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 0
Service sshd restart
#注意:新开一个连接才有效

范例:解决ssh登录缓慢的问题

vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd
0

评论区