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\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication 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  "\b22/\b" /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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM