ssh相关命令
1、ssh登录服务器命令实例
[root@backup ~]# #ssh登录服务器命令实例
[root@backup ~]# ssh -p22 root@10.0.0.61
The authenticity of host '10.0.0.61 (10.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:qPpNM+nx1ocU3YpYuWip1Pih3ZAMsFqKOvxYD+JJJCM.
ECDSA key fingerprint is MD5:a6:89:9e:ff:b1:77:b3:23:6a:52:10:31:dd:26:88:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.61' (ECDSA) to the list of known hosts.
root@10.0.0.61's password:
Last login: Mon May 27 14:53:34 2019 from 10.0.0.1
[root@m01 ~]# #-p指定连接远程主机端口 默认22端口可省略
[root@m01 ~]# #@前为用户名 联系当前用户不适用
[root@m01 ~]# #@后为要连接的服务器的ip
2、scp复制数据至远程主机命令(全量复制)
[root@m01 ~]# #scp复制数据至远程主机命令(全量复制)
[root@m01 ~]# #scp连接远程主机命令的基本语法;
[root@m01 ~]# #-p指定端口
[root@m01 ~]# #-r 递归拷贝目录
[root@m01 ~]# #-p(小写)在拷贝文件前后保持文件或目录属性不变
[root@m01 ~]# #-l 限制传输使用带宽
[root@m01 ~]# #推 将本地/tmp/hostname推送至远端服务器10.0.0.41的/tmp目录,使用对端的root用户[root@m01 ~]# scp -P22 -rp /tmp/hostname root@10.0.0.41:/tmp
hostname 100% 7 0.2KB/s 00:00
[root@m01 ~]# #拉 将远程10.0.0.8服务器/tmp/apple文件拉取到本地/opt目录下
[root@m01 ~]# scp -P22 -rp root@10.0.0.8:/tmp/apple /opt
The authenticity of host '10.0.0.8 (10.0.0.8)' can't be established.
ECDSA key fingerprint is SHA256:qPpNM+nx1ocU3YpYuWip1Pih3ZAMsFqKOvxYD+JJJCM.
ECDSA key fingerprint is MD5:a6:89:9e:ff:b1:77:b3:23:6a:52:10:31:dd:26:88:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.8' (ECDSA) to the list of known hosts.
apple 100% 0 0.0KB/s 00:00
[root@m01 ~]# ll /opt
total 4
-rw-r--r-- 1 root root 0 May 27 17:08 apple
-rw-r--r-- 1 root root 7 May 27 10:04 hostname
结论:
1.scp通过ssh协议加密方式进行文件或目录拷贝
2.scp连接时的用户作为拷贝文件或目录的权限
3.scp支持数据推送和拉取,每次都是全量拷贝,效率较低
3、sftp远程数据传输命令
[root@m01 ~]# #默认可以通过sftp命令连接sftp服务
[root@m01 ~]# sftp root@10.0.0.41
Connected to 10.0.0.41.
#sftp使用get下载文件至本地服务器(m01服务器)
sftp> get banana /tmp
Fetching /root/banana to /tmp/banana
#sftp使用put上传本地服务器文件至远程服务器(上传到backup服务器)
sftp> put /tmp/banana /tmp/
Uploading /tmp/banana to /tmp/banana
/tmp/banana 100% 0 0.0KB/s 00:00
#[root@backup ~]# ll /tmp
total 4
-rw-r--r-- 1 root root 0 May 27 17:44 banana
-rw-r--r-- 1 root root 7 May 17 12:27 hostname
ssh连接方式
1、基于用户账户密码远程登录
[root@m01 ~]# ssh -p22 root@10.0.0.41
root@10.0.0.41 password:
[root@backup ~]#
2、基于秘钥远程登录
降低密码泄露的几率和登录的方便性 ,建议使用秘钥验证方式。
[root@m01 ~]# ssh -p22 root@10.0.0.41
Last login: Mon May 27 14:53:39 2019 from 10.0.0.1
[root@backup ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/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:Ji66oeIC+SD1DsOSGckX80+nuUH8MAk7TLb8KZTM+Fs root@backup
The key's randomart image is:
+---[RSA 2048]----+
| |
| o + |
|.. % * . |
|o..o % B . |
| B..o B S |
|O + .+ E . |
|o+.+. = o |
|o..o.o . |
|=.o. |
+----[SHA256]-----+