SSH,被称之为 Security Shell,是常见的用于连接服务端终端的工具.虽然它的名字中包含shell,但是它并不是通常意义上所说的bash/ksh这种shell,而只是提供从客户端到服务端的通道,并对通道中传输的数据做自动加密解密.这个通道,不仅限于登录shell,也可以用于文件传送,远程命令执行等等功能.
SSH是基于C/S架构,服务端上有个sshd之类的守护程序,客户端是ssh或者scp等进程.
关于SSH的一些术语:
SSH:SSH协议和产品的通称
SSH-1:SSH协议v1版本,常用的有SSH-1.3和SSH-1.5两个版本
SSH-2:SSH协议的V2版本,在RFC4250-4254中定义.
ssh1:最原始的实现SSH-1的免费软件
ssh2:实现SSH-2的商业软件
OpenSSH:实现SSH-1和SSH-2的开源软件.
我们现在一般都用OpenSSH.以CentOS上的OpenSSH为例.
我们的环境如下:
实验环境
两台机器(容器) ssh_server(172.17.0.2),ssh_client(172.17.0.3)
SSH服务端的简单安装与配置
在CentOS上,有如下OpenSSH安装包:
[root@ssh_server /]# yum search OpenSSH
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* extras: mirrors.sohu.com
* updates: mirrors.sohu.com
============================================================== N/S matched: OpenSSH ==============================================================
openssh-askpass.x86_64 : A passphrase dialog for OpenSSH and X
openssh-keycat.x86_64 : A mls keycat backend for openssh
openssh-server-sysvinit.x86_64 : The SysV initscript to manage the OpenSSH server.
openssh.x86_64 : An open source implementation of SSH protocol versions 1 and 2
openssh-cavs.x86_64 : CAVS tests for FIPS validation
openssh-clients.x86_64 : An open source SSH client applications
openssh-ldap.x86_64 : A LDAP support for open source SSH server daemon
openssh-server.x86_64 : An open source SSH server daemon
Name and summary matches only, use "search all" for everything.
其中openssh-server.x86_64
是服务端软件,openssh.x86_64
是客户端软件.
我们可以通过yum install openssh-server.x86_64
,安装OpenSSH的服务端:
[root@ssh_server /]# yum install openssh-server.x86_64
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* extras: mirrors.sohu.com
* updates: mirrors.sohu.com
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:7.4p1-13.el7_4 will be installed
--> Processing Dependency: openssh = 7.4p1-13.el7_4 for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-6.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-6.el7.x86_64
---> Package openssh.x86_64 0:7.4p1-13.el7_4 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
openssh-server x86_64 7.4p1-13.el7_4 updates 458 k
Installing for dependencies:
fipscheck x86_64 1.4.1-6.el7 base 21 k
fipscheck-lib x86_64 1.4.1-6.el7 base 11 k
openssh x86_64 7.4p1-13.el7_4 updates 509 k
tcp_wrappers-libs x86_64 7.6-77.el7 base 66 k
Transaction Summary
==================================================================================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 1.0 M
Installed size: 3.0 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): fipscheck-lib-1.4.1-6.el7.x86_64.rpm | 11 kB 00:00:00
(2/5): fipscheck-1.4.1-6.el7.x86_64.rpm | 21 kB 00:00:00
(3/5): tcp_wrappers-libs-7.6-77.el7.x86_64.rpm | 66 kB 00:00:00
(4/5): openssh-server-7.4p1-13.el7_4.x86_64.rpm | 458 kB 00:00:00
(5/5): openssh-7.4p1-13.el7_4.x86_64.rpm | 509 kB 00:00:01
--------------------------------------------------------------------------------------------------------------------------------------------------
Total 575 kB/s | 1.0 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : fipscheck-lib-1.4.1-6.el7.x86_64 1/5
Installing : fipscheck-1.4.1-6.el7.x86_64 2/5
Installing : openssh-7.4p1-13.el7_4.x86_64 3/5
Installing : tcp_wrappers-libs-7.6-77.el7.x86_64 4/5
Installing : openssh-server-7.4p1-13.el7_4.x86_64 5/5
Verifying : openssh-7.4p1-13.el7_4.x86_64 1/5
Verifying : openssh-server-7.4p1-13.el7_4.x86_64 2/5
Verifying : fipscheck-1.4.1-6.el7.x86_64 3/5
Verifying : fipscheck-lib-1.4.1-6.el7.x86_64 4/5
Verifying : tcp_wrappers-libs-7.6-77.el7.x86_64 5/5
Installed:
openssh-server.x86_64 0:7.4p1-13.el7_4
Dependency Installed:
fipscheck.x86_64 0:1.4.1-6.el7 fipscheck-lib.x86_64 0:1.4.1-6.el7 openssh.x86_64 0:7.4p1-13.el7_4 tcp_wrappers-libs.x86_64 0:7.6-77.el7
Complete!
在正常的系统上,安装完成ssh的服务端之后,会自动启动对应的sshd服务.在这个实验环境中,我们是在docker里安装OpenSSH服务端,所以需要手工启动sshd进程.
[root@ssh_server /]# netstat -an|grep 22
[root@ssh_server /]# which sshd
/usr/sbin/sshd
[root@ssh_server /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 13:39 pts/0 00:00:00 bash
root 112 1 0 14:00 pts/0 00:00:00 ps -ef
[root@ssh_server /]# nohup /usr/sbin/sshd &
[1] 113
[root@ssh_server /]# nohup: ignoring input and appending output to 'nohup.out'
[1]+ Exit 1 nohup /usr/sbin/sshd
[root@ssh_server /]#
[root@ssh_server /]#
[root@ssh_server /]# cat nohup.out
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
[root@ssh_server /]#
启动后直接就退出了,检查日志,发现是ssh_host_rsa_key等信息没有.如果是Ubuntu机器,可以运行sudo dpkg-reconfigure openssh-server
重新配置服务端进程.这里是Centos机器,可以用手工方式生成key:
[root@ssh_server /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:JbIBOaAsOXr381yZX/nnGOpQIGIO07ZkrY6RsF4xj10 root@ssh_server
The key's randomart image is:
+---[RSA 2048]----+
| .... |
|.o oo . |
|=.. =.X E o |
|o. o / O + . |
|. o * B S . |
| o o = + . |
| . . + = + |
| + . o o +.|
| o .+ ..+|
+----[SHA256]-----+
[root@ssh_server /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
SHA256:++14EgJDu2znUN0AnAp/fj38ZzgyvtB/rMxKC15+Ymc root@ssh_server
The key's randomart image is:
+---[DSA 1024]----+
| ..o |
| . . o . |
| + o . o |
| * o . . |
| . BS o |
| = +.o.+ |
| . +.oo.+o o |
| .o.O*=E =|
| +*OX=* |
+----[SHA256]-----+
[root@ssh_server /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
Generating public/private ecdsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
SHA256:xRaqsG/D2EFWfKbv6Ke8ImjHXBpo3jZ3N1Xp0NVhJLg root@ssh_server
The key's randomart image is:
+---[ECDSA 256]---+
| .. . ...+o|
| ..ooo o o|
| . o .++ .. o |
| = ..o E. + |
| .. o S. + |
| o .=.. . . . |
| o =.+* o . |
| + X.ooo = |
| . o + +== . |
+----[SHA256]-----+
再次执行sshd进程:
[root@ssh_server /]# >nohup.out
[root@ssh_server /]# nohup /usr/sbin/sshd &
[1] 118
[root@ssh_server /]# nohup: ignoring input and appending output to 'nohup.out'
[1]+ Done nohup /usr/sbin/sshd
[root@ssh_server /]# ps -ef|grep sshd
root 119 1 0 23:28 ? 00:00:00 /usr/sbin/sshd
root 121 1 0 23:29 pts/0 00:00:00 grep --color=auto sshd
[root@ssh_server /]# netstat -an|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
[root@ssh_server /]#
从上面的执行过程和结果可以看出,命令成功执行,我们尝试从客户端连接ssh服务端.
首先,修改下root的密码,以方便通过用户名/密码登录(这个容器启动后默认密码未知):
[root@ssh_server /]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password:
passwd: all authentication tokens updated successfully.
[root@ssh_server /]#
—-待续