質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

解決済

2回答

13108閲覧

vagrant sshで毎回パスワードを聞かれるのをなくしたい

marimo.

総合スコア48

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

0グッド

1クリップ

投稿2016/06/06 03:21

編集2016/06/08 14:45

vagrant upのあとにvagrant sshでゲストPCにログインできると思うのですが、その際に毎回パスワードを聞かれます.

Shell

1work hogeTaro$ vagrant ssh-config 2Host default 3 HostName 127.0.0.1 4 User vagrant 5 Port 2222 6 UserKnownHostsFile /dev/null 7 StrictHostKeyChecking no 8 PasswordAuthentication no 9 IdentityFile "/Users/hogeTaro/work/.vagrant/machines/default/virtualbox/private_key" 10 IdentitiesOnly yes 11 LogLevel FATAL

とssh-configを見る限りはIdentityFileで秘密鍵を指定しているのですが、これだけではダメなのでしょうか?例えばVagrantは~/.ssh/configも見に行っているとか、そういうことなんでしょうか?(sshに関する知識が中途半端ですいません)

vagarnt sshでパスワードを入力ぜずにログインできる方法を教えていただけますと幸いです。
よろしくお願い致します。

Vagrant 1.8.1

Vagrantfile

1# -*- mode: ruby -*- 2# vi: set ft=ruby : 3 4# All Vagrant configuration is done below. The "2" in Vagrant.configure 5# configures the configuration version (we support older styles for 6# backwards compatibility). 7# you're doing. 8Vagrant.configure(2) do |config| 9 # The most common configuration options are documented and commented below. 10 # For a complete reference, please see the online documentation at 11 # https://docs.vagrantup.com. 12 13 # Every Vagrant development environment requires a box. You can search for 14 # boxes at https://atlas.hashicorp.com/search. 15 config.vm.box = "debian/jessie64" 16 17 # Disable automatic box update checking. If you disable this, then 18 # boxes will only be checked for updates when the user runs 19 # `vagrant box outdated`. This is not recommended. 20 # config.vm.box_check_update = false 21 22 # Create a forwarded port mapping which allows access to a specific port 23 # within the machine from a port on the host machine. In the example below, 24 # accessing "localhost:8080" will access port 80 on the guest machine. 25 # config.vm.network "forwarded_port", guest: 80, host: 8080 26 27 # Create a private network, which allows host-only access to the machine 28 # using a specific IP. 29 config.vm.network "private_network", ip: "192.168.33.10" 30 31 # Create a public network, which generally matched to bridged network. 32 # Bridged networks make the machine appear as another physical device on 33 # your network. 34 # config.vm.network "public_network" 35 36 # Share an additional folder to the guest VM. The first argument is 37 # the path on the host to the actual folder. The second argument is 38 # the path on the guest to mount the folder. And the optional third 39 # argument is a set of non-required options. 40 config.vm.synced_folder "./", "/home/vagrant/" 41 42 # Provider-specific configuration so you can fine-tune various 43 # backing providers for Vagrant. These expose provider-specific options. 44 # Example for VirtualBox: 45 # 46 # config.vm.provider "virtualbox" do |vb| 47 # # Display the VirtualBox GUI when booting the machine 48 # vb.gui = true 49 # 50 # # Customize the amount of memory on the VM: 51 # vb.memory = "1024" 52 # end 53 # 54 # View the documentation for the provider you are using for more 55 # information on available options. 56 57 # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies 58 # such as FTP and Heroku are also available. See the documentation at 59 # https://docs.vagrantup.com/v2/push/atlas.html for more information. 60 # config.push.define "atlas" do |push| 61 # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" 62 # end 63 64 # Enable provisioning with a shell script. Additional provisioners such as 65 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the 66 # documentation for more information about their specific syntax and use. 67 # config.vm.provision "shell", inline: <<-SHELL 68 # sudo apt-get update 69 # sudo apt-get install -y apache2 70 # SHELL 71end

shell

1$ vagrant up 2Bringing machine 'default' up with 'virtualbox' provider... 3==> default: Checking if box 'debian/jessie64' is up to date... 4==> default: A newer version of the box 'debian/jessie64' is available! You currently 5==> default: have version '8.4.0'. The latest is version '8.5.0'. Run 6==> default: `vagrant box update` to update. 7==> default: Clearing any previously set forwarded ports... 8==> default: Clearing any previously set network interfaces... 9==> default: Preparing network interfaces based on configuration... 10 default: Adapter 1: nat 11 default: Adapter 2: hostonly 12==> default: Forwarding ports... 13 default: 22 (guest) => 2222 (host) (adapter 1) 14==> default: Booting VM... 15==> default: Waiting for machine to boot. This may take a few minutes... 16 default: SSH address: 127.0.0.1:2222 17 default: SSH username: vagrant 18 default: SSH auth method: private key 19==> default: Machine booted and ready! 20GuestAdditions 5.0.16 running --- OK. 21==> default: Checking for guest additions in VM... 22==> default: Configuring and enabling network interfaces... 23==> default: Rsyncing folder: /Users/hogeTaro/work/ => /vagrant 24==> default: Mounting shared folders... 25 default: /home/vagrant => /Users/hogeTaro/work 26==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` 27==> default: flag to force provisioning. Provisioners marked to run always will still run.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

公開鍵認証には公開鍵が必要です。
Vagrantは対象となるVMの~/.ssh/autorized_keysに公開鍵情報を追記してくれます。
まずはこちらのファイルが存在するか確認してみましょう。(パーミッションも)

秘密鍵と公開鍵のペアを生成
→公開鍵をログインしたい先の~/.ssh/autorized_keysに追記
→秘密鍵を指定してssh接続

という流れは一般的なSSH公開鍵認証方式です。

投稿2016/06/06 04:01

編集2016/06/06 04:02
romiogaku

総合スコア546

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

marimo.

2016/06/06 04:10

~/.ssh/以下にはautorized_keysは存在していませんね。 vagrant up時に~/.ssh/autorized_keysは自動生成されないのでしょうか? 加えてもう1点、秘密鍵の場所はIdentityFileで指定されている箇所にあるとは思いますが、vagrant ssh接続するゲストPCへの公開鍵は通常どこに生成されるのでしょうか?(vagrant up時に秘密鍵、公開鍵ともにが生成される?)
romiogaku

2016/06/06 04:57 編集

通常vagrant up時に自動生成されます。何かの拍子に削除してしまった可能性もあります。 一度VMを破棄して再度立ち上げてみると良いかもしれません。 (代わりにssh-keygenコマンドを使って秘密鍵から公開鍵を生成しても良いですし、自分で秘密鍵と公開鍵を作りなおしても良いです。) 秘密鍵と公開鍵は初回vagrant up時に生成されます。 公開鍵はゲスト側の~/.ssh/autorized_keysに追記されます。
marimo.

2016/06/06 05:43 編集

ホストPCとゲストPCを誤読しました。すいません。 ホスト側で~/.ssh/authorized_keysを探してました(よくよく考えたらsshする側に公開鍵は必要ないですよね)。ゲスト側(VM側)にはそもそも~/.ssh/(~ = /home/vagrant)が存在していません。ちなみにvagrant destroyしても.sshは作成されませんでした。 ゲスト側の公開鍵の場所をホスト側で教えて上げる必要はあるのでしょうか?(例えばVagrantfileに記述するとか) また、ゲスト側に~/.ssh/autorized_keysが作成されなかったのはどんな原因があるのでしょうか?(vagrantが正しくインストールされなかった?)
romiogaku

2016/06/06 06:03

>ゲスト側の公開鍵の場所をホスト側で教えて上げる必要はあるのでしょうか 通常その必要はありません。 生成されなかった原因についてはちょっと特定難しいですね。 差し支えなければVagrantfileとvagrantのバージョン、vagrant up時のコンソールについて記載してもらえますか。
marimo.

2016/06/08 14:38 編集

コードを質問文に移しました
marimo.

2016/06/06 09:45

本当に大したことはしてないです(と思っています)
romiogaku

2016/06/07 05:40 編集

config.vm.synced_folder "./", "/home/vagrant/" ホーム直下をsynced_folderに指定するのはダメです。(.sshフォルダはホーム直下に作られるので) 例えば以下のように指定してあげてください。(hogeフォルダは自動で作られます) config.vm.synced_folder "./", "/home/vagrant/hoge" それとコードブロックはコメント欄では効かないようなので、質問に追記する形でお願いします。履歴も残りますので。
marimo.

2016/06/08 14:58 編集

確認なのですが,ゲストOS上のホーム直下とは/home/vagrant/とのことですよね? そのディレクトリをsynce_folderに指定するのはダメというのは,つまり,通常authorized_keysはゲスト側の/home/vagrant/.ssh/authorized_keysに作成されるので,そのディレクトリを指定してsynce_folderでホスト側のディレクトリと同期してしまうと結果的にauthorized_keysを削除(上書きされるといったほうがいいのか)している,ということでしょうか?
marimo.

2016/06/08 15:33

夜遅くに回答していただきありがとうございました.ディレクトリを変更ましたら無事解決しました.また今回を通してvagrantの理解を進めることが出来ました. また機会がございましたら回答お願い致します.
guest

0

以下のコマンドでssh設定を確認します。

sh

1$ vagrant ssh-config

IdentityFileの項目が秘密鍵のファイルになりますので、以下のコマンドでパスワードなしに設定します。

sh

1$ ssh-keygen -f {IdentityFile項目のファイル名} -p
Enter old passphrase: {現在のパスワードを入力} Key has comment '{IdentityFile項目のファイル名}' Enter new passphrase (empty for no passphrase): {空欄のままEnter} Enter same passphrase again: {空欄のままEnter}

投稿2016/06/08 15:02

tkmtmkt

総合スコア1800

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問