Linux SSH免密码登陆

需求:在一台服务器上完成所有项目的部署

例如:有服务器A(192.168.0.1)、B(192.168.0.2),分别部署不同的应用,但是来回切换服务器较麻烦,就可以用ssh命令在A服务器上完成两台服务器的部署工作了

步骤:

  1. 在服务器A创建公钥-私钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "注释"

注意: 密钥的文件名称必须是id_xxx, 这里的xxx就是-t参数指定的密钥类型. 比如密钥类型是rsa, 那么密钥文件名就必须是id_rsa.

如果没有安装ssh-keygen,则安装,然后生成

yum install -y ssh-keygen
  1. 进入文件查看创建的公钥-私钥对
[root@VM_0_14_centos ~]# cd ~/.ssh/
[root@VM_0_14_centos .ssh]# ll
total 12
-rw------- 1 root root    0 Dec 17 10:00 authorized_keys
-rw------- 1 root root 1679 Mar  5 13:55 id_rsa
-rw-r--r-- 1 root root  393 Mar  5 13:55 id_rsa.pub
-rw-r--r-- 1 root root  701 Mar  5 13:33 known_hosts
  1. ssh-copy-id把A的公钥发送给B
    默认用法是: ssh-copy-id [email protected], ssh-copy-id命令连接远程服务器时的默认端口是22, 当然可以指定文件、远程主机的IP、用户和端口:
[root@VM_0_14_centos .ssh]# pwd
/root/.ssh
[root@VM_0_14_centos .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22  [email protected]

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -p '22' '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

如果未安装 ssh-copy-id,则首先安装

yum install -y ssh-copy-id
  1. 测试
[root@VM_0_14_centos .ssh]#  ssh [email protected] "cd /usr/local ; ls"
bin
elfin
etc
games
include
lib
lib64
libexec
sbin
share
src

你可能感兴趣的:(Linux)