---
title: "LINUX Centos6 SSH互相无密码访问"
author: "zeallf"
date: "2015-01-14T14:37:21Z"
tags: []
categories: ["操作系统"]
slug: "linux-centos6-ssh"
draft: false
lastmod: "2015-01-14T14:37:21Z"
id: 2175836
url: "https://zeallf.iteye.com/blog/2175836"
---

在CENTOS6下面，　若需要SSH可以无密码访问，　可以做如下处理

1、产生密码，本例是在主机151上执行

```java

ssh-keygen -f /var/lib/pgsql/.ssh/id_rsa -N ''

```



```java

[root@pg1 ~]# ssh-keygen -f /var/lib/pgsql/.ssh/id_rsa -N ''
Generating public/private rsa key pair.
/var/lib/pgsql/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.
Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.
The key fingerprint is:
f9:e5:7e:a6:17:3a:ff:29:db:ae:83:67:98:2e:bc:af root@pg1.localdomain
The key's randomart image is:
+--[ rsa 2048]----+
|                 |
|                 |
|                 |
|         .       |
|        S   .    |
|         . o  .  |
|        . . =. . |
|         o =o== .|
|         E*o=XB=.|
+-----------------+

```



2、将密码复制到想访问的远程主机
本例将151的公钥复制到150和152

```

[root@pg1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.35.150
Now try logging into the machine, with "ssh 'root@192.168.35.150'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@pg1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.35.152
root@192.168.35.152's password:
Now try logging into the machine, with "ssh 'root@192.168.35.152'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

```


3、完成，　验证

> 
> [root@pg0 ~]# ssh pg1
> Last login: Wed Jan 14 14:31:08 2015 from 192.168.35.150
> [root@pg1 ~]# ssh pg2
> Last login: Wed Jan 14 14:31:10 2015 from 192.168.35.151
> 

经验证可以正常访问

另外如果端口不是默认的22，　需要加访问端口

> 
> ssh-copy-id -i /root/.ssh/id_rsa.pub "root@192.168.35.150 -p 242"
>