一,在A主机里的用户
运行 #ssh-keygen -t rsa # 生成 rsa规则的密钥
结果如下
Generating public/private rsa key pair.
Enter file in which to save the key (/home/.username/ssh/id_rsa):#回车
Enter passphrase (empty for no passphrase):#回车
Enter same passphrase again:#回车
Your identification has been saved in /home/.username /.ssh/id_rsa.
Your public key has been saved in /home/.username /.ssh/id_rsa.pub.
The key fingerprint is:
38:25:c1:4d:5d:d3:89:bb:46:67:bf:52:af:c3:17:0cusername@localhost
Generating RSA keys:
Key generation complete.
会在用户目录~/.ssh/产生两个文件:
id_rsa 、 id_rsa.pub
二,把A主机上的id_rsa.pub文件,拷贝到B主机的root用户主目录下的.ssh目录下,并且改名为authorized_keys (可以不是root用户,其他用户也可以)
即:
/root/.ssh/authorized_keys
或
zzd@gpu3:~/.ssh$ cd ~/.ssh
zzd@gpu3:~/.ssh$ ls
authorized_keys known_hosts
这样在A主机上使用scp命令复制文件到B上将不提示输入密码了,直接复制了。
也可在A主机上使用scp命令将B上文件复制到本机;总之不需要验证,就可以在A访问B
反之亦然!
注:如果有多台计算机需要与B进行复制,则只需将id_rsa.pub中的内容复制到authorized_keys文件中即可。
执行 cat ~/.ssh/a.pub >> ~/.ssh/authorized_keys 将内容追加进去
确保~/.ssh/目录的权限700,~/.ssh/authorized_keys文件的权限是600
三,复制文件或目录命令:
复制文件:
(1)将本地文件拷贝到远程
scp 文件名 用户名@计算机IP: 远程路径目录
scp /root/.jenkins/workspace/superid-webapi-2-0-200/superid-web-app/target/superid-web-app-1.0-SNAPSHOT.war root@192.168.1.200:/home/simu/tomcat/webapps/mk.war
(2)从远程将文件拷回本地
scp 用户名@计算机IP: 文件名 本地路径目录
复制目录:
(1)将本地目录拷贝到远程
scp -r 目录名 --用户名@计算机IP: 远程路径
(2)从远程将目录拷回本地
scp -r 用户名@计算机IP: 目录名 本地路径目录
四、sshd_config配置文件的修改:
修改root的ssh权限,即修改 /etc/ssh/sshd_config文件中 PermitRootLogin no / without-password改为 PermitRootLogin yes
然后重启ssh服务(sudo service ssh restart)
详见:【sshd】sshd_config 中 PermitRootLogin 的forced-commands-only的限定密钥登陆、限定执行命令