centos7服务搭建常用服务配置之一:SSH

1 SSH服务协议

1.1 ssh服务协议说明

SSH 是 Secure Shell Protocol 的简写,由 IETF 网络工作小组(Network Working Group )制定;在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输。确保了传递的数据安全。

SSH是专为远程登录会话和其他网络服务提供的安全性协议。利用 SSH 协议可以有效的防止远程管理过程中的信息泄露问题,在当前的生产环境运维工作中,绝大多数企业普遍采用SSH协议服务来代替传统的不安全的远程联机服务软件,如telnet(23端口,非加密的)等。

在默认状态下,SSH服务主要提供两个服务功能:

  • 一是提供类似telnet远程联机服务器的服务,即上面提到的SSH服务。
  • 另一个是类似FTP服务的sftp-server,借助SSH协议来传输数据的.提供更安全的SFTP服务(vsftp,proftp)。

1.2 ssh服务工作机制

服务器启动的时候自己产生一个密钥(768bit公钥),本地的ssh客户端发送连接请求到ssh服务器,服务器检查连接点客户端发送的数据和IP地址,确认合法后发送密钥(768bits)给客户端,此时客户端将本地私钥(256bit)和服务器的公钥(768bit)结合成密钥对key(1024bit),发回给服务器端,建立连接通过key-pair数据传输。

1.3 ssh加密技术说明

简单的说,SSH加密技术就是将人类可以看得懂的数据,通过一些特殊的程序算法,把这些数据变成杂乱的无意义的信怠,然后,通过网络进行传输,而当到了目的地后,在通过对应的解密算法,把传过来的加密的数据信怠解密成加密前的可读的正常数据。因此,当数据在互联网上传输时即使被有心的黑客监听窃取了,也很难获取到真正黑要的数据。

当前,网络上的数据包加密技术一般是通过所谓的一对公钥与私钥(PublickeyandPivatekey)组合成的密钥对进行加密与解密操作。如下图,A-Server要给B_Client传数据,首先会通过本地的公钥加密后再到发到网络上传输。而加密的数据到达B_Client端后,再经由B_Client本地的私钥将加密的数据解密出来。由于在intemet上传输过程中的数据是加密过的,所以,传输的数据内容一般来说是比较安全的。

1.3.1 ssh实现安全链接建立,利用要是和锁头

  • 1.钥匙=私钥 锁头=公钥,私钥可以解密公钥
  • 2.公钥可以再网络中传输,私钥再本地主机保存

1.3.2 ssh加密算法

v1漏洞: 密钥不更换

v2 定期更换密钥

利用Diffie-Hellman机制定期更新密钥

1.4 ssh知识要点

ssh是安全的加密协议,用于远程链接linux服务器

ssh 默认端口是22,安全协议版本sshv2,出来2之外还有1(有漏洞)

ssh服务端主要包括两个服务功能 ssh远程链接和sftp服务

linux ssh 客户端包括ssh 远程链接命令,以及远程拷贝scp命令等

2.ssh服务软件详细说明

2.1 ssh服务软件安装

客户端:


[root@centos7-127 ~]# rpm -qf `which ssh`
openssh-clients-7.4p1-16.el7.x86_64


服务端


[root@centos7-127 ~]# rpm -qf `which sshd`
openssh-server-7.4p1-16.el7.x86_64

2.2 openssh-clients软件的主要内容


[root@centos7-127 ~]# rpm -ql openssh-clients
/etc/ssh/ssh_config 	# --ssh客户端配置文件
/usr/bin/scp			# --ssh远程复制命令
/usr/bin/sftp			# --ssh远程文件传输命令
/usr/bin/slogin			# --ssh远程登陆命令
/usr/bin/ssh			# --ssh远程登陆管理主机
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id	# --ssh服务分发公匙命令
/usr/bin/ssh-keyscan
/usr/lib64/fipscheck/ssh.hmac

2.3 openssh-server软件的主要内容


[root@centos7-127 ~]# rpm -ql openssh-server
/etc/pam.d/sshd			
/etc/ssh/sshd_config	# ssh服务配置脚本
/etc/sysconfig/sshd		# ssh服务启动脚本
/usr/lib/systemd/system/sshd-keygen.service
/usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/sshd.socket
/usr/lib/systemd/system/sshd@.service
/usr/lib64/fipscheck/sshd.hmac	# ssh创建密匙有关
/usr/libexec/openssh/sftp-server
/usr/sbin/sshd 	# ssh服务进程启动命令
/usr/sbin/sshd-keygen # ssh服务生成密匙命令


2.4 启动sshd服务

systemctl start sshd # 启动sshd服务
systemctl restart sshd # 重启sshd服务
systemctl enable sshd # 容许sshd服务开机启动

2.5 ssh服务配置文件说明

1 配置文件中所有注释信息,表示默认参数配置

2 配置文件中#空格 后面内容表示注释说明信息

#参数 表示没人配置参数信息

3 配置文件参数信息修改后,一旦变为注释,即还原为默认配置

2.5.1 ssh服务的配置文件路径

修改SSH服务的运行参数,是通过修改配置文件/etc/ssh/sshd_config实现的。

一般来说SSH服务使用默认的配置已经能够很好的工作了,如果对安全要求不高,仅仅提供SSH服务的情况,可以不需要修改任何配置。



[root@test ~]# vim /etc/ssh/sshd_config

#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

 

# This is the sshd server system-wide configuration file.  See

# sshd_config(5) for more information.

 

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

 

# The strategy used for options in the default sshd_config shipped with

# OpenSSH is to specify options with their default value where

# possible, but leave them commented.  Uncommented options change a

# default value.

 

# OpenSSH is to specify options with their default value where

# possible, but leave them commented.  Uncommented options change a

# default value.

 

Port 25113                   # 端口号

ListenAddress 10.0.0.41   # 监听地址(本地网卡地址),指定本地网卡那个网卡提供服务

PermitRootLogin no         # 是否允许root用户登陆

#PermitEmptyPasswords no  # 禁止空密码登陆

#UseDNS no                   # 不使用DNS

GSSAPIAuthentication no    # API认证

# 连接慢的解决

#AddressFamily any         # 指定监听ipv4地址,或是ipv6地址,或者所有都监听

配置文件内容说明:

井号(#)注释的参数信息为默认配置

井号(#)后面有空格的为描述信息

井号(#)后面没有空格的为参数信息

另外:配置文件参数信息修改后,一旦变为注释,即还原为默认配置

2.5.3 配置文件语法检查方法

使用sshd -t 命令 对配置文件的语法进行检查


[root@centos7-128 ~]# sshd -t /etc/ssh/sshd
Extra argument /etc/ssh/sshd.    # 正确的语法

2.5.4 ListenAddress 监听地址的说明

监听地址只能监听本地网卡上配置的地址,监听的网卡可以对请求做出相应,为未监听的网卡不响应请求。

2.5.5 ssh服务配置文件相关参数详细说明

命令参数 参数说明
Port 指定sshd进程监听的端口号,默认为22.可以使用多条指令监听多个端口.
默认将在本机的所有网络接□上监听,但是可以通过ListenAddress指走只在某个特定的接口上监听.
PermitEmptyPasswords 是否允许密码为空的用户远程登录.默认为"no"
PermitRootLogin 是否允许root登录.可用值如下:"yes"(默认)表示允许."no"表示禁止.
"without-password"表示禁止使用密码认证登录.
"forced-commands-only"表示只有在指走了command选项的情况下才允许使用公钥认证登录.同时其它认证方法全部被禁止.这个值常用于做远程备份之类的事情.
1.多开一个窗口
2.临时多部署一条连接方式
3.给普通用户sudo权限
UseDNS 指定定sshd是否应该对远程主机名进行反向解折,以检查此主机名是否与其IP地址真实对应.默认值为"yes”.
备注:可以通过禁用来提高ssh服务的速度
ListenAddress 指定监听并提供服务相应的网卡地址信息

2.5.6 快速修改配置参数


sed -i '13 iPort 52113
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no'  /etc/ssh/sshd_config 
# 修改ssh端口为52113 不容许root账号登陆 不容许空密码登录 不使用DNS解析 不使用API解析

3 ssh服务认证类型

  1. 基于口令认证方式
  2. 基于密钥认证方式

3.1 基于密码的认证类型

基于口令的安全验证的方式就是大家现在一直在用的,只要知道服务器的SSH连接帐号和口令(当然也要知道对应服务器的 IP及开放的 SSH端口,默认为22 ),就可以通过 ssh客户端登录到这台远程主机。此时,联机过程中所有传输的数据都是加密的。

演示了 SecureCR及ssh客户端连接,口令验证的测试。


[root@centos7-128 .ssh]# ssh 192.168.87.127  # 使用ssh远程登陆192.168.87.127,默认是当前用户
The authenticity of host '192.168.87.127 (192.168.87.127)' can't be established.
ECDSA key fingerprint is SHA256:u8wZCROAUd6ZyEsKRGgZOsFNGTXX33UYEvguqqZuWrY.
ECDSA key fingerprint is MD5:bc:de:dc:d5:c8:f6:27:35:3a:8a:7b:24:ae:8b:6d:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.87.127' (ECDSA) to the list of known hosts.
root@192.168.87.127's password:   # 输入所需密码
Last login: Tue Jul 30 16:52:47 2019 from centos7-128.cn
Warning ! From now on, all of your operation has been record!


3.2 基于密匙的安全认证方法

基于密钥的安全验证方式是指,需要依靠密钥,也就是必须事先建立一对密钥对,然后把公用密钥(锁头)(Public key)放在需要访问的目标服务器上,另外,还需要把私有密钥(钥匙)(Private key)放到SSH的客户端或对应的窖户端服务器上。

  • 私钥不能在网络中传输---私钥可以解密公钥

  • 公钥可以再网路中传输---公钥不能解密私钥

此时,如果要想连接到这个带有公用密钥的SSH服务器,客户端SSH软件或者客户端服务器就会向SSH服务器发出请求,请求用联机的用户密钥进行安全验证。SSH服务器收到请求之后,会先在该SSH服务器上连接的用户的家目录下寻找事先放上去的对应用户的公用密钥,然后把它和连接的SSH客户端发送过来的公用密钥进行比较。如果两个密钥一致,SSH服务器就用公用密钥加密"质询"(challenge)并把它发送给SSH客户端。

3.3 基于密钥登录配置

作用 主机名 ip
管理服务器 centos6-126 192.168.87.126
备份服务器 centos7-128 192.168.87.128
存储服务器 centos7-127 192.168.87.127

3.3.1 第一步:在备份服务器上创建密匙对

命令:ssh-keygen -t rsa


[root@centos7-128 ~]# 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 already exists.
Overwrite (y/n)? y
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:W58OoAljXU3AgVgGOQ9zvaWPNUMsgsrEoSJb7SGHunk root@centos7-128
The key's randomart image is:
+---[RSA 2048]----+
| ....*o+o+.      |
| .ooB.+ +o+      |
|+o+.+* ..*.      |
|o+o+ o..o +      |
|o   = . S+.o     |
| o . o o.+.. .   |
|o E   o . . o    |
| .         o     |
|            .    |
+----[SHA256]-----+

参数说明:

-t 指定创建密钥对的类型,可以创建的类型如下↓

Specifies the type of key to create.  The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa” or “rsa” for protocol version 2.

查看创建出来的密钥对:


[root@centos7-128 ~]# ll ~/.ssh/
总用量 12
-rw------- 1 root root 1675 7月  31 10:49 id_rsa   # 私钥文件
-rw-r--r-- 1 root root  398 7月  31 10:49 id_rsa.pub # 公钥文件
-rw-r--r-- 1 root root  358 7月  30 16:58 known_hosts



3.3.2 第二步:将公钥分发给存储服务器

命令:ssh-copy-id [-f] [-n] [-i [identity_file]] [-p port] [-o ssh_option] [user@]hostname

  • -i 参数指定 公钥文件的存放位置
  • [use@]表示使用的用户,默认使用当前登陆的用户

[root@centos7-128 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.87.127
/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.87.127's password:   # 注意: 首次分发密钥需要输入对端服务器的用户密码

Number of key(s) added: 1

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

3.3.3 第三步:登陆测试

将公钥发送给对端服务器以后就可以免密直接登录了,如下所示

bash

[root@centos7-128 ~]# ssh centos7-127
The authenticity of host 'centos7-127 (192.168.87.127)' can't be established.
ECDSA key fingerprint is SHA256:u8wZCROAUd6ZyEsKRGgZOsFNGTXX33UYEvguqqZuWrY.
ECDSA key fingerprint is MD5:bc:de:dc:d5:c8:f6:27:35:3a:8a:7b:24:ae:8b:6d:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'centos7-127' (ECDSA) to the list of known hosts.
Last login: Wed Sep 11 09:31:05 2019 from 192.168.87.1
Warning ! From now on, all of your operation has been record!
[root@centos7-127 ~]# 


3.4 进行免密码scp传输测试

3.4.1 scp的基本语法使用

scp - secure copy (remote file copy program)

备注:每次都是全量拷贝,增量拷贝rsync

a、PUSH推方式

命令格式:

scp -P22 -rp /tmp/2.txt root@192.168.87.127:/tmp

参数说明:

  • -P(大写,注意和ssh命令的不同)接端口,默认22端口时可以省略-P22;
  • -r递归,表示拷贝目录;
  • -p表示在拷贝前后保持文件或目录属性;
  • -l limit 限制速度。
  • /tmp/2.txt为本地的目录文件。“@”前为用户名,“@”后为要连接的服务器的IP。IP后的:/tmp目录,为远端的目标目录。

说明:

以上命令作用是把本地/tmp/2.txt拷贝到远端服务器192.168.87.127的/tmp目录;

b、PULL拉方式

命令格式

scp -P22 -rp root@192.168.87.127:/tmp/test /tmp

结论:

scp为远程拷贝文件或目录的命令,更多用法,请man scp;

拷贝权限为连接的用户对应的权限。

3.5 使用sftp进行基于密钥的文件传输

3.5.1 sftp简介

sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的其中一部分.

3.5.2 sftp命令说明


[root@centos7-128 test]# sftp -o Port=22 192.168.87.127
Connected to 192.168.87.127.
sftp> 

说明:

-o 连接的时候指定选项

Port=22 端口指定为22、

3.5.3 sftp使用说明

a、操作远端服务器

  • ls 显示远端主机的列表
  • cd 切换远程的工作目录
  • pwd 显示远程的工作目录

sftp> ls -al
dr-xr-xr-x   17 root     root          239 Sep 16 10:14 .
dr-xr-xr-x   17 root     root          239 Sep 16 10:14 ..
-rw-r--r--    1 root     root            0 Sep 16 10:14 127.txt # 显示远端服务器127的文件列表
lrwxrwxrwx    1 root     root            7 Jun 17 19:15 bin
dr-xr-xr-x    5 root     root         4096 Sep 11 09:30 boot
drwxr-xr-x   20 root     root         3300 Sep 11 09:29 dev
drwxr-xr-x  154 root     root         8192 Sep 16 09:28 etc
drwxr-xr-x    3 root     root           21 Jun 17 19:24 home
lrwxrwxrwx    1 root     root            7 Jun 17 19:15 lib
lrwxrwxrwx    1 root     root            9 Jun 17 19:15 lib64
drwxr-xr-x    2 root     root            6 Apr 11  2018 media
drwxrwxr-x    8 root     root         2048 Nov 26  2018 mnt
drwxr-xr-x    4 root     root           69 Jul 10 11:45 opt
dr-xr-xr-x  174 root     root            0 Sep 11 09:28 proc
dr-xr-x---    7 root     root         4096 Sep 16 10:06 root
drwxr-xr-x   46 root     root         1380 Sep 16 09:28 run
lrwxrwxrwx    1 root     root            8 Jun 17 19:15 sbin
drwxr-xr-x    2 root     root            6 Apr 11  2018 srv
dr-xr-xr-x   13 root     root            0 Sep 16 10:14 sys
drwxrwxrwt   14 root     root         4096 Sep 16 10:07 tmp
drwxr-xr-x   13 root     root          155 Jun 17 19:15 usr
drwxr-xr-x   22 root     root         4096 Jun 17 19:26 var
sftp> cd /root
sftp> pwd
Remote working directory: /root

b、操作本地服务器

  • lls 显示本地主机的列表
  • lcd 切换本地的工作目录
  • lpwd 查看本地目录信息

sftp> lcd /
sftp> lls -al
总用量 30
dr-xr-xr-x.  19 root root   265 9月  16 10:15 .
dr-xr-xr-x.  19 root root   265 9月  16 10:15 ..
-rw-r--r--    1 root root     0 9月  16 10:15 128.txt # 显示本地服务器128的文件列表
lrwxrwxrwx.   1 root root     7 5月  17 14:51 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 5月  17 15:02 boot
drwxr-xr-x   20 root root  3340 9月  11 09:30 dev
drwxr-xr-x. 166 root root 12288 9月  16 09:28 etc
drwxr-xr-x.   3 root root    21 5月  17 15:01 home
lrwxrwxrwx.   1 root root     7 5月  17 14:51 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 5月  17 14:51 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 4月  11 2018 media
drwxrwxr-x    8 root root  2048 11月 26 2018 mnt
drwxr-xr-x.   4 root root    69 7月  10 11:46 opt
dr-xr-xr-x  176 root root     0 9月  11 09:28 proc
drwxr-xr-x.   2 root root     6 6月  14 18:04 raid0
drwxr-xr-x.   2 root root     6 6月  14 18:24 raid1
dr-xr-x---.   7 root root   286 9月  16 09:42 root
drwxr-xr-x   53 root root  1520 9月  16 09:28 run
lrwxrwxrwx.   1 root root     8 5月  17 14:51 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 4月  11 2018 srv
dr-xr-xr-x   13 root root     0 9月  11 16:15 sys
drwxrwxrwt.  14 root root  4096 9月  16 09:58 tmp
drwxr-xr-x.  13 root root   155 5月  17 14:51 usr
drwxr-xr-x.  24 root root  4096 5月  17 15:05 var
sftp> lpwd
Local working directory: /

c、上传下载文件

get 从远端服务器下载单个文件


sftp> get /tmp/test/1.txt /tmp/test
Fetching /tmp/test/1.txt to /tmp/test/1.txt


mget 从远端服务器下载多个文件,支持通配符


sftp> mget /tmp/test/*.txt /tmp/test
Fetching /tmp/test/1.txt to /tmp/test/1.txt
Fetching /tmp/test/2.txt to /tmp/test/2.txt
Fetching /tmp/test/3.txt to /tmp/test/3.txt
Fetching /tmp/test/4.txt to /tmp/test/4.txt
Fetching /tmp/test/5.txt to /tmp/test/5.txt
Fetching /tmp/test/6.txt to /tmp/test/6.txt
Fetching /tmp/test/7.txt to /tmp/test/7.txt
Fetching /tmp/test/8.txt to /tmp/test/8.txt
Fetching /tmp/test/9.txt to /tmp/test/9.txt


put 从本地服务器上传单个文件


sftp> put /tmp/test/a.txt /tmp/test
Uploading /tmp/test/a.txt to /tmp/test/a.txt
/tmp/test/a.txt                                                                                                100%    0     0.0KB/s   00:00 

mput 从本地服务器上传多个文件,支持通配符


sftp> mput /tmp/test/[cde].txt /tmp/test
Uploading /tmp/test/c.txt to /tmp/test/c.txt
/tmp/test/c.txt                                                                                                100%    0     0.0KB/s   00:00    
Uploading /tmp/test/d.txt to /tmp/test/d.txt
/tmp/test/d.txt                                                                                                100%    0     0.0KB/s   00:00    
Uploading /tmp/test/e.txt to /tmp/test/e.txt
/tmp/test/e.txt    

3.6 ssh相关知识点总结

  • 协议:sshd---远程连接(sshd),sftp为加密的远程连接协议,相关软件有openssh.openssh---https。
  • 默认端口22
  • 协议版本1X和2.x,2.x更安全。了解SSH协议原理(ssh连接过程X
  • 服务端ssh远程连接服务,sftp服务。sshd守护进程,开机要自启动。
  • ssh客户端包含ssh,scp,sftp命令。
  • ssh安全验证方式:口令和密钥,这两种都是基于口令的,SSH密钥登录的原理。
  • ssh服务安全优化,修改默认端口22,禁止root远程连接,禁止dns,SSH只监听内网IP
  • ssh密钥对,公钥(publickey)在服务器端,比喻就是锁头,私钥(privatekey)在客户端,比喻就是钥匙。

4 相关知识点补充

4.1 一个服务始终无法启动排错方法

  • 查看服务日志、系统日志
  • 检查服务端口有无冲突

4.2 测试服务端口是否开启

ss -lntup|grep 22

netstat -lntup|grep -w "22"

lsof -i:22

grep  "22/" /etc/services

nmap -p 22 192.168.87.128

nc 192.168.87.128 22

telnet 192.168.87.128 22


END
2019-9-16 11:29:37
原文地址:https://www.cnblogs.com/yj411511/p/11526431.html