13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sshfsを使って踏み台サーバ経由で開発用サーバのファイルをマウントする方法

Posted at

概要

  • 踏み台サーバ経由で開発用サーバにアクセスする必要がある場合
  • 開発用サーバのファイルをローカルにマウントしたい
  • sshfsを使ってマウントする

Screenshot from 2020-03-09 03-36-38.png

なぜsshfsを使う?

例えば、VSCodeを使って開発する場合、
Remote SSHを使えば無駄な労力なくローカル環境で開発できるが、
接続先があまりにも大きすぎると、フリーズすることがしばしば

sshfsでは体感ですが、快適に開発することができます

環境構築

macの場合

  • FUSE for macをインストール
$ brew tap homebrew/cask
$ brew cask install osxfuse
  • sshfsのインストール
$ brew install sshfs

# インストールされたことを確認
$ sshfs --version

Ubuntuの場合

$ apt install -y sshfs

# インストールされたことを確認
$ sshfs --version

sshの設定

キーペアの配置

ssh接続するために作成した開発用サーバのキーペアをダウンロードして、
名前をdev.pemに変更し~/.ssh/keysにコピー

# キーペアの名前をdev.pemに変更
$ mv xxxx.pem dev.pem

# キーペアの移動
$ cd dev.pem ~/.ssh/keys

キーペアがない場合は、こちらを参考にキーペアを作成する。

configファイルの編集

~/.ssh/config
Host jump-host
    User <<踏み台サーバのホスト名:ec2-userなど>>
    HostName <<踏み台サーバのグローバルIPアドレス>>
    port 22
    TCPKeepAlive yes
    ServerAliveInterval 60

Host dev-server
    User <<踏み台サーバのホスト名:ec2-userなど>>
    HostName <<開発用サーバのプライベートIPアドレス>>
    port 22
    IdentityFile ~/.ssh/keys/dev.pem
    ProxyCommand ssh -W %h:%p jump-host
    TCPKeepAlive yes
    ServerAliveInterval 60
    RemoteForward 52698 127.0.0.1:52698

IPアドレスがわからない場合は、以下の記事参照
EC2のIPアドレスを調べる方法(超初心者向け)

マウント

マウントしたいファイルのパスを指定して、下記のコマンドを入力

$ sshfs -o follow_symlinks dev-server:<<マウントしたいファイルのパス>> ~/Workspace/dev

ファイルパスは以下のコマンドで確認

$ pwd
/home/ec2-user/dev

例) /home/ec2-user/devをマウントするとき

$ sshfs -o follow_symlinks dev-server:/home/ec2-user/dev ~/Workspace/dev

アンマウント

$ umount -f ~/Workspace/dev

便利な設定

マウントおよび、アンマウント時に入力するコマンドはどうしても長くなってしまうので、エイリアスを設定しましょう

~/.bashrc
# setting sshfs
# mount
alias sfdev='sshfs -o follow_symlinks dev-server:<<マウントしたいファイルのパス>> ~/Workspace/dev'
# unmount
alias ufdev='umount -f ~/Workspace/dev'
13
8
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
13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?