centos7下ssh服务搭建

一 环境准备

1.配置网络
vi /etc/sysconfig/network-scripts/ifcfg-ens33(ip地址根据自身实际修改)
在这里插入图片描述
保存退出
重启网络:systemctl restart network
2.关闭防火墙和selinux
关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld
关闭selinux:
setenforce 0
sed -i “s/enforcing/permissive/g” /etc/selinux/config
3.搭建本地yum源
在这里插入图片描述
将内容添加到文件中
在这里插入图片描述
创建挂载目录并挂载光盘
在这里插入图片描述
4.安装ssh
centos7默认已安装
可用命令:
systemctl status sshd查看
若没有则执行:
yum -y install openssh安装

二 服务搭建

1.加密算法

  • 对称加密(DES)
    双方使用一个密钥进行交互
    特点:数据传输快,效率高,安全性较低
  • 非对称加密(RSA)
    双方各有两个密钥:一个公钥和一个私钥
    自身的公钥只能解密自身私钥的加密
    自身的私钥也只能解密自身公钥的加密
    特点:数据传输慢,效率低,安全性较高

实际应用中,两者结合使用:
1.使用非对称来传输对称密码。
2.使用对称密码进行数据的加解密。

2.认证原理(交互过程)

1.SSH客户端向SSH服务端发起一个登录请求
2.SSH服务端将自己的公钥发送给SSH客户端
第一次访问时,会出现以下内容:

[root@node2 ~]# ssh 172.16.8.74 (对172.16.8.74远程连接)
#无法确认主机真实性
The authenticity of host '172.16.8.74 (172.16.8.74)' can't be established.
ECDSA key fingerprint is SHA256:4CYyMRIEX6d5yk8uXA+0JMc5USgkT8pU1v+yQ2qELZM.
ECDSA key fingerprint is MD5:ba:57:17:32:fd:f0:cf:de:14:84:a4:4d:ad:02:b2:37.
Are you sure you want to continue connecting (yes/no)? yes #(手动确认)
Warning: Permanently added '172.16.8.74' (ECDSA) to the list of known hosts.
root@172.16.8.74's password: 
Last login: Sat May 15 09:29:42 2021 from 172.16.8.36
[root@node1 ~]# 
#说明:
#当客户端输入yes确认对方的公钥指纹后,server端的公钥就会被存放到客户机的用户家目录里
#~/.ssh/known_hosts文件中,下次再访问就直接通过密码登录,不需要再确认公钥。
#退出
#再次访问时:
[root@node1 ~]# exit
logout
Connection to 172.16.8.74 closed.
[root@node2 ~]# ssh 172.16.8.74
root@172.16.8.74's password: 
Last login: Sat May 15 09:30:32 2021 from 172.16.8.75
[root@node1 ~]# 
#只需要输入密码便可连接
  1. SSH客户端使用服务端发过来的公钥将自己的密码加密并且发送给SSH服务端
  2. SSH服务端收到SSH客户端发过来的加密密码后使用本地留存的私钥进行解密
  3. SSH服务端将解密出来的密码和/etc/shadow文件里的用户密码对比认证
  4. SSH服务端认证成功,则返回登录成功结果,并发送一个随机会话口令给客户端,该口令用于后面两台主机进行数据传输的一个临时加密会话口令

3.使用公钥验证登录(免密登录)

#客户端生成一个密钥对
[root@node2 ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  #/root/.ssh/id_rsa为默认存储密钥对的文件,也可以自己指定存放文件,不过后续免密登录时,需指定密钥文件
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:9UF903za9vo3xKtrF45q8Z+d5HoeI1y9iVHCR9XVtbY root@node2
The key's randomart image is:
+---[RSA 2048]----+
|            .. o@|
|           .. o.O|
|          . .o Oo|
|         . . .* =|
|        S   ...Eo|
|           .. +++|
|            o++Bo|
|           . +=*B|
|          ..o+OB=|
+----[SHA256]-----+
#将密钥复制到服务端上
[root@node2 ~]# ssh-copy-id root@172.16.8.74
/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@172.16.8.74's password:  #输入密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.16.8.74'"
and check to make sure that only the key(s) you wanted were added.
#测试免密登录
[root@node2 ~]# ssh 172.16.8.74             
Last login: Sat May 15 09:34:35 2021 from 172.16.8.75
[root@node1 ~]# 
#成功

4.对已有的ssh私钥加密

[root@node2 ~]# ssh-keygen -p -f .ssh/id_rsa
Enter new passphrase (empty for no passphrase):  #最少需要5位
Enter same passphrase again: 
Your identification has been saved with the new passphrase.

5.使用ssh-agent临时将密语缓存到缓存中

因为前面对ssh私钥进行了加密,所以再次登录node1又需要输入对ssh私钥
加密时的密码

[root@node2 ~]# ssh 172.16.8.74
Enter passphrase for key '/root/.ssh/id_rsa': 
Last login: Sat May 15 09:45:28 2021 from 172.16.8.75

因此引入ssh-agent,它可以将ssh私钥加密的密码临时写入到内存中

#启动ssh-agent
[root@node2 ~]# ssh-agent bash
#添加密码
[root@node2 ~]# ssh-add .ssh/
id_rsa       id_rsa.pub   known_hosts  
[root@node2 ~]# ssh-add .ssh/id_rsa
Enter passphrase for .ssh/id_rsa: 
Identity added: .ssh/id_rsa (.ssh/id_rsa)
[root@node2 ~]# ssh-add -l
2048 SHA256:9UF903za9vo3xKtrF45q8Z+d5HoeI1y9iVHCR9XVtbY .ssh/id_rsa (RSA)
#使用ssh-add -l命令查看当前添加的所有密码
#再次登录node1
[root@node2 ~]# ssh 172.16.8.74
Last login: Sat May 15 09:51:11 2021 from 172.16.8.75
[root@node1 ~]# 
#可直接登录

三 配置文件详解

/etc/ssh/sshd_config

vim /etc/ssh/sshd_config
#常用
#禁止root用户登录(默认允许)
PermitRootLogin no
- 以下为自己添加使用
#禁止指定用户登录
DenyUsers user1
#配置完后node1以user1身份访问node2
[root@node1 ~]# ssh user1@172.16.8.75
user1@172.16.8.75's password: 
Permission denied, please try again.
user1@172.16.8.75's password: 
#访问被拒绝
#允许指定用户登录
AllowUsers user2
#配置完后node1分别以user1和user2身份访问
[root@node1 ~]# ssh user1@172.16.8.75
user1@172.16.8.75's password: 
Permission denied, please try again.
user1@172.16.8.75's password: 

[root@node1 ~]# ssh user2@172.16.8.75
user2@172.16.8.75's password: 
Last login: Sat May 15 19:19:28 2021 from 172.16.8.74
[user2@node2 ~]$ 
#user1访问被拒绝,user2成功登录
#禁止指定ip登录
DenyUsers *@172.16.8.74
#禁止指定ip的指定用户登录
DenyUsers user1@172.16.8.74
#禁止指定网段登录
DenyUsers *@172.16.8.*
#允许指定ip登录
AllowUsers *@172.16.8.74
- 以上为自己添加使用
#禁止使用密码认证(默认允许)
#理解为:仅允许基于密钥验证登录
PasswordAuthentication no

至此,centos7下ssh服务搭建讲解完成

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值