SSH
ssh是什么?有什么用?用在哪里?
secure shell 安全的shell
对传输的数据进行加密,保护传输数据的安全
ssh理解是一个协议,用来远程控制服务器的
使用在 linux/unix系统里,远程控制服务器
如何搭建ssh服务?
软件来实现某个功能--》远程登录
openssh
OpenSSH是使用SSH协议进行远程登录的首要连接工具。它对所有通信进行加密,以消除窃听、连接劫持和其他攻击。此外,OpenSSH提供了大量的安全隧道功能、几种身份验证方法和复杂的配置选项。
centos7/8/6安装好系统,默认安装openssh
[root@nginx-kafka01 ssh]# rpm -qa |grep openssh
openssh-server-7.4p1-21.el7.x86_64
openssh-clients-7.4p1-21.el7.x86_64
openssh-7.4p1-21.el7.x86_64
[root@nginx-kafka01 ssh]#
openssh 提供某些ssh的工具--》客户端
openssh-clients 客户端程序
openssh-server 服务器端
c/s架构的软件
redhat package managment 红帽公司出品的软件包管理软件
rpm 是linux里的一个软件管理的命令
在redhat/centos里有使用 oracle linux
-q 查询 query
a all
rpm -qa 查询在本机已经安装的所有的软件
selinux 是linux系统里的一套安全机制,用来保护linux系统的安全,会限制进程去做某些对安全有威胁的事情。
永久修改selinux的策略为disabled
[root@localhost ssh]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ssh]# getenforce 查看selinux的状态
Enforcing
[root@localhost ssh]# setenforce 0 临时调整selinux的策略为宽容模式
[root@localhost ssh]# getenforce
Permissive
[root@localhost ssh]#
[root@localhost ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
[root@localhost ssh]# netstat -anplut|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2383/ssh
关闭防火墙
[root@localhost ssh]# service firewalld stop 关闭防火墙
Redirecting to /bin/systemctl stop firewalld.service
ssh安装位置以及其配置文件
[root@nginx-kafka01 ssh]# cd /etc/ssh/
[root@nginx-kafka01 ssh]# ls
moduli sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
sshd_config --》sshd 服务器端的进程
ssh_config --》ssh 客户端命令的配置文件
如何知道sshd服务是否启动?
1.看进程
2.看端口
3.直接访问
4.看日志
[root@nginx-kafka01 ssh]# ps aux|grep sshd
root 1077 0.0 0.1 112924 4332 ? Ss 15:45 0:00 /usr/sbin/sshd -D
root 1697 0.0 0.1 161536 6088 ? Ss 15:53 0:00 sshd: root@pts/0
root 1843 0.0 0.1 161536 6092 ? Ss 16:06 0:00 sshd: root@pts/1
root 2078 0.0 0.0 112828 988 pts/1 S+ 17:18 0:00 grep --color=auto sshd
[root@nginx-kafka01 ssh]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1077 root 3u IPv4 21567 0t0 TCP *:ssh (LISTEN)
sshd 1077 root 4u IPv6 21569 0t0 TCP *:ssh (LISTEN)
sshd 1697 root 3u IPv4 23751 0t0 TCP nginx-kafka01:ssh->192.168.1.107:55849 (ESTABLISHED)
sshd 1843 root 3u IPv4 25789 0t0 TCP nginx-kafka01:ssh->192.168.1.107:56096 (ESTABLISHED)
[root@nginx-kafka01 ssh]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1077 root 3u IPv4 21567 0t0 TCP *:ssh (LISTEN)
sshd 1077 root 4u IPv6 21569 0t0 TCP *:ssh (LISTEN)
sshd 1697 root 3u IPv4 23751 0t0 TCP nginx-kafka01:ssh->192.168.1.107:55849 (ESTABLISHED)
sshd 1843 root 3u IPv4 25789 0t0 TCP nginx-kafka01:ssh->192.168.1.107:56096 (ESTABLISHED)
看日志 /var/log/secure
/var --->variable 变量
log 日志
ssh daemon :sshd
firewalld
守护进程: daemon :一直在内存里运行的程序,除非人为的停止
如何优化加固ssh服务?
1.修改端口
2.禁用root用户
3.升级openssh
4.密钥认证
5.密码认证
默认开启的
经常更换密码+设置复杂的密码
scp 远程拷贝 底层通过ssh协议远程连接到其他的机器上,复制文件
ssh copy
[root@localhost ssh]# ssh cali@192.168.1.173 mkdir /home/cali/fengdeyong/zhangtongtong -p
cali@192.168.1.173's password:
[root@localhost ssh]#
[root@localhost ssh]# scp cali@192.168.1.173:/etc/passwd /etc/ssh
cali@192.168.1.173's password:
passwd 100% 1217 677.1KB/s 00:00
[root@localhost ssh]# ls
moduli ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
passwd sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
[root@localhost ssh]# pwd
/etc/ssh
[root@localhost ssh]#
[root@localhost ssh]# scp -r cali@192.168.1.173:/boot /etc/ssh
源 目的地
[root@localhost ssh]# scp -r /etc/passwd cali@192.168.1.173:~
cali@192.168.1.173's password:
passwd 100% 1692 745.6KB/s 00:00
[root@localhost ssh]#
sftp ssh协议实现ftp功能,文件传输功能
[root@localhost ssh]# sftp cali@192.168.1.173
ls
!ls
pwd
lpwd
cd
lcd
get 下载
put 上传
put -r 上传文件夹
sftp> put -r grub2
exit
免密通道的搭建
1.先关闭 防火墙 和 selinux
2.准备2台服务器
A:192.168.52.136
B:192.168.52.138
1.生成密钥对,在192.168.52.136上使用root用户生成密钥对,密钥对会在用户的家目录下
[root@localhost ~]# ssh-keygen -t rsa (-t rsa)可以不接
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): (直接按enter)
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again: (直接按enter)
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: (直接按enter)
SHA256:YhoQBeYoT7tmpigvG4LKb5FJ3qb4qrWq/SWNjfSJbEU root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| +o. |
| + . |
|o + E |
|.o + . |
| = * + S |
|. O # o |
|+ B % * |
|BX = o |
|%BO+. |
+----[SHA256]-----+
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa id_rsa.pub
[root@localhost .ssh]# ll
总用量 8
-rw-------. 1 root root 1675 8月 8 14:50 id_rsa
-rw-r--r--. 1 root root 408 8月 8 14:50 id_rsa.pub
id_rsa w为私钥 id_rsa.pub为公钥
2.上传公钥到对方的服务器,要求对方的服务器运行root用户登录
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.52.136 //公钥这里用的是相对地址
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.52.136 (192.168.52.136)' can't be established.
ECDSA key fingerprint is SHA256:RbOKienlYdeiA2ed0RGsPXuRSCrU4dScbX6swpg7DF0.
ECDSA key fingerprint is MD5:23:a0:45:a8:74:f8:bf:a6:1e:cd:11:69:a7:ad:15:2a.
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@192.168.52.136's password: (目的主机的root密码)
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.52.136'"
and check to make sure that only the key(s) you wanted were added.
会在自己的/root/.ssh 目录下生成known_hosts文件里面存放以经免密的主机信息
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
会在被免密的机器的/root/.ssh目录下生成authorized_keys文件里面存放A主机公钥
[root@nginx-kafka01 ssh]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
默认端口22
如果需要指定端口 这样可以增加安全性
加一个 -p 选项 加上端口号
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub -p 2277 root@192.168.52.136
不过 需要将对方的ssh 服务的端口号改为你需要的端口
修改
[root@nginx-kafka01 .ssh]# cd /etc/ssh
[root@nginx-kafka01 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@nginx-kafka01 ssh]# vim /etc/ssh/sshd_config
然后 重启一下ssh服务
systemctl restart sshd
3.验证登录是否需要密码
Connection to 192.168.52.136 closed.
[root@localhost .ssh]# ssh root@192.168.52.136
Last login: Mon Aug 8 15:01:48 2022 from 192.168.52.138
[root@nginx-kafka01 ~]#
完成单向免密
信任关系:
单向信任关系:一边进行了免密通道的操作
双向信任关系:俩边都进行免密通道的方式
如果要完成双向信任关系就在B机器上重复以上操作