LoginSignup
4
5

More than 5 years have passed since last update.

SSHの公開鍵によって環境変数を変える

Posted at

公開鍵のフォーマット

OpenSSH にはユーザごとに公開鍵を登録しておくファイル authorized_keys のそれぞれの行に環境変数やらコマンドやら登録できる機能があります。

公式マニュアルはこのへん。

environment="NAME=value"
Specifies that the string is to be added to the environment when logging in using this key. Environment variables set this way override other default environment values. Multiple options of this type are permitted. Environment processing is disabled by default and is controlled via the PermitUserEnvironment option.

これを使うとログインするときに使う鍵ごとに環境変数を設定できるようです。

つかってみる

さくっと Docker で試してみます。

$ docker run -it --rm alpine sh
/ # apk -U add openssh
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
(1/5) Installing openssh-keygen (7.5_p1-r1)
(2/5) Installing openssh-client (7.5_p1-r1)
(3/5) Installing openssh-sftp-server (7.5_p1-r1)
(4/5) Installing openssh-server (7.5_p1-r1)
(5/5) Installing openssh (7.5_p1-r1)
Executing busybox-1.26.2-r5.trigger
OK: 8 MiB in 16 packages
/ # ssh-keygen -A
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 
/ # ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_ed25519.
Your public key has been saved in /root/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:FHBfazmzGyPAN4FoAyxKM19o4/HtSMHIzX/Z/pOVI4o root@b836dc797f40
The key's randomart image is:
+--[ED25519 256]--+
|  ..B..oo.. .    |
| +.B.*+o.o o o   |
|..*.=.+.+ * *    |
|.  o o + = + +   |
|    . o S o +   .|
|     . .   o = + |
|          . + + .|
|         E . +   |
|              .  |
+----[SHA256]-----+
/ # echo -n 'environment="HOGE=hogehoge" ' > ~/.ssh/authorized_keys 
/ # cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys 
/ # chmod 600 ~/.ssh/authorized_keys 
/ # /usr/sbin/sshd 
/ # ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:vKnNgkryhN9E0+OPtSJdgWd3FNdmifbmmMYbBhe3VhY.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

70d137c0945f:~# env | grep HOGE
70d137c0945f:~# env | grep HOGE | wc -l
0

あれ?

ユーザー環境変数

環境変数が設定されないのは man に書いてある PermitUserEnvironment がデフォルトで no になっているのが原因でした。

設定ファイルに追加します。

/ # echo 'PermitUserEnvironment yes' >> /etc/ssh/sshd_config 
/ # kill `pgrep sshd`
/ # /usr/sbin/sshd 
/ # ssh localhost
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

70d137c0945f:~# env | grep HOGE
HOGE=hogehoge

でました!!

使い道

ISUCONで3人が同時に git commit するために使いました、というか使いたかったです。
環境変数に GIT_AUTHOR_NAMEGIT_AUTHOR_EMAIL を設定しておくと git config しなくてもコミットできます。

予選では PermitUserEnvironment no だったことに気付かずデフォルトユーザーのままコミットしてしまいましたが、原因が分かったので本選では予定通り使ってみるつもりです。

4
5
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
4
5