SSH协议

SSH是什么?

英文: secure shell

shell翻译过来叫: 壳

安全的壳

SSH协议是应用层协议,它的传输层协议是TCP。 第七层
TCP—>面向连接,不能丢失数据

ssh服务就是实现–>远程控制和登陆某台机器,进行操作

ssh是 本质上是网络加密协议

服务的背后就是有个进程,进程是—>sshd—>监听的端口是22
21 ftp文件传输协议
23 telnet 远程控制协议—>明文传输
22 ssh 远程控制协议—>密文传输

在ssh之前是telnet服务

是一个远程控制的服务

在网络是传输的数据是明文的

telnet服务这个远程控制服务很少使用了

telnet是23号端口 ssh是22号端口

查看本机开放的端口

netstat -anplut

[root@Gao-da-hui ~]# netstat -antplu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1011/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1223/master         
tcp        0     36 192.168.2.155:22        192.168.2.108:59940     ESTABLISHED 1526/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      1011/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1223/master         
[root@Gao-da-hui ~]# 

直接用telnet连接的时候,是没有开放端口的,所以连接不上

nc 可以查看对方开了哪个端口 -->有输出就是开放了,连上了
nmap 是批量扫描端口

wireshark 是个抓包工具
科莱 也是
tcpdump

查看本机的进程

ps aux|grep sshd

[root@Gao-da-hui ~]# ps aux|grep sshd
root       1011  0.0  0.1 112900  4300 ?        Ss   09:05   0:00 /usr/sbin/sshd -D
root       1526  0.0  0.1 158904  5596 ?        Rs   09:06   0:00 sshd: root@pts/0
root       1596  0.0  0.0 112828   976 pts/0    S+   09:59   0:00 grep --color=auto sshd
[root@Gao-da-hui ssh]# ps aux|grep sshd
root       1461  0.0  0.1 161512  6088 ?        Ss   10:55   0:00 sshd: root@pts/0
root       1484  0.0  0.1 161512  6088 ?        Ss   10:56   0:00 sshd: root@pts/1
root       1573  0.0  0.1 112900  4304 ?        Ss   10:59   0:00 /usr/sbin/sshd -D
root       1629  0.0  0.0 112824   976 pts/0    S+   11:11   0:00 grep --color=auto sshd

前两行!!!-------->是xshell上的两个端口
第三行----->父进程---->真正提供sshd服务的进程
前两个是进去父进程之后的进程

/etc这个文件

各种杂七杂八放到这,ssh只是其中的一个服务

为什么是22端口

[root@Gao-da-hui ~]# cd /etc/ssh
[root@Gao-da-hui ssh]# ls
moduli       ssh_host_ecdsa_key      ssh_host_ed25519_key.pub
ssh_config   ssh_host_ecdsa_key.pub  ssh_host_rsa_key
sshd_config  ssh_host_ed25519_key    ssh_host_rsa_key.pub
[root@Gao-da-hui ssh]# 

配置文件的本质

sshd_config 是sshd进程的配置文件,配置文件本质上就是传参的,给sshd进程传递参数
参数就是变量

sshd是什么,是进程

ssh服务—>守护进程—>sshd

d是守护进程,就是一直在内存中运行

守护进程是一直在内存中运行的进程,可以人为的停止他
service sshd stop/statt/restart

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
: : 这是ipv6

改端口号,禁用root用户登陆

属于安全加固行为,让系统更加牢固

去 /etc/ssh 中改端口,还要允许root登陆

看日志

tail -f /var/log/secure

secure是安全的意思

自己试一试,改一改端口和禁用root登陆

[root@Gao-da-hui ssh]# vim sshd_config 
[root@Gao-da-hui ssh]# useradd sc
[root@Gao-da-hui ssh]# echo "123456"|passwd sc --stdin
更改用户 sc 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@Gao-da-hui ssh]# 

firewall-cmd --zone=public --add-port=“6677”/tcp --permanent

firewall-cmd --permanent --query-port=“6677”/tcp

重中之重

必须关闭 SElinux,不然起不来
输入:getenforce
看看是不是 Disabled

在 /etc/selinux/config 中改
改成 disabled

setenforce 0 ---->这个也要改一下


root给其他人授权

在 /etc/sudoers
100零几行

image.png

SSH基于公钥,非对称加密

对称加密:两边使用相同的钥匙
非对称: 公钥(公钥给别人)和私钥(自己留着) 两把钥匙不一样 —>安全些
public private
1.数据加密
2.验证身份
天王盖地虎,宝塔镇河妖----->要对的上

image.png

/etc/ssh

以pub结尾的是公钥,没有就是私钥

3种非对称加密算法
ecdsa
rsa
ed25519

查看openSSH版本

[root@Gao-da-hui ssh]# rpm -qa|grep openssh
openssh-clients-7.4p1-21.el7.x86_64
openssh-7.4p1-21.el7.x86_64 --->提供客户端的软件
openssh-server-7.4p1-21.el7.x86_64 -->服务器端的软件

如何判断我的服务起来了没有

第一条看进程

ps aux

第二条看端口号

netstat -anplut

lsof -i:22
lsof -i:6677

ss 也可以看

关闭防火墙

[root@Gao-da-hui sc]# service firewalld stop  立马停止防火墙服务
Redirecting to /bin/systemctl stop firewalld.service
[root@Gao-da-hui sc]# systemctl disable firewalld 关闭开机启动

查询命令是通过什么按住过来的

[root@Gao-da-hui ssh]# which ssh
/usr/bin/ssh
[root@Gao-da-hui ssh]# rpm -qf /bin/ssh
openssh-clients-7.4p1-21.el7.x86_64
[root@Gao-da-hui ssh]# 

加个横幅

image.png

然后写个txt
复制会话!!就可以看了
image.png

阿里云的实现方式是:
image.png

UseDNS no 是干啥的

`UseDNS no`是OpenSSH服务器配置文件(通常是`/etc/ssh/sshd_config`)中的一项配置选项。将其设置为`no`可以禁用SSH服务器在用户登录时进行反向DNS解析。 当SSH服务器启用了反向DNS解析时,它会尝试根据与连接相关的IP地址进行反向DNS查询,以获取连接的客户机的主机名。但是,如果DNS解析服务器响应缓慢或无法响应,这可能会导致SSH连接延迟较长的问题。

通过将`UseDNS`设置为`no`,可以告诉SSH服务器不要进行反向DNS解析,从而避免因DNS解析问题而导致的登录延迟。这对于提高SSH服务器的连接速度和响应性特别有用。 需要注意的是,禁用反向DNS解析可能会导致一些安全风险。因为禁用反向DNS解析可能使得SSH服务器更容易受到IP欺骗(IP spoofing)攻击。如果安全性是一个关键问题,建议在禁用反向DNS解析时采取其他安全措施来保护SSH服务器。

正向解析用的多
反向解析少

DenyUsers,AllowUsers

DenyUsers 拒绝用户从哪里登陆 ------>黑名单
AllowUsers 允许用户从哪里登陆 ------>白名单

不在白名单里的用户不能登陆ssh服务

  1. DenyUsers选项用于指定不允许登录的用户列表。您可以在sshd_config文件中添加以下行来设置拒绝登录的用户:

    plaintextCopy codeDenyUsers user1 user2 user3
    

    其中,user1user2user3是您希望禁止登录的用户名。您可以根据实际需求添加或删除用户名。保存并关闭文件后,重新加载SSH服务器配置,使更改生效。

  2. AllowUsers选项用于指定允许登录的用户列表。您可以在sshd_config文件中添加以下行来设置允许登录的用户:

    plaintextCopy codeAllowUsers user1 user2 user3
    

    其中,user1user2user3是您希望允许登录的用户名。您可以根据实际需求添加或删除用户名。保存并关闭文件后,重新加载SSH服务器配置,使更改生效。 需要注意以下几点:

  • 如果同时使用了DenyUsersAllowUsers选项,AllowUsers的优先级更高。也就是说,如果一个用户同时在DenyUsersAllowUsers列表中,那么该用户将被允许登录。

  • 在配置DenyUsersAllowUsers时,请确保列出的用户是正确的,并且没有遗漏或错误,以免意外阻止或允许错误的用户登录。

  • 在对SSH服务器进行配置更改后,建议重新加载SSH服务器配置,以使更改立即生效。您可以使用以下命令重新加载SSH服务器配置:

    plaintextCopy codesudo systemctl reload sshd
    

使用ssh命令远程登陆

  1. ssh 用户名@服务器地址

  2. ssh -l 用户名 服务器地址

  3. ssh 服务器地址

指定端口号是 -p

在远程机器上执行命令,给了权限就可以

[root@Gao-da-hui ssh]# ssh sc@192.168.2.155 -p 6677 mkdir /quanyan123
The authenticity of host '[192.168.2.155]:6677 ([192.168.2.155]:6677)' can't be established.
ECDSA key fingerprint is SHA256:+ygrXw1zr3TOAQmBMD+Wa0Lk2yN1DTXVq5oROFPhBn4.
ECDSA key fingerprint is MD5:5d:f9:d9:16:bd:98:2c:75:4e:c7:0e:55:07:d7:53:90.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.2.155]:6677' (ECDSA) to the list of known hosts.
welcome to sanchuang web server
sc@192.168.2.155's password: 
mkdir: 无法创建目录"/quanyan123": 权限不够

任务

小任务:
1.启用白名单功能,添加用户machi 设置密码为123456
2.禁用DNS反向解析功能
3.开启横幅功能,登录的时候出现welcome to sanchuang
4.使用ssh命令,指定machi用户登录和端口号 并且执行命令mkdir ~/sc
5.验证效果

[root@Gao-da-hui ssh]# useradd machi
[root@Gao-da-hui ssh]# echo "123456"|passwd machi --stdin
[root@Gao-da-hui ssh]# vim sshd_config 
UseDNS no
AllowUsers machi
Banner /etc/ssh/welcome.txt
[root@Gao-da-hui ssh]# ssh machi@192.168.2.155 -p 6677  mkdir ~/sc
welcome to sanchuang
machi@192.168.2.155's password: 
mkdir: 无法创建目录"/root/sc": 权限不够

解决方法: 
vim /etc/sudoers
root    ALL=(ALL)       ALL
machi   ALL=(ALL)       ALL

ssh的时候加个 sodu -S

[root@Gao-da-hui ssh]# ssh machi@192.168.2.155 -p 6677 "sudo -S mkdir ~/sc"
welcome to sanchuang
machi@192.168.2.155's password: 

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:

    #1) 尊重别人的隐私。
    #2) 输入前要先考虑(后果和风险)。
    #3) 权力越大,责任越大。

[sudo] machi 的密码:123456
[root@Gao-da-hui ssh]# su machi
[machi@Gao-da-hui ssh]$ cd ~/sc
[machi@Gao-da-hui sc]$ 

scp传文件

scp -P 6677 lihaihui.txt machi@192.168.2.198:~

[goweb-~]# scp -P 6677 lihaihui.txt machi@192.168.2.155:~
The authenticity of host '[192.168.2.155]:6677 ([192.168.2.155]:6677)' can't be established.
ECDSA key fingerprint is SHA256:+ygrXw1zr3TOAQmBMD+Wa0Lk2yN1DTXVq5oROFPhBn4.
ECDSA key fingerprint is MD5:5d:f9:d9:16:bd:98:2c:75:4e:c7:0e:55:07:d7:53:90.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.2.155]:6677' (ECDSA) to the list of known hosts.
welcome to sanchuang
machi@192.168.2.155's password: 
lihaihui.txt                                                      100%    0     0.0KB/s   00:00    
[goweb-~]# ^C
[goweb-~]# 

将别人的文件拿过来:
M(34U(8PW(U~KGEOS4620%R_tmb.png

建立免密通道

免除了密码,密钥认证

就是 服务器有一个 know_host 文件,是客户机给的

然后别人又单独给客户机 文件到 ~/.ssh/authorized_keys

前提要求

首先准备两台虚拟机,ip得不一样

如果一样的ip,那就改一下

client: 192.168.50.1
server: 192.168.2.155

单向免密通道:客户机会生成密钥对,公钥和私钥 把公钥传到服务器,对方家目录下
双向就是对方再来一次,很简单

image.png

生成密钥对

[goweb-~]# ssh-keygen
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:lIChxz8d/yihVZC6aOMxxaL4LaBmnBhekcA2kX0PgqA root@goweb
The key's randomart image is:
+---[RSA 2048]----+
|+.= .o. ..       |
|.B =.o ..o       |
|E + *.o.+ .      |
|   +..++ +       |
| . ..+o.S .      |
|+ ..* .+ . o     |
|=+o+ +. . . .    |
|o*o o    .       |
|o  .             |
+----[SHA256]-----+
[goweb-~]# ^C

密钥对存放在 /root/.ssh/ 目录下

将公钥上传到服务器

会存放到 ~/.ssh/authorized_keys

[goweb-.ssh]# ssh-copy-id -i id_rsa.pub -p 22 root@192.168.2.155
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.2.155 (192.168.2.155)' can't be established.
ECDSA key fingerprint is SHA256:+ygrXw1zr3TOAQmBMD+Wa0Lk2yN1DTXVq5oROFPhBn4.
ECDSA key fingerprint is MD5:5d:f9:d9:16:bd:98:2c:75:4e:c7:0e:55:07:d7:53:90.
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
welcome to sanchuang
root@192.168.2.155's password: 

Number of key(s) added: 1

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

[goweb-.ssh]# ^C

试一试登陆:

[goweb-.ssh]# ssh -p '22' 'root@192.168.2.155'
welcome to sanchuang
Last login: Wed Jul 26 16:47:38 2023 from 192.168.2.108

关闭dnat

image.png

image.png

传脚本

image.png

如何管理100台机器,思路

ssh管理100台机器.png

小任务

1.2台机器直接建立双向信任关系的免密通道
2.编写脚本mkdir.sh,批量建立文件夹,或者是空文件
3.远程scp mkdir.sh 到另外一台机器上
4.ssh执行刚刚scp过去的脚本 mkdir.sh
[goweb-sc]# vim mkdir.sh 
[goweb-sc]# cat mkdir.sh
#!/bin/bash

#建立gaohui文件
mkdir /gaohui -p
cd /gaohui/
#循环建立10个空文件,或者.txt文件
for i in {1..10}
do
	touch gaohui$i
	mkdir gaohui$i.txt -p
done

[goweb-sc]# scp -P 22 mkdir.sh root@192.168.2.155:
[goweb-sc]# ssh -p 22 root@192.168.2.155 bash mkdir.sh 
[root@Gao-da-hui /]# cd /gaohui
[root@Gao-da-hui gaohui]# ls
boot         gaohui10.txt  gaohui3      gaohui5      gaohui7      gaohui9
boot.tat.gz  gaohui1.txt   gaohui3.txt  gaohui5.txt  gaohui7.txt  gaohui9.txt
gaohui1      gaohui2       gaohui4      gaohui6      gaohui8
gaohui10     gaohui2.txt   gaohui4.txt  gaohui6.txt  gaohui8.txt
[root@Gao-da-hui gaohui]# 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不冤不乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值