10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macOS sierraのSSHの問題を解消する

Last updated at Posted at 2017-01-09

SSHでパスフレーズが聞かれる問題

ssh-add -Aをしなければいけないと言う記事も見られるが、macOS 10.12.2からは、~/.ssh/configUseKeychain yesを加えれば良いらしい1 2

Host *
  UseKeychain yes

$ man ssh_configしてみると、以下のように書いてある。

UseKeychain
  On macOS, specifies whether the system should search for passphrases in
  the user's keychain when attempting to use a particular key. When the
  passphrase is provided by the user, this option also specifies whether
  the passphrase should be stored into the keychain once it has been
  verified to be correct.  The argument must be ``yes'' or ``no''.
  The default is ``no''.

要するに、UseKeychain yesにするとkeychainからパスフレーズを探したり、保存したりするようになる。

agent fowardingが必要な場合は、AddKeysToAgent yesも必要。

Host *
    UseKeychain yes
    ForwardAgent yes
    AddKeysToAgent yes

manには

AddKeysToAgent
  Specifies whether keys should be automatically added to a running
  ssh-agent(1).  If this option is set to ``yes'' and a key is loaded from
  a file, the key and its passphrase are added to the agent with the
  default lifetime, as if by ssh-add(1).  If this option is set to
  ``ask'', ssh will require confirmation using the SSH_ASKPASS program 
  before adding a key (see ssh-add(1) for details).  If this option is set
  to ``confirm'', each use of the key must be confirmed, as if the -c
  option was specified to ssh-add(1).  If this option is set to ``no'', no
  keys are added to the agent.  The argument must be ``yes'', ``confirm'',
  ``ask'', or ``no''.  The default is ``no''.

と書いてあり、ssh-agentに自動的に鍵が追加される。

もしも、~/.ssh/configの設定を見ないプログラムがある場合は、ssh-add -Assh-add -Kを自分でやる必要があるかもしれない。

$ man ssh-addすると、それぞれのオプションは

-K    When adding identities, each passphrase will also be stored in
      the user's keychain.  When removing identities with -d, each
      passphrase will be removed from it.

-A    Add identities to the agent using any passphrase stored in
      the user's keychain.

と書いてあり、-Kオプションで、鍵がkeychainに保存され、-Aでkeychainから鍵がagentに追加される。

SSHでレガシーな暗号が無効になった問題

macOS SierraでOpenSSH v7が採用されたため、レガシーな暗号が無効になった3 4

例えば

Unable to negotiate with XX.XXX.XX.XX: no matching host key type found. Their offer: ssh-dss

というエラーが出た場合は

Host your-remote-host
    HostkeyAlgorithms +ssh-dss

してやると解消される5(アルゴリズムの変更ができる場合はそちらを変更したほうがいいとは思う)。

ただし、これをしても、RubyのNet::SSHで

Net::SSH::Exception: could not settle on host_key algorithm

がでるかもしれない。これは古いNet::SSHが+を使った表記に対応していないためだ。
Correctly parse '+' in config files by clupprich · Pull Request #470 · net-ssh/net-sshというPRで修正されたので、

$ gem update net-ssh

でアップデートするか、古いNet::SSHを使う場合はHostkeyAlgorithmsに使いたい全てのアルゴリズムを書いてやる必要がある。

デフォルトのアルゴリズムは$ man ssh_confgで確認できる。

HostKeyAlgorithms
        Specifies the host key algorithms that the client wants to use in
        order of preference.  Alternately if the specified value begins
        with a `+' character, then the specified key types will be
        appended to the default set instead of replacing them.  The
        default for this option is:

           ecdsa-sha2-nistp256-cert-v01@openssh.com,
           ecdsa-sha2-nistp384-cert-v01@openssh.com,
           ecdsa-sha2-nistp521-cert-v01@openssh.com,
           ssh-ed25519-cert-v01@openssh.com,
           ssh-rsa-cert-v01@openssh.com,
           ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,
           ssh-ed25519,ssh-rsa

        If hostkeys are known for the destination host then this default
        is modified to prefer their algorithms.

        The list of available key types may also be obtained using the -Q
        option of ssh(1) with an argument of ``key''.

これらにアルゴリズムにssh-dssを加えて、HostkeyAlgorithmsを設定すると良い。

Host your-remote-host
    HostkeyAlgorithms ecdsa-sha2-nistp256-cert-v01@openssh.com, ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,ssh-rsa,ssh-dss
  1. macOS Sierra 10.12 SSH Keys – testequals

  2. difficulties with ssh-agent in macOS Sierra : osx

  3. Apple、macOS SierraでOpenSSH v7を採用。いくつかのレガシーな暗号が無効に。 | AAPL Ch.

  4. OpenSSH: Legacy Options

  5. web hosting - Unable to negotiate with XX.XXX.XX.XX: no matching host key type found. Their offer: ssh-dss - Stack Overflow

10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?