自宅のintel-nucにVPS経由でssh tunnelする
ここが大変参考になり、ほぼ真似しただけです。
https://blog.alprosys.com/2019/01/06/reverse-ssh-tunneling/
概要 #
sshのトンネル機能を利用して、自宅外からVPS経由で自宅サーバー(intel-nuc)にsshでログインする
自宅サーバーの設定 #
sshの設定 #
- VPSにログインできるように、自宅サーバーの公開鍵をvpsの.ssh/authorized_keysに登録する
- configファイルを.ssh/以下に作成する
Host vps-ssh-tunnel
HostName public.example.com
User vps
ServerAliveInterval 60
ExitOnForwardFailure yes
TCPKeepAlive no
IdentityFile /path/to/public.example.com.rev.id_ecdsa
- ssh vps-ssh-tunnel して接続確認
自動起動の設定 #
- systemdのserviceを作る
~/.config/systemd/user/以下にvps-ssh-tunnel.serviceを作成 踏み台がVPS(外部)でnucは内部なので、SSHではリモートフォワードを使う
[Unit]
Description= SSH Tunneling to VPS
[Service]
ExecStart=ssh -NR 2222:intel-nuc:22 vps-ssh-tunnel
Type=simple
Restart=always
[Install]
WantedBy=default.target
この設定でVPSサーバー上でport 2222に流れる通信がintel-nucのport 22に転送される。
- 起動を確認
systemctl --user daemon-reload
systemctl --user start public-rev
systemctl --user enable public-rev
systemctl --user status して動いていることを確認
- 一般ユーザーでもserviceを起動起動する
ユーザーレベルで起動したサービスは起動直後やログオフ時には実行されないようなので、
以下を参考に設定を変える
https://harpyja.daemon.asia/wiki/Unix/systemd-continue-user-service
/etc/systemd/logind.conf の
KillUserProcesses=no
また
loginctl enable-linger <username>
をするらしい。
VPSサーバーの設定 #
自宅サーバーにログインできるように、VPSサーバーの公開鍵を自宅サーバーの.ssh/authorized_keysに登録する。VPSサーバー上で自宅サーバーに接続できるか確認
ssh username@localhost -p 2222 でログインできるか確認
クライアントの設定 #
ターミナル上でVPSにログインして、そこからまたintel-nucに接続するのが面倒なので、
クライアントの.ssh/configに以下を追加
Host private.intel-nuc
HostName localhost
Port 2222
User username
ProxyCommand ssh -W %h:%p vps@public.example.com
DynamicForward 2222
ServerAliveInterval 60
ssh private.intel-nuc でvps経由で接続できるか確認
SFTPでも接続する #
参考 SSHトンネルでSFTP接続
クライアント端末のターミナル上で、VPSに対してローカルフォワードで接続する。
VPS上でのフォワード先をlocalhostにしてやれば、クライアント端末のポート2000からの通信を、VPSのポート2222対してフォワードして、2222に対する通信は更に、intel-nucのポート22にリモートフォワードされることになる。
ssh -L 2000:localhost:2222 vps@public.example.com
トンネルを確立した状態で、FFFTPやTRANSMITでlocalhost:2222に接続すればVPS経由でsftpができる。 クライアントの.ssh/configに以下を追加
Host private.intel-nuc.sftp
HostName public.example.com
User vps
LocalForward 2000 localhost:2222
IdentityFile /path/to/public.example.com.rev.id_ecdsa
こうすれば、ターミナル上で
ssh private.intel-nuc.sftp
で接続できるので便利
コメント