LoginSignup
6
6

More than 5 years have passed since last update.

ssh-copy-idを使ってssh公開鍵を対象サーバーに配置する

Last updated at Posted at 2015-12-09

ssh-copy-idを使ってssh公開鍵を対象サーバーに配置する

背景

ssh-copy-id という、操作中のユーザーのSSH公開鍵を対象サーバーに簡単に配置するためのコマンドを知ったので使ってみた。

環境

$ uname -a
Darwin genzouw-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64 i386 MacBookPro10,1 Darwin

$ bash -version
GNU bash, バージョン 4.3.42(1)-release (x86_64-apple-darwin14.5.0)

$ zsh --version
zsh 5.1.1 (x86_64-apple-darwin15.0.0)

施行

Vagrantで新しいLinuxサーバーを立てる

$ vagrant init

$ vi Vagrantfile        # ここではすでにBoxを追加していたため、Vagrantfileを編集して利用するBoxを設定した

$ vagrant up

$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ sudo useradd genzouw
[vagrant@vagrant-centos65 ~]$ sudo su - genzouw
[genzouw@vagrant-centos65 ~]$ mkdir .ssh            # sshの鍵の設定はない状態
[genzouw@vagrant-centos65 ~]$ chmod 700 .ssh/
[genzouw@vagrant-centos65 ~]$ exit
logout


[vagrant@vagrant-centos65 ~]$ sudo passwd genzouw   # 初期パスワードを設定
[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.

Mac上に ssh-copy-id がインストールされていなかったのでインストール

$ brew install ssh-copy-id
==> Downloading https://homebrew.bintray.com/bottles/ssh-copy-id-6.8p1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ssh-copy-id-6.8p1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/ssh-copy-id/6.8p1: 7 files, 344K

$ ssh-copy-id                                                                                                      15-12-09 - 21:01:53
Usage: /usr/local/bin/ssh-copy-id [-h|-?|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname

早速鍵の設定

$ ssh-copy-id genzouw@127.0.0.1 -p2222

結果

おもむろにVagrant環境に接続してみる

$ ssh genzouw@127.0.0.1 -p2222

予め作成しておいたsshの鍵がパスワード無しのものだったので、
パスワード無しでログインができる。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6