---
title: "ssh-keygen ssh-copy-id 无密码登陆"
author: "stephen830"
date: "2015-05-29T08:59:39Z"
tags: []
categories: ["操作系统"]
slug: "ssh-keygen-ssh-copy-id"
draft: false
lastmod: "2015-09-29T20:15:46Z"
id: 2215251
url: "https://stephen830.iteye.com/blog/2215251"
---

# ssh-keygen ssh-copy-id 无密码登陆

分类： [Ubuntu使用点滴](http://blog.csdn.net/smilettxp/article/category/806378) [Android开发](http://blog.csdn.net/smilettxp/article/category/908787) [Linux资源](http://blog.csdn.net/smilettxp/article/category/789849) 2013-09-04 16:56 1301人阅读 [评论](http://blog.csdn.net/smilettxp/article/details/11074049#comments)(0) [收藏]() [举报](http://blog.csdn.net/smilettxp/article/details/11074049#report "举报")

进行android开发经常需要登录远程ubuntu server服务器进行编译查看代码等，每次都需要输入密码，很烦人。

使用 ssh-keygen和ssh-copy-id配置一下就可以无需输入密码直接登录服务器了。

ssh-keygen 创建公钥和密钥。  
ssh-copy-id 把本地主机的公钥复制到远程主机的authorized\_keys文件上。  
ssh-copy-id 也会给远程主机的用户主目录（home）和~/.ssh, 和~/.ssh/authorized\_keys设置合适的权限 。

 

```
user@machine:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
43:c6:7d:c4:9c:85:bd:8f:f0:5a:b7:8b:37:e6:e9:12 user@machine
The key's randomart image is:
+--[ RSA 2048]----+
|           o.=.  |
|       . . .= .  |
|        + . .  . |
|       o   .. .  |
|        S    o o |
|         .   E+ o|
|             o...|
|            ...=.|
|             .*=o|
+-----------------+
user@machine:~$ 
user@machine:~$ ls -l .ssh/
total 12
-rw------- 1 user user 1679 Sep  4 16:34 id_rsa
-rw-r--r-- 1 user user  393 Sep  4 16:34 id_rsa.pub
-rw-r--r-- 1 user user  222 Sep  3 13:03 known_hosts
user@machine:~$ ssh-copy-id -i .ssh/id_rsa.pub user@172.16.2.15
user@172.16.2.15's password: 
Now try logging into the machine, with "ssh 'user@172.16.2.15'", and check in:

  ~/.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

user@machine:~$ ssh user@172.16.2.15
```

 

 如果有端口号的话：

ssh-copy-id -i ~/.ssh/id\_rsa.pub  **-p 20022** test@192.168.3.2