OpenSUSE Leap 42: SSHクライアントのsshfsをインストールする

SSHクライアントのsshfsをインストールする手順を記載します。

1 sshfsのインストール

sshfsパッケージをインストールします。

$ sudo zypper -n in sshfs

2 sshfsでマウント

sshfsコマンドで/mntディレクトリにマウントします。pathには書き込み権限が必要です。

$ sshfs <server> <path>

ここでは$HOME/mntにマウントします。

$ mkdir $HOME/mnt
$ sshfs ssh-server.hiroom2.com:$HOME $HOME/mnt
The authenticity of host 'ssh-server.hiroom2.com
(192.168.11.96)' can't be established.
ECDSA key fingerprint is
SHA256:jnXzkA7FZ1MW7K2zr9lM87nLt/IxJBIqKyt9EMF7mbc.
Are you sure you want to continue connecting (yes/no)? yes
hiroom2@ssh-server.hiroom2.com's password:

SSHサーバの$HOMEにアクセスできます。

$ ls $HOME/mnt
bin      Documents  examples.desktop  Pictures  src        Videos
Desktop  Downloads  Music             Public    Templates

3 SSH鍵の作成

SSHクライアントのrootユーザでSSHサーバのrootユーザに接続するためにSSH鍵を作成します。

SSHクライアントのrootユーザでssh-keygenを実行します。

$ # Run the following command on SSH client.
$ sudo ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
$ sudo cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3Nza<snip> root@ssh-client

作成した公開鍵をSSHサーバのrootユーザのautorized_keysに追加します。

$ # Run the following command on SSH server.
$ cat <<EOF | sudo tee /root/.ssh/authorized_keys
ssh-rsa AAAAB3Nza<snip> root@ssh-client
EOF

SSHクライアントからSSHサーバへ接続してのknown_hostsにSSHサーバの鍵を記載する必要があります。ここではSSHサーバのknown_hostsのチェックを無効化します。

$ # Run the following command on SSH client.
$ cat <<EOF | sudo tee /root/.ssh/config
Host 192.168.11.*
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

Host *.hiroom2.com
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null
EOF

4 起動時にマウント

他のファイルシステムと同様に/etc/fstabに追記します。ネットワーク初期化前にマウントするのを防ぐために、_netdevをオプションを指定する必要があります。x-systemd.automountにマウントさせるために、x-systemd.automountをオプションに指定する必要があります。identityfileでSSH鍵を指定しない場合は/root/.sshの鍵が使用されます。

$ SSH_SERVER=ssh-server.hiroom2.com
$ SSH_DIR=/
$ cat <<EOF | sudo tee -a /etc/fstab
${SSH_SERVER}:${SSH_DIR} /mnt fuse.sshfs _netdev,x-systemd.automount 0 0
EOF

SSHサーバのrootユーザ以外にアクセスするには<user>@<server>を用います。identityfileでSSH鍵を指定できます。allow_other、uid、gidでマウントポイントの権限を自分のユーザに変更できます。ただし、ホームディレクトリのパーミッションが755の場合はマウントポイントを他のユーザが覗けることに留意してください。

$ OPT=_netdev,x-systemd.automount,identityfile=/home/hiroom2/.ssh/id_rsa
$ OPT=${OPT},allow_other,uid=hiroom2,gid=users
$ mkdir -p /home/hiroom2/mnt
$ cat <<EOF | sudo tee -a /etc/fstab
hiroom2@${SSH_SERVER}:/home/hiroom2 /home/hiroom2/mnt fuse.sshfs ${OPT} 0 0
EOF

マウントポイントを他ユーザにアクセスされないようにするには、ホームディレクトリパーミッションを700にするか、ホームディレクトリとマウントポイントの間に700のディレクトリを作成します。

$ mkdir -p /home/hiroom2/guard/mnt
$ chmod 700 /home/hiroom2/guard
$ cat <<EOF | sudo tee -a /etc/fstab
hiroom2@${SSH_SERVER}:/home/hiroom2 /home/hiroom2/guard/mnt fuse.sshfs ${OPT} 0 0
EOF