SSH概述
SSH是一个安全协议,在进行数据传输时,会对数据包进行加密处理,加密后在进行数据传输。确保了数据传输安全。那SSH服务主要功能有哪些呢?
1.提供远程连接服务器的服务
2.对传输的数据进行加密
RDP协议:(remote desktop)3389
SSH协议:(secure shell protocol)22
| 服务 | 端口 |
|---|---|
| SSH | 22 |
| RDP | 3389 |
| HTTP | 80 |
| HTTPS | 443 |
| redis | 6379 |
| mysql | 3306 |
| ftp | 20 21 |
| rsync | 873 |
| DNS | 53 |
| elasticsearch(ES) | 9200 9300 |
那么除了SSH协议能提供远程连接服务,Telnet也能提供远程连接服务, 那么分别的区别是什么呢?
ssh服务会对传输数据进行加密, 监听在本地22/tcp端口, ssh服务默认支持root用户登录
telnet服务不对数据进行加密, 监听在本地23/tcp端口, Telnet默认不支持root用户登录
C/S client server
SSH相关命令
1.ssh
[root@backup ~]# ssh root@10.0.0.7 -p 22
ssh:命令
root:用户
@:分隔符,分隔用户与IP
10.0.0.7:远程机器的IP
p:端口选项
22:指定端口
2.scp命令
scp:命令
-r:递归选项
root:用户
@:分隔符
172.16.1.31:远程服务器地址
:/tmp:远程服务器目录
-l:限速
#推:
[root@web01 html]# scp ./kaoshi.zip root@172.16.1.31:/tmp
[root@web01 ~]# scp -r /etc root@172.16.1.31:/tmp/
#拉:
[root@web01 html]# scp root@172.16.1.31:/tmp/yum.log ./
[root@web01 ~]# scp -r root@172.16.1.31:/tmp ./
#限速
[root@m01 ~]# scp /opt/1.txt root@172.16.1.31:/tmp
root@172.16.1.31 password:
test 100% 656MB '83.9MB/s' 00:07
#限速为8096kb,换算为MB,要除以 8096/8=1024KB=1MB
[root@m01 ~]# scp -rp -l 8096 /opt/1.txt root@172.16.1.31:/tmp
root@172.16.1.31s password:
test 7% 48MB '1.0MB/s' 09:45
3.SFTP
#默认可以通过sftp命令连接sftp服务
sftp root@10.0.0.61
sftp -oPort=52113 root@10.0.0.61 #sftp的特殊端口连接
# sftp使用get下载文件至于本地服务器
sftp> get conf.txt /tmp/
# sftp使用put上传本地服务器文件至远程服务器
sftp> put /root/t1.txt /root/
#上传文件命令:
rz:
1.不支持4G以上的文件
2.不支持目录传输
3.不支持断点续传
#xftp传输:
1.支持4G以上文件传输
2.支持断点续传
3.支持传输目录
#上传文件的工具:
1.xftp
2.filazilla
3.flashFXP
xshell连接不上虚拟机
1.看网络
[root@backup ~]# ping 10.0.0.7
PING 10.0.0.7 (10.0.0.7) 56(84) bytes of data.
64 bytes from 10.0.0.7: icmp_seq=1 ttl=64 time=1.07 ms
64 bytes from 10.0.0.7: icmp_seq=2 ttl=64 time=0.209 ms
[root@backup ~]# tcping 10.0.0.7 22
10.0.0.7 port 22 open.
2.看端口
[root@backup ~]# telnet 10.0.0.7 22
Trying 10.0.0.7...
Connected to 10.0.0.7.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
[root@backup ~]# tcping 10.0.0.7 22
10.0.0.7 port 22 open.
3.网络和端口都通,看防火墙和selinux
[root@backup ~]# systemctl stop firewalld
[root@backup ~]# setenforce 0
setenforce: SELinux is disabled
SSH验证方式
方式一:用户密码验证
当我知道我要连接的服务:IP、用户、密码、端口
[root@backup ~]# ssh -p 22 root@10.0.0.7
root@10.0.0.7's password:
Last login: Thu Dec 5 09:21:52 2019 from 10.0.0.1
[root@web01 ~]#
方式二:密钥方式
1.在准备连接其他机器的那台机器上
[root@m01 ~]# 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:xxWwrFG97V+TSEASxbUSoxnLipiXl86bgBFIVNty84M root@m01
The key's randomart image is:
+---[RSA 2048]----+
|.o.. +BBo. |
|. . o .o*++.. |
| . + + .=o.o+ |
| * * o+ .o.. |
| + E *S o ... .|
| + + .. ..o.|
| . . o .o|
| . o .|
| o |
+----[SHA256]-----+
[root@m01 ~]#
2.推送公钥至免密登录的服务器
#方法1:
#在家目录下创建.ssh目录,授权为700
[root@web01 ~]# mkdir ~/.ssh -p
[root@web01 .ssh]# chmod 700 ~/.ssh/
#在.ssh目录下编辑 authorized_keys,内容为m01的公钥 id_rsa.pub
[root@web01 .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4dpzj1fMLAIhtbQpZjDyR7hJ1BzSPwZ6CzN/ABfeagbUKUcgsCrDpP9M7C1M0jkeKLwDCQVqNy4j3LqsZBgQsrlgPmf0cDaw3wpdUPzQtCe6zK0TP6hz/gzbq/Mje5e74sn607hFIY0cQgnruPbGzLOBYhnOQRJXZVNY5M53NUsEnQGUcGHQ4X5mD4tenHpHZSgqMDt/Fqnbyor0QAIO1RULoPgIPMWkrRv3ayPz1KV2DJRzN3Y8LKcxF7v1a1oF5Qv2VfSpVUJI2b7cDxvCnyQrbEi4yZx7gFK5uqyFc6iMYMEcuM04Q44M77vVRSay2Tsou9VPhPSE9nAc8JYyD root@m01
#授权密码文件
[root@web01 .ssh]# chmod 600 authorized_keys
#方法二:
[root@m01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.1.31
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:U3g1ySF4cjqUOKLZYeztSXhKHKK+HbCxwjB7hmKAxYA.
ECDSA key fingerprint is MD5:d9:dd:3e:6a:a0:1c:b7:9e:e2:97:fe:86:19:08:f7:d7.
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.16.1.31's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.1.31'"
and check to make sure that only the key(s) you wanted were added.
[root@m01 ~]#
SSH安全优化
SSH作为远程连接服务,通常我们需要考虑到该服务的安全,所以需要对该服务进行安全方面的配置。
1.更改远程连接登陆的端口
2.禁止ROOT管理员直接登录
3.密码认证方式改为密钥认证
4.重要服务不使用公网IP地址
5.使用防火墙限制来源IP地址
SSH服务登录防护需进行如下配置调整,先对如下参数进行了解
Port 6666 # 变更SSH服务远程连接端口
PermitRootLogin no # 禁止root用户直接远程登录
PasswordAuthentication no # 禁止使用密码直接远程登录
UseDNS no # 禁止ssh进行dns反向解析,影响ssh连接效率参数
GSSAPIAuthentication no # 禁止GSS认证,减少连接时产生的延迟
优化步骤:
1.先创建用户
[root@m01 ~]# useradd lhd
[root@m01 ~]# passwd lhd
Changing password for user lhd.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@m01 ~]#
2.在普通用户的加目录下添加公钥
#切换普通用户
[root@m01 ~]# su - lhd
#在家目录下创建.ssh目录,授权为700
[lhd@m01 ~]# cd /home/lhd/
[lhd@m01 lhd]# mkdir .ssh
[lhd@m01 lhd]# chmod 700.ssh
#在.ssh目录下编辑 authorized_keys,内容为xshell的公钥
[lhd@m01 .ssh]# vim authorized_keys
#授权密码文件
[lhd@m01 .ssh]# chmod 600 authorized_keys
########################################################
#为了避免密码文件权限出现问题,我们可以推送一次公钥
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub lhd@172.16.1.61
#然后添加公钥内容到文件
[root@m01 ~]# vim /home/lhd/.ssh/authorized_keys
3.修改优化项目
[root@m01 ~]# vim /etc/ssh/sshd_config
Port 6666
PermitRootLogin no
PasswordAuthentication no
[root@m01 ~]# systemctl restart sshd
4.在xshell测试使用root用户加密码连接服务器22端口,肯定是行不通的
免交互expect[扩展]
1.安装expect
[root@m01 ~]# yum install -y expect
2.编写expect脚本
#!/usr/bin/expect
set ip 10.0.0.51
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"(yes/no)" {send "yes\r"; exp_continue}
"password:" {send "$pass\r"}
}
expect "root@*" {send "df -h\r"}
expect "root@*" {send "exit\r"}
expect eof
免交互sshpass[扩展]
1.安装sshpass
[root@m01 ~]# yum install -y sshpass
2.使用sshpass命令
[root@m01 ~]# sshpass -p 1 ssh root@10.0.0.7
[root@m01 ~]# sshpass -p 1 ssh -o stricthostkeychecking=no root@10.0.0.7
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
Last login: Thu Dec 5 12:13:08 2019 from 10.0.0.61
[root@web01 ~]#
[root@m01 ~]# sshpass -p 123456 ssh root@10.0.0.51
[option]
-p:指定密码
-f:从文件中取密码
-e:从环境变量中取密码
-P:设置密码提示