Table of Contents
1 sshfsのインストール
sshfsパッケージをインストールします。
$ sudo dnf install -y sshfs
2 sshfsでマウント
sshfsコマンドで/mntディレクトリにマウントします。pathには書き込み権限が必要です。
$ sshfs <server> <path>
ここでは$HOME/mntにマウントします。
$ mkdir $HOME/mnt$ sshfs ssh-server.hiroom2.com:$HOME $HOME/mntThe authenticity of host ‘ssh-server.hiroom2.com(192.168.11.96)’ can’t be established.ECDSA key fingerprint isSHA256:jnXzkA7FZ1MW7K2zr9lM87nLt/IxJBIqKyt9EMF7mbc.Are you sure you want to continue connecting (yes/no)? [email protected]’s password:
SSHサーバの$HOMEにアクセスできます。
$ ls $HOME/mntbin Documents examples.desktop Pictures src VideosDesktop 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.pubssh-rsa AAAAB3Nza<snip> root@ssh-client
作成した公開鍵をSSHサーバのrootユーザのautorized_keysに追加します。
$ # Run the following command on SSH server.$ cat <<EOF | sudo tee /root/.ssh/authorized_keysssh-rsa AAAAB3Nza<snip> root@ssh-clientEOF
SSHクライアントからSSHサーバへ接続してのknown_hostsにSSHサーバの鍵を記載する必要があります。ここではSSHサーバのknown_hostsのチェックを無効化します。
$ # Run the following command on SSH client.$ cat <<EOF | sudo tee /root/.ssh/configHost 192.168.11.* StrictHostKeyChecking no UserKnownHostsFile=/dev/nullHost *.hiroom2.com StrictHostKeyChecking no UserKnownHostsFile=/dev/nullEOF
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 0EOF
SSHサーバのrootユーザ以外にアクセスするには<user>@<server>を用います。identityfileでSSH鍵を指定できます。allow_other、uid、gidでマウントポイントの権限を自分のユーザに変更できます。
$ OPT=_netdev,x-systemd.automount,identityfile=/home/hiroom2/.ssh/id_rsa$ OPT=${OPT},allow_other,uid=hiroom2,gid=hiroom2$ mkdir -p /home/hiroom2/mnt$ cat <<EOF | sudo tee -a /etc/fstabhiroom2@${SSH_SERVER}:/home/hiroom2 /home/hiroom2/mnt fuse.sshfs ${OPT} 0 0EOF