クラウドドライブにgitのリモートリポジトリを構築する

そもそもクラウド自体がgit(だと思う)を使って実現しているわけだから,いかにも屋上屋を架している感があるが,メリットがないわけではない.

こういう場合に

  • 複数台のコンピュータを使うが,それ以外の環境(スマートフォンタブレット)ではアクセスしない
    • gitがインストール・実行できる環境であることが必要
    • androidなら(中身はLinuxだし)可能?
  • 主に扱うのはテキストファイル(ワードやパワーポイントはあまり使わない)
    • SourcetreeなどのCUI環境も使えば対応可能
  • 細かい変更履歴がほしい
  • 任意の時点の状態に戻せる可能性がほしい
  • クラウドの容量にそれほど余裕がない(節約できると嬉しい)
    • 試してないけど,push先を分散させれば,手元のディレクトリ構造がクラウドの容量に制約されなくなるのでは
2019/4/25追記

デメリットとして,「web上のクラウドのページからファイルにブラウザでアクセスできない」ということがあるのに気づいた.

これを解決するには,たとえばAWSのようなサーバにリモートを作成して,同じサーバの上にローカルを作って,
一定時間で(あるいは他のリモートからアクションがあったら)自動でプルするようにしておけばいいのかもしれないが,
容量の節約というメリットはなくなる.

やったこと

mac osxの場合(ほかはわからない)

OpenSSHのインストール(しなくても大丈夫かも)

homebrewで,

$ brew install openssh

もしhomebrewのインストールがまだなら,適当に検索してインストール.

「設定」→「共有」→「リモートログイン」にチェック

若干はまった.
Macの「設定」から設定を変更し,アクセスを許可する.
qiita.com

gitのユーザー名とメールアドレスを設定(一度もgitを使ったことがない場合)
$ git config --global user.name "username"
$ git config --global user.email "user@email.com"
リモートリポジトリを作る

pushする先になるディレクトリをDropboxなどの適当な場所に作る.
末尾を ".git" にするのが慣習らしい.
ディレクトリを作ったら,gitをリモートとして(--bareオプション)初期化する.

$ mkdir ~/Dropbox/myrepo.git
$ git --bare init --shared
ローカルリポジトリを作る

共有したい既存のフォルダでgitを初期化し,リモートリポジトリを登録する.

$ git init
$ git remote add origin localhost:/Path/to/Dropbox/myrepo.git

www.sejuku.net

2台目以降のコンピュータにローカルリポジトリを設置する場合には,プルリクエストして取得してもいいが,

$ cd Path/to/dest/
$ git clone localhost:/Path/to/Dropbox/myrepo.git

とやることで,/Path/to/dest/myrepoが作成される.

公開鍵認証を設定する(次以降で詳しく)

公開鍵認証の設定

今回ほとんど唯一と言っていい,はまった箇所.
公開鍵暗号を設定すると,いちいちコマンドを打つたびにパスワードを聞かれるということがなくなる.

...はずなのだが,この公開鍵認証が一向に成功せず,パスワードを聞かれ続け,
やっと聞かれなくなったと思ったら認証が通らない状態になった.

こんな感じのエラーが出て,

username@localhost: Permission denied (publickey,password).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
$ ssh -v localhost

デバッグモードで接続してログを詳細表示すると,

OpenSSH_7.9p1, OpenSSL 1.0.2q  20 Nov 2018
debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file /Users/username/.ssh/id_rsa type 0
debug1: identity file /Users/username/.ssh/id_rsa-cert type -1
debug1: identity file /Users/username/.ssh/id_dsa type -1
debug1: identity file /Users/username/.ssh/id_dsa-cert type -1
debug1: identity file /Users/username/.ssh/id_ecdsa type -1
debug1: identity file /Users/username/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/username/.ssh/id_ed25519 type -1
debug1: identity file /Users/username/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/username/.ssh/id_xmss type -1
debug1: identity file /Users/username/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.9
debug1: match: OpenSSH_6.9 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as 'username'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:  compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:  compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:(なんらかの鍵)
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /Users/username/.ssh/known_hosts:2
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: Will attempt key: /Users/username/.ssh/id_rsa RSA SHA256:(公開鍵)
debug1: Will attempt key: /Users/username/.ssh/id_dsa
debug1: Will attempt key: /Users/username/.ssh/id_ecdsa
debug1: Will attempt key: /Users/username/.ssh/id_ed25519
debug1: Will attempt key: /Users/username/.ssh/id_xmss
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/username/.ssh/id_rsa RSA SHA256:(公開鍵)
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/username/.ssh/id_dsa
debug1: Trying private key: /Users/username/.ssh/id_ecdsa
debug1: Trying private key: /Users/username/.ssh/id_ed25519
debug1: Trying private key: /Users/username/.ssh/id_xmss
debug1: No more authentication methods to try.
username@localhost: Permission denied (publickey).

実際には,ちょっとずつ設定をいじるたびにちょっとずつ内容が変わるが,
だいたい最後のところでパスワードを認証に使うかどうかというところが違うくらい.

やったこと

公開鍵と秘密鍵のペアを作る
$ ssh-keygen
# いろいろ聞かれるが,基本的にデフォルトのままenterでOK
# id_rsaとid_rsa.pubが生成される
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# 要するに名前を変えてコピーしてるだけ
# とんでもない間違いに気づいたので修正…

stackoverflow.com

/etc/ssh/sshd_configを書き換える

地味にこれがあんまり情報がなくてきつかった.
結果的に動くようになった時点までの変更を挙げておくが,不要なものが混入している可能性がある.
なお,それぞれの行は分散して書かれており,コメントアウトされている.
また,参考リンクの通りにはなっていない.

# 認証に公開鍵暗号方式を使う
RSAAuthentication yes
PubkeyAuthentication yes
# パスワードを使わない
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

qiita.com

関係するファイル・ディレクトリのパーミッションを書き換える

最終的にこれにずっとはまってた.

$ chmod 600 ~/.ssh/authorized_keys
# 必須
$ chmod 700 ~/.ssh
# たぶん必須
# id_rsaパーミッションが間違ってたので修正
$ chmod 600 ~/.ssh/id_rsa
$ chmod 644 ~/.ssh/id_rsa.pub
# よくわからん
$ chmod 755 ~/
# 必須(本来はこのパーミッションのはず)

serverfault.com
qiita.com