● 生成本机的密码(私钥和公钥)
#一直回车下去即可(敲三下)
[root@localhost ~]# ssh-key
ssh-keygen ssh-keyscan
[root@localhost ~]# 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)?
[root@localhost ~]#
● 拷贝公钥至需要远程登录的服务器
[root@localhost .ssh]# scp ./id_rsa.pub 192.168.0.22:/root/.ssh/id.11
[email protected]'s password:
id_rsa.pub 100% 408 0.4KB/s 00:00
[root@localhost .ssh]# ll
total 16
-rw-r--r-- 1 root root 408 Oct 26 16:17 id.11
-rw------- 1 root root 1679 Oct 26 15:20 id_rsa
-rw-r--r-- 1 root root 408 Oct 26 15:20 id_rsa.pub
-rw-r--r-- 1 root root 174 Oct 26 16:03 known_hosts
[root@localhost .ssh]# pwd
/root/.ssh
● 若/root/.ssh目录下无authorized_keys,则手动创建一个文件
[root@localhost .ssh]# cat id.11 >> authorized_keys
● 此时连接远程服务器无需再输入密码
[root@localhost .ssh]# ssh 192.168.0.22
Last failed login: Fri Oct 26 16:15:25 GST 2018 from 192.168.0.22 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Fri Oct 26 16:04:02 2018 from 192.168.0.22
[root@localhost ~]#
● 将远端服务器文件拷贝至本地
[root@localhost ~]# scp -r 192.168.0.22:/root/hello ./
hello
● 将本地文件拷贝至远端服务器
#a 本地当前目录文件或目录
[root@localhost ~]# scp -r a 192.168.0.22:/root
a
● 当远端服务器默认端口(22)发生改变,则如下:
scp -P 端口号 -r a 192.168.0.22:/root
● scp 一些参数详解
-1: 强制scp命令使用协议ssh1
-2: 强制scp命令使用协议ssh2
-4: 强制scp命令只使用IPv4寻址
-6: 强制scp命令只使用IPv6寻址
-B: 使用批处理模式(传输过程中不询问传输口令或短语)
-C: 允许压缩。(将-C标志传递给ssh,从而打开压缩功能)
-p:保留原文件的修改时间,访问时间和访问权限。
-q: 不显示传输进度条。
-r: 递归复制整个目录。
-v:详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
-c cipher: 以cipher将数据传输进行加密,这个选项将直接传递给ssh。
-F ssh_config: 指定一个替代的ssh配置文件,此参数直接传递给ssh。
-i identity_file: 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l limit: 限定用户所能使用的带宽,以Kbit/s为单位。
-o ssh_option: 如果习惯于使用ssh_config(5)中的参数传递方式,
-P port:注意是大写的P, port是指定数据传输用到的端口号
-S program: 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。