layout: post
title: ssh服务
date: 2018-01-09
tags: [“Linux”,”远程登录与文件传输工具”]
一、openssh简介
1、ssh(secure shell protocol)基于tcp协议下的22端口, 安全的远程登录
2、两种方式的用户登录认证:
基于password 基于DH算法做密钥交换
基于key 基于RSA或DSA实现身份认证
3、openssh基于C/S结构,Client: ssh, scp, sftp,slogin,xshell, putty, securecrt, sshsecureshellclient;Serve:sshd
4、客户端配置文件:/etc/ssh/ssh_config;服务端配置文件:/etc/ssh/sshd_config
5、客户端的使用格式
ssh [user@]host [COMMAND]
ssh [-l user] host [COMMAND]
-p port:远程服务器监听的端口
-b:指定连接的源IP(客户端有多个ip地址,指定使用哪个ip去连接)
-v:调试模式(可用于排错)
-C:压缩方式
-X: 支持x11转发
-Y:支持信任x11转发
ForwardX11Trusted yes
-t: 强制伪tty分配(跳板机)
ssh -t remoteserver1 ssh remoteserver2
6、允许实现对远程系统经验证地加密安全访问,当用户远程连接ssh服务器时,会复制ssh服务器/etc/ssh/ssh_host*key.pub(CentOS7默认是ssh_host_ecdsa_key.pub)文件中的公钥到客户机的~./ssh/know_hosts中。下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接。
7、 基于用户和口令的登录验证方式的连接过程
1)、 客户端发起ssh请求,服务器会把自己的公钥发送给用户
2 )、用户会根据服务器发来的公钥对密码进行加密
3 )、加密后的信息回传给服务器,服务器用自己的私钥解密,如果密码正确,则用户登录成功
8、基于密钥的登录方式
1)、首先在客户端生成一对密钥(ssh-keygen)
2)、 并将客户端的公钥ssh-copy-id 拷贝到服务端
3)、 当客户端再次发送一个连接请求,包括ip、用户名
4)、 服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如:acdf
5)、 服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端
6)、 得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的字符串发送给服务端
7)、服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一致,就允许免密码登录
二、基于用户名密码的验证方式的方式登录Linux主机
我们使用三台Linux服务器做实验
- 1、使用ssh登录主机
- [root@newhostname ~]# ssh root@172.18.30.253
- root@172.18.30.253's password:
- Last login: Tue Jan 9 09:10:07 2018 from 172.18.101.180
- 2、使用指定接口登录ssh主机
- [root@newhostname ~]# ssh -b 172.18.30.253 172.18.30.254
- root@172.18.30.254's password:
- Last login: Tue Jan 9 08:22:24 2018 from 172.18.101.180
- 指定使用172.18.30.253来连接host2
三、基于key验证登录Linux主机
- 3、通过跳板机进行连接(角色分配:cent7 172.18.30.253/16 作为客户端;cent6 172.18.30.254/16 作为跳板机,gentoo 172.18.30.100/16 作为服务器来操作)
- [root@joker-6-01 ~]# ssh -t 172.18.30.254 ssh 172.18.30.100
- root@172.18.30.254's password:
- The authenticity of host '172.18.30.100 (172.18.30.100)' can't be established.
- RSA key fingerprint is b6:48:a5:b0:b8:ff:e1:f8:1d:99:27:86:c5:a4:c3:34.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '172.18.30.100' (RSA) to the list of known hosts.
- Password:
- joker ~ #
- 通过伪终端登录,目标主机显示连接的foreign ip地址是跳板机。
- 我们在getoo上看一下实际的socket3
- joker /etc/ssh # ss
- Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
- tcp ESTAB 0 0 172.18.30.100:ssh 172.18.30.254:40646 #centos6的地址
- tcp ESTAB 0 0 172.18.30.100:ssh 172.18.101.180:50821
- joker /etc/ssh #
- 1、生成自己的公私钥(对客户端操作,我现在登录的是gentoo)
- joker /etc/ssh # ssh-keygen
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.ssh/id_rsa):
- Created directory '/root/.ssh'.
- 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:u7zpUjmNV3zx4XLTbplpZYc7tMmLvt4e2XZ3Vasspkk root@joker
- The key's randomart image is:
- +---[RSA 2048]----+
- ' '
- ' .. '
- ' . .++'
- ' o.++O'
- ' S+ . +oBO'
- ' =.o . BB+'
- ' ..E o +++*'
- ' ...o+ o..o+'
- ' o*+ o+oo '
- +----[SHA256]-----+
- 系统默认会在/root/.ssh/下生成id_rsa(私钥) 、id_rsa.pub(公钥),后续如果希望对私钥加密,则输入密码,不想加密则一路回车。
- 2、将生成的公钥文件内容拷贝到目标server的/root/.ssh/authorized_keys这个文件中,可使用ssh-copy-id命令,避免拷贝错钥匙
- joker /etc/ssh # ssh-copy-id 172.18.30.253
- /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
- The authenticity of host '172.18.30.253 (172.18.30.253)' can't be established.
- ECDSA key fingerprint is SHA256:cJiwhG4qYrpipaouDkuKIxxLnszvnEZkR8mad+EWfsU.
- Are you sure you want to continue connecting (yes/no)? 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@172.18.30.253's password:
- Number of key(s) added: 1
- Now try logging into the machine, with: "ssh '172.18.30.253'"
- and check to make sure that only the key(s) you wanted were added.
- 现在我们将gentoo的公钥成功拷贝到了centos7上,现在使用ssh来连接centos7
- joker /etc/ssh # ssh 172.18.30.253
- Last login: Tue Jan 9 09:10:16 2018 from 172.18.30.253
- [root@newhostname ~]#
- 直接免密登录了
使用CRT生成公私钥,如下图 ,选择Create Public Key
一路下一步,最后选择将key保存到当前用的家目录
秘钥生成好了,但是有一个问题,那就是CRT的秘钥文件格式和openssh的秘钥文件格式不一样,我们需要将其转化才可以使用。
第一步:
我们可以通过服务器端执行
ssh-keygen -i -f Identity.pub >> ~/.ssh/authorized_keys
将秘钥格式转换成openssh的格式,并将公钥导入到~/.ssh/authorized_keys 这个文件中(Identify.pub公钥需要实现拷贝到server端上)
第二步:
将生成私钥文件导入到CRT中
将私钥文件导入
可以使用key来访问server了
四、ssh托管工具
我们在生成私钥文件时,有时由于私钥的重要性,我们会在生成私钥时对其进行对称加密,那么当我们在使用key认证登录的时候,需要输入私钥的秘钥才可以,而且是每次使用每次都需要输入,对我们的工作带来了不便,这时候我们可以开启代理托管工具,来实现只输入一次秘钥,就可以登录,但是这个功能只能在当前终端生效,退出终端则会失效。
命令:
ssh-agent bash 开始代理
ssh-add 将私钥密码加入
使用代理之后,我们可以对一些基于ssh的自动化软件,放一些心了。
五、openssh的优化
修改/etc/ssh/sshd_config 文件中的选项为下面的样式
UseDNS no #将链接地址解析成域名,我们也用不到,会降低ssh的连接速度,我们改成”no”
GSSAPIAuthentication no #这一项认证我们用不到,且其会降低ssh的连接速度,我们改成”no”
PermitRootLogin yes #允许root远程登录
六、还有一些其他的ssh工具,如dropbear等。openssh还是比较常用的,在这里就不再讲其他的开源软件了,如果感兴趣可以自行搜索相关文档。