Skip to main content

openssh

openssh関連ツールについて。

ssh 鍵ペアの作成

ED25519を使うには比較的新しい環境が必要となる(TeraTerm、PuTTyでは既に対応済み) 鍵作成時に鍵長は指定できない(256ビット)。 (ssl証明書ではまだed25519に対応してない模様。ECDSAを使わないならRSAを使わざるをえない。)

ssh-keygen -t ed25519
ssh-keygen -l -f ~/.ssh/id_ed25519

sshサーバへ、公開鍵を転送

cat ~/.ssh/<pubkey> | ssh user@host 'cat >> ~/.ssh/authorized_keys'

known_hostsに登録されているsshサーバ公開鍵の確認と削除

ssh-keygen -F distname
ssh-keygen -R distname

recreate public key from private key

Once create pem format public key from private key. Then import it and output as openssh format key.

$ ssh-keygen -e -f id_rsa.testkey > id_rsa.testkey.pub.pem
$ ssh-keygen -i -f id_rsa.testkey.pub.pem > id_rsa.testkey.pub

接続時のオプション

ssh host
-l user
-v vervose messages
-i private key
-p port
-X # allow x11 forwarding
-L localport:remotehost:remoteport # local port forwarding
-R localport:remotehost:remoteport # remote port forwarding

example of port forwarding

local port forwarding to access remote rdphost
$ ssh username@remotehost -L 13389:rdphost:3389
$ freerdp /v:127.0.0.1:13389

remote port forwarding to allow rdp access
<localhost >$ ssh username@remotehost -R 13389:rdphost:3389
<remotehost>$ freerdp /v:127.0.0.1:13389

ssh ca

create ca certificate

$ ssh-keygen -t ed25519 -f ca.key

make client certificate (sign client public key with ca key)

$ ssh-keygen -s ca.key -I certificate-test -n username id_rsa.pub
$ ssh-keygen -L -f id_rsa-cert.pub

send public ca key to ssh server

$ scp -p ca.key.pub username@remotehost:/home/username

$ sudo mv ca.key.pub /etc/ssh/ca.key.pub
$ sudo chown root:root /etc/ssh/ca.key.pub
$ echo "TrustedUserCAKeys /etc/ssh/ca.key.pub" | sudo tee -a /etc/ssh/sshd_config

make host certificate (sign host public key with ca key)

$ scp -p username@remotehost:/etc/ssh/ssh_host_ed25519_key.pub ssh_host_ed25519_key.pub

$ ssh-keygen -s ca.key -h -I certificate-test ssh_host_ed25519_key.pub
$ ssh-keygen -L -f ssh_host_ed25519_key-cert.pub

send host certificate back to ssh server

$ scp -p ssh_host_ed25519_key-cert.pub username@remotehost:/home/username/ssh_host_ed25519_key-cert.pub

$ sudo mv ssh_host_ed25519_key-cert.pub /etc/ssh/ssh_host_ed25519_key-cert.pub
$ sudo chown root:root /etc/ssh/ssh_host_ed25519_key-cert.pub
$ echo "HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub" | sudo tee -a /etc/ssh/sshd_config

$ service sshd restart

add public key to known host

$ cat << EOF >> ~/.ssh/known_hosts
> @cert-authority *.example.com ssh-ed25519 ...(content of ca.key.pub)...
> EOF

.ssh/config

.ssh/config ファイルにあらかじめ設定を書いておけば、接続のたびにコマンドでいろいろ引数指定しなくてすむ。

以下の例の場合、ssh myalias と実行すれば ssh -l username -p 2222 -i ~/.ssh/I'd_RSA.foo my.example.com で接続できる。

ワイルドカード指定もできる。 以下の例の場合、*.example.net へ ssh するとき、 秘密鍵として ~/.ssh/id_rsa.bar が使われる。

Host myalias
    HostName my.example.com
    User username
    Port 2222
    IdentityFile ~/.ssh/id_rsa.foo
Host *.example.net
    IdentityFile ~/.ssh/id_rsa.bar