1、什么是SSH
SSH是一个安全协议,在进行数据传输时,会对数据包进行加密处理,加密后在进行数据传输。确保了数据传输安全。
2、SSH和telnet的对比
1、安装telnet(telnet是明文登录服务器的方式,不支持root用户登录)[root@localhost ~]# yum install telnet-*2、创建一个普通用户
[root@localhost ~]# useradd test[root@localhost ~]# passwd test3、启动telnet服务
[root@localhost ~]# systemctl start telnet.socket4、在windows上的xshell上使用telnet登录服务器
[c:\~]$ telnet test@192.168.230.52
3、SSH的相关命令
ssh root@192.168.230.52
ssh: 是链接的命令
root : 登录的用户
@ :分隔符,没有特殊含义
192.168.230.52 : 登录的机器的IP
-p : 指定端口号(ssh默认情况下是22端口)
-o StrictHostKeyChecking=no : 指定首次登录不提示,输入信息
#推送公钥到 172.16.1.7[root@m01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".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.1.7's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.1.7'"
and check to make sure that only the key(s) you wanted were added.
[root@m01 ~]# #连接测试[root@m01 ~]# ssh 172.16.1.7
Last login: Tue Nov 24 09:02:26 2020 from 10.0.0.1
[root@web01 ~]#
1.终端连接
[C:\~]$ sftp10.0.0.31
2.下载文件
sftp:/data> get 2_nfs.jpg
Fetching /data/2_nfs.jpg to 2_nfs.jpg
sftp: received 29.7 KB in0.03 seconds
3.上传文件
sftp:/data> put
#选择文件
(2)服务器之间连接
1.连接
[root@web01 ~]# sftp 172.16.1.31
root@172.16.1.31's password:
Connected to 172.16.1.31.
sftp>2.操作远程主机
sftp>pwd
Remote working directory: /root
sftp>cd /data
sftp>pwd
Remote working directory: /data
sftp>ls
2_nfs.jpg
sftp>ls -l
-rw-r--r-- 1 www www 30419 Nov 2318:17 2_nfs.jpg
3.操作本机(在命令前面加一个 l ,表示localhost)
sftp> lls -l
total 8
-rw-------. 1 root root 1588 Nov 1712:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Nov 18 09:02 dir1
drwxr-xr-x. 2 root root 6 Nov 18 09:02 dir2
-rw-r--r--. 1 root root 194 Nov 1712:29 rewriteip.sh
4.拉取命令
sftp> get 2_nfs.jpg
Fetching /data/2_nfs.jpg to 2_nfs.jpg
/data/2_nfs.jpg 100% 30KB 3.1MB/s 00:00
sftp> lls -l
total 40
-rw-r--r--. 1 root root 30419 Nov 2410:22 2_nfs.jpg
#指定存储目录拉取
sftp> get 2_nfs.jpg /opt
Fetching /data/2_nfs.jpg to /opt/2_nfs.jpg
/data/2_nfs.jpg 100% 30KB 10.9MB/s 00:00
sftp> lls -l /opt
total 32
-rw-r--r--. 1 root root 30419 Nov 2410:22 2_nfs.jpg
5.推送命令
sftp> put 2_nfs.jpg
Uploading 2_nfs.jpg to /data/2_nfs.jpg
2_nfs.jpg
4、ssh安全优化
# 一、SSH作为远程连接服务,通常我们需要考虑到该服务的安全,所以需要对该服务进行安全方面的配置。1.更改远程连接登陆的端口
2.禁止ROOT管理员直接登录
3.密码认证方式改为密钥认证
4.重要服务不使用公网IP地址
5.使用防火墙限制来源IP地址
# 二、优化的配置[root@m01 ~]# vim /etc/ssh/sshd_config#修改ssh服务的端口
Port 1748#禁止使用root登录服务器
PermitRootLogin no
#禁止使用密码登录服务器
PasswordAuthentication no
UseDNS no # 禁止ssh进行dns反向解析,影响ssh连接效率参数
GSSAPIAuthentication no # 禁止GSS认证,减少连接时产生的延迟