LoginSignup
37
11

More than 1 year has passed since last update.

1Password for SSHを試してみる

Last updated at Posted at 2022-03-17

これはなに

1Password で SSH 鍵を管理できるようになった様。

お試しとして、ローカルに Docker で ssh サーバーを用意して ssh してみる。

  • 1Password for SSH は 1Password 8 の機能です
    • Mac 版 1Password 8 は記事投稿時点ではベータ版です
  • 今回試すのは Mac です

下準備

今回は検証用の ssh サーバーを用意するので、以下の手順で用意した。(長いので details で囲っている)

Dockerでsshサーバーを立てる

雑に ssh サーバーをローカルに Docker コンテナで立ち上げる。

以下にあるsshd_configは今回の検証用です。公開されるサーバーには使用しないでください。

ディレクトリ構成は以下。

$ tree -L 2
.
├── Dockerfile
├── config
│   ├── authorized_keys
│   └── sshd_config
└── docker-compose.yml
Dockerfile
FROM ubuntu:22.04

RUN apt update && apt install openssh-server -y
RUN mkdir -p /var/run/sshd

ADD ./sshd_config /etc/ssh/sshd_config
ADD ./authorized_keys /root/.ssh/authorized_keys

CMD ["/usr/sbin/sshd", "-D"]
config/authorized_keys
あとで公開鍵を追加するので、ここでは空のファイルにしておく
config/sshd_config
# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
AuthorizedKeysFile     .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem       sftp    /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
docker-compose.yml
version: '3'
services:
  ssh_server:
    build: .
    ports:
      - 50022:22

配置が完了したら、

$ docker compose build
$ docker compose up -d

して準備完了。

1Password for SSH を試す

設定を有効にする

Preferences > Developer > Use the SSH agent をオンにしておく。

オンにした際に表示されたスニペットを~/.ssh/configに貼り付けておく。

~/.ssh/config
Host *
	IdentityAgent "~/.1password/agent.sock" # 環境によってsocketのパスは異なると思われます

新規の SSH 鍵を作成する

1. 1Password の「New item」から「SSH Key」を選ぶ

2. 「Add Private Key」から「Generate a New Key」を選択

3. 好きな「Key Type」を選び「Generate」し、「Save」

4. SSH Key ができていることを確認

5. 「public key」を ssh サーバーに登録する

今回だと Docker で用意した ssh サーバーに公開鍵を配置する。
config/authorized_keysに公開鍵を貼り付ける。

config/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJ5jW/e+6x9hxgugyt147bgvpfZit3VL+Odbsv4K2+X

変更を反映させる。

$ docker compose build
$ docker compose up -d

7. 実際に ssh してみる

できた

$ ssh -p 50022 root@127.0.0.1
Welcome to Ubuntu Jammy Jellyfish (development branch) (GNU/Linux 5.10.76-linuxkit x86_64)

既存の SSH 鍵を使う

0. SSH 鍵を作成する

すでにある SSH 鍵を再現するため、ssh-keygenで SSH 鍵を作成しておく。(パスフレーズも設定してみた)

$ ssh-keygen -t ed25519 -f docker-server-key
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase): password
Enter same passphrase again: password

1. SSH 鍵をインポートする

秘密鍵を選択する。

パスフレーズを設定している場合、パスフレーズを聞かれるので入力して「Save」

2. SSH 鍵が追加されているか確認

3. 「public key」を ssh サーバーに登録する

今回だと Docker で用意した ssh サーバーに公開鍵を配置する。
config/authorized_keysに公開鍵を貼り付ける。

config/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvuLD9gNGBQFyx8afPPQt/WjRDhr1UV9AmiDAF8cC9L

変更を反映させる。

$ docker compose build
$ docker compose up -d

7. 実際に ssh してみる

できた

$ ssh -p 50022 root@127.0.0.1
Welcome to Ubuntu Jammy Jellyfish (development branch) (GNU/Linux 5.10.76-linuxkit x86_64)

最後に

1Password で SSH 鍵が管理できるのは楽でいいなと思った。Touch ID で認証ができるのは楽でめちゃめちゃいい。
(ssh-agent周りは全然理解できてない...)

公式ドキュメントがあるので、こちらも見てみるといいかもしれません。

37
11
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
37
11