LoginSignup
1
1

More than 5 years have passed since last update.

Dropbox/.ssh/の鍵ファイルを全て~/.sshにシンボリックリンクを貼るスクリプトを書いた

Last updated at Posted at 2017-04-15

まずDropboxの任意のディレクトリに鍵を全て置いてください。
僕は以下のような感じで配置しました。

$ pwd
/path/to/Dropbox/.ssh
$ tree .
.
├── config
├── id_hoge_rsa
├── id_hoge_rsa.pub
└── hoge.pem

この /path/to/Dropbox/.ssh ディレクトリに以下の setup.sh を配置します。

$ pwd
/path/to/Dropbox/.ssh
$ touch setup.sh
$ chmod 755 setup.sh
$ vim setup.sh
setup.sh
#!/bin/sh

files=`find . -maxdepth 1 -type f | sed "s!^.*/!!" | grep -vE "setup.sh|.DS_Store"`

for file in $files; do
  file_path=`pwd`/$file
  target_dir_path=$HOME/.ssh/

  ln -s $file_path $target_dir_path

  target_file_path=$target_dir_path/$file
  if [[ $file =~ '.pem' ]]; then
    chmod -h 400 $target_file_path
  elif [[ $file =~ '.pub' ]]; then
    chmod -h 644 $target_file_path
  elif [[ $file =~ 'config' ]]; then
    chmod -h 644 $target_file_path
  else
    chmod -h 600 $target_file_path
  fi
done

注意) シンボリックリンクを貼りたくないファイルについて
setup.sh.DS_Store のようなシンボリックリンクを貼りたくないファイルは、3行目の grep -vE の引数に例にならって追加してください。

最後にこのスクリプトファイルを実行します。既にリンク先に同名のファイルが存在する場合でも上書きなどはされません。
もし上書きする場合は、 ln -f -s のように書き換えてください。(危険なのでオススメしません。)

$ pwd
/path/to/Dropbox/.ssh
$ ./setup.sh

以下について確認すれば良いでしょう。
1. ls -la で参照先とパーミッションが問題ないか
2. ファイル内がきちんと参照されているか

$ cd ~/.ssh
$ ls -la
lrw-r--r--   1 me  staff    42  4 15 18:24 config@ -> /path/to/Dropbox/.ssh/config
$ vim config
Host hogehoge
...(中略)...
1
1
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
1
1