您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页37ssh介绍与实践

37ssh介绍与实践

来源:二三娱乐

1.1 SSH服务基本介绍

SSH 服务是Secure Shell Protocol 的简写,是安全的Shell协议。是古老的基础服务,是远程连接Linux,管理Linux的服务,使用的默认端口为22。

1.1.1Linux ssh客户端里含有的命令

[root@m01 ~]# rpm -ql openssh-clients
/usr/bin/scp          #远程拷贝文件(加密)。
/usr/bin/sftp         #ftp服务,加密的传输文件。
/usr/bin/ssh          #远程连接。
/usr/bin/ssh-copy-id  #拷贝密钥中的公钥文件的。

1.2 网络服务对应端口介绍

rsync       对应端口:   873
ssh         对应端口:   22
smtp        对应端口:   25
pop3        对应端口:  110   邮件接收服务
web         对应端口:   80 
telnet      对应端口:   23
rpcbind     对应端口:  111
ftp             对应端口:   21
windows 对应端口:  3389        远程桌面

1.3 ssh协议

ssh协议属于tcp/ip协议族,在传输过程中数据是加密的,是安全的Shell协议。
ssh协议有两种架构形式:
服务端和客户端,称之为c/s架构,client/server
浏览器/服务器,称之为b/s架构,browser/server

1.4 ssh服务端软件

安装:
[root@m01 ~]$ yum install openssl openssh -y
[root@m01 ~]$ rpm -qa openssh openssl
openssh-7.4p1-16.el7.x86_64   <==ssh服务。
openssl-1.0.2k-16.el7.x86_64  <==加密用的ssl。

重启:

[root@m01 ~]$ systemctl restart sshd
[root@m01 ~]$ systemctl enable sshd

配置文件:

[root@m01 ~]$ ll /etc/ssh/sshd_config 
-rw-------. 1 root root 3907 4月  11 2018 /etc/ssh/sshd_config

1.5 ssh客户端工具介绍

windows下使用的软件有xshell、SecureCRT和putty
linux下的就是ssh

1.6 Linux ssh客户端里含有的命令

[root@m01 ~]# rpm -ql openssh-clients
/usr/bin/scp          #远程拷贝文件(加密)。
/usr/bin/sftp         #ftp服务,加密的传输文件。
/usr/bin/ssh          #远程连接。
/usr/bin/ssh-copy-id  #拷贝密钥中的公钥文件的

1.7 ssh客户端命令介绍与实践

1.7.1 ssh命令:远程连接linux

ssh -p  22  root@10.0.0.31

命令 -p 端口 用户@IP
等价于ssh 10.0.0.31 IP后面可以接要执行的命令

1.7.3 scp命令:远程拷贝(secure copy),加密的拷贝

参数      说明
-P      端口  scp 端口是大P
-r      递归
-p      保持属性
-l      限制速度

推:
scp     -P  22  -rp /data   root@172.16.1.7:/tmp
命令  大P  端口  人品  本地目录    用户@IP:目录

拉
scp     -P  22  -rp root@172.16.1.7:/tmp    /data
命令  大P  端口  人品  目标   用户@IP:目录   本地目录

1.74 rsync和scp区别

scp 全量拷贝
reync   增量

1.7.5 sftp(了解即可)

sftp命令:加密的ftp服务,默认缺点不能锁定固定目录。

sftp  -oPort=22 root@172.16.1.7
sftp> put "/etc/hosts"      <===上传到远端家目录。
sftp> put /etc/hosts /tmp   <===上传到远端指定目录/tmp。
sftp> get /etc/yum.conf     <===下载到本地家目录。
sftp> get oldboy.txt /etc   <===下载到本地指定目录/etc
         支持二进制命令

1.8 xshell ssh连不上服务器如何排查

ping    IP
检查防火墙和SElinux
查服务状态 SSHD
检查XSHELL设置  IP 端口 协议 密码 用户 终端

1.9 ssh服务端配置文件

[root@m01 ~]$ ll /etc/ssh/ssh*
-rw-r--r-- 1 root root 2276 4月 11 2018 /etc/ssh/ssh_config  <==ssh配置文件。
-rw------- 1 root root 3907 4月 11 2018 /etc/ssh/sshd_config <==sshd配置文件。

1.10 ssh服务配置优化

在配置文件/etc/ssh/sshd_config中添加以下信息:
####Start by oldboy#2019-04-22###
Port 52113                             #使用大于10000的端口
PermitRootLogin no               #禁止root远程登录,可以su - root,C7需要改yes为no
PermitEmptyPasswords no     #禁止空密码登录,C7默认就是
UseDNS no                             #不使用dns解析,将yes改为no
GSSAPIAuthentication no       #禁止连接慢的解决配置
ListenAddress 172.16.1.61:52113  #只允许内网IP连接SSH(172.16.1.0)这里可以先注释等以后再开
####End by oldboy#2019-04-22###

由于设置了只允许内网IP连接ssh,所以禁止root远程登录和使用大于10000的端口可以不需要配置,所以,最终需要配置的就是如下几项:

PermitEmptyPasswords no          #禁止空密码登录
UseDNS no                        #不使用dns解析
GSSAPIAuthentication no          #禁止连接慢的解决配置
#ListenAddress 172.16.1.61:22    #暂时不配,将来配置完VPN安全服务之后再加此行。

1.11.2 秘钥认证

就是不需要用户名和密码,只使用秘钥文件就可以连接服务器。

1.12 实战:SSH批量处理命令和文件

项目经验:实现SSH批量分发文件、批量管理服务方案
借助:秘钥文件,登录的时候不用使用密码。

1.12.1 环境准备:

m01   172.16.1.61  管理机(放钥匙)
web01 172.16.1.7   被管理机(放锁)
web02 172.l6.1.8   被管理机(放锁)

1.12.2 开始实战

  1. 基于root用户做Linux之间的秘钥认证。

  2. 生成密钥对,不需要指定服务器。

  3. 在172.16.1.61上生成密钥对。

[root@m01 ~]$ ssh-keygen 
ssh-keygen 连续回车

查看生成的密钥对:

[root@m01 ~]$ ls /root/.ssh -l
总用量 12
-rw------- 1 root root 1679 4月  22 12:12 id_rsa        <==私钥,钥匙。
-rw-r--r-- 1 root root  390 4月  22 12:12 id_rsa.pub    <==公钥,锁。
-rw-r--r-- 1 root root  513 4月  22 11:45 known_hosts

4. 把锁放到172.16.1.7和172.16.1.8上

使用/usr/bin/ssh-copy-id命令:专业拷贝密钥中的公钥文件的工具。

[root@m01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub  172.16.1.7   放锁
[root@m01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub  172.16.1.8   放锁

5. 测试远程执行命令:

[root@m01 ~]$ ssh 172.16.1.7 ifconfig
[root@m01 ~]$ ssh 172.16.1.8 ifconfig

6. 写个脚本,实现远程批量执行命令:

[root@m01 ~]$ cat /server/scripts/cmd.sh
for n in 7 8
do
   echo "=====172.16.1.$n======"
   ssh 172.16.1.$n "$1"
done

7批量执行命令

[root@m01 ~]$ sh /server/scripts/ "命令"

8. 批量发文件:

创建批量分发文件脚本:

1)简单脚本:

[root@m01 ~]$ cat /server/scripts/cmd.sh
for n in 7 8
do
echo "=====172.16.1.$n======"
ssh 172.16.1.$n "$1"
done

2)复杂脚本

[root@m01 /]# cat fenfa1.sh 

#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 2 ]
then
echo "usage:$0 localfile remotedir"
exit 1
fi

for n in 31 41 51
do
scp -P 22 -rp $1 root@172.16.1.$n:$2 &>/dev/null
if [ $? -eq 0  ]
then
    action "172.16.1.$n successful" /bin/true
else
    action "172.16.1.$n failure" /bin/false
fi
done    

$# 是指命令行脚本后面参数的个数

$? 表示上一个命令是否执行成功,如为0,则成功

$0 是指脚本的名字(含路径)。

执行脚本,实现分发软件:将本地/data目录下的内容拷贝到远端服务器的/opt目录下

[root@m01 ~]$ mkdir -p /data  
[root@m01 ~]$ ls /data
[root@m01 ~]$ cd /data
[root@m01 /data]$ touch {1..5}.txt
[root@m01 /data]$ sh /server/scripts/fenfa.sh /data /opt/
172.16.1.7 successful                                      [  确定  ]
172.16.1.8 successful                                      [  确定  ]

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务