About me

Thursday 13 July 2017

sshfs

   

命令描述

通常在Linux裡面,一般我們要操作到遠端的檔案系統的話,都會用『samba』和『nfs』,這兩個方式比較正統,但是其實有第三種方式 - 『SSHFS (Secure SHell FileSystem)』的存在。sshfs可以直接用ssh的方式來掛載一個遠端檔案系統,主要是在linux上的檔案操作(還有其他有實做FUSE的OS,像是Mac OS X和FreeBSD等等)。 在本地端如果要把sshfs給掛載起來的話,實做的話會採用到FUSE (Filesystem in Userspace)的kernel module。所以呢,本地端就是要安裝這個軟體,而遠端則只要SSH的SFTP subsystem就夠了。這個命令如果要用到IPv6的話,記得要將IP給放在中括號(square brackets)裡面,像是[fe80::1025:45d0:9e4e:65c7]。 安裝
sudo apt-get install sshfs 另外,sshfs也可以安裝在MAC和windows,但是我這邊就只說明Linux系統的安裝喔。 語法
mounting sshfs [user@]host:[dir] mountpoint [options] unmounting fusermount -u mountpoint 簡單使用
首先,你local端一定會有一個要掛載的資料夾,假設是『/mnt』,而遠端有一個資料夾叫做『/home/myremote』。 1. 先確保local端已經安裝sshfs了。 2. 確保remote端有安裝相關的ssh,並且有啟動服務。 3. 看一下需求,是否要允許其他使用者,記得修改一下『/etc/fuse.conf』,允許『user_allow_other』。 4. 直接下指令掛載 $ sshfs -o allow_other hugh@192.168.0.1:/home/myremote /mnt 5. 看一下『df』,或是『mount』指令,看是否已經有掛載上去了,然後就可以正常操作了。 6. 要取消掛載,可以使用『umount』或是『fusermount』,如下: $ fusermount -u /mnt or $ sudo umount /mnt 永久掛載
sshfs其實跟『samba』和『nfs』比較起來,它簡單了一點,但是其實只適合暫時使用,所以說,如果你想要長期掛載一個檔案系統的話,最好還是用『samba』和『nfs』,如果要長期掛載一個sshfs其實你就要自己承擔相關的風險,但是這邊還是介紹一下如何永久掛載。跟『samba』和『nfs』一樣,都要從『/etc/fstab』下手,請在這個文件底下加入: hugh@192.168.0.1:/home/myremote /mnt fuse.sshfs defaults 0 0 等一下,這樣還沒結束阿,要記得配對一下你的local和remote的ssh key阿,不然你開機時會等著你輸入ssh的密碼,這不好吧,所以這部份需要使用一下命令『ssh-keygen』和『ssh-copy-id』的幫助: 1. 先用『ssh-keygen』產生key pair: $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/hugh/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/hugh/.ssh/id_rsa. Your public key has been saved in /home/hugh/.ssh/id_rsa.pub. The key fingerprint is: SHA256:H2dzTAAJM2j7d7rEzWEbTXWrm0p1Jc63QYW0f1THcQk hugh@hugh-VirtualBox The key's randomart image is: +---[RSA 2048]----+ | .++o+o.oE.o*| | . ooo..+ . o+| | . + + o o | | . . .. B + | | . . oS.= * . | | . o oo O . | | . . o O o | | . o..= o | | o.o. | +----[SHA256]-----+ 2. 將public key複製到remote: $ ssh-copy-id -i ~/.ssh/id_rsa.pub hugh@192.168.0.1 3. 使用『mount』指令掛載看看: $ sudo mount -a 底下是一些其他可以用的options: OPTIONS
general options: -o opt, [opt...] 其他的mount options. -h --help print help. -V --version 列印版本資訊。 SSHFS options: -p PORT 相等於 '-o port=PORT'. -C 相等於 '-o compression=yes'. -F ssh_configfile 具體指定另一個替代的ssh組態檔。 -1 相等於'-o ssh_protocol=1'. -o reconnect 重新與server的連線。 -o delay_connect 延遲與server的連線。 -o sshfs_sync 同步寫入。 -o no_readahead 同步讀取(no speculative readahead)。 -o sync_readdir 同步資料夾讀取。 -o sshfs_debug 列印一些除錯(debugging)資訊。 -o cache=BOOL 是否啟用快取(caching),預設是yes。 -o cache_timeout=N 設定cache的timeout,單位是秒,預設的話是20秒。 -o cache_X_timeout=N 設定{stat, dir, link} cache的timeout。 -o workaround=LIST 這段看不太懂,就不翻了。 colon separated list of workarounds none no workarounds enabled all all workarounds enabled [no]rename fix renaming to existing file (default: off) [no]nodelaysrv set nodelay tcp flag in ssh (default: off) [no]truncate fix truncate for old servers (default: off) [no]buflimit fix buffer fillup bug in server (default: on) -o idmap=TYPE user/group ID 的mapping, 可能的類型有以下三個: 1. none 沒有ID space的轉換,這是預設。 2. user 只有轉換目前連線的使用者UID。 3. file 根據『uidfile』和『gidfile』的內容來轉換UIDs和GIDs。 -o uidfile=FILE 如果你的idmap是設定成『idmap=file』,而這個檔案主要就是指到這個包含了username:uid對應關係的檔案。 -o gidfile=FILE 如果你的idmap是設定成『idmap=file』,而這個檔案主要就是指到這個包含了groupname:gid對應關係的檔案。 -o nomap=TYPE 如果你的idmap是設定成『idmap=file』,那如果沒有對映到的關係怎麼辦?這個選項主要就是關於這個,使用『ignore』的話,就是不要做任何的重新對映(re-mapping),而選項『error』的話,就會回傳error,預設的話是會回傳error。 -o ssh_command=CMD 執行後面接的命令(假設是CMD),而不是直接執行『ssh』。 -o ssh_protocol=N 要使用的ssh協定,預設是『2』。 -o sftp_server=SERV 到sftp server或是subsystem的路徑,預設是『sftp』。 -o directport=PORT 經由ssh直接連線到某個port。 -o slave 經由網路的stdin和stdout來跟slave溝通。 -o disable_hardlink 在使用link這個命令建立檔案連結時,有錯誤時將會回傳相關的errno,而這個errno是設定到ENOSYS上的。而Hard links在sshfs上其實會有一些狀況,所以如果你發現因為hard links所造成的一些問題的話,可以試著用這個選項將hard link關掉在試看看。 -o transform_symlinks 將絕對路徑的連結(symlinks)轉成對路徑的連結。 -o follow_symlinks 遵循server上的symlinks。 -o no_check_root 不要檢查server上是否有『dir』的存在。 -o password_stdin 從stdin上面讀取密碼(必須要掛載pam)。 -o SSHOPT=VAL 有額外的ssh options可以在這邊指定,這部份請參考『man ssh_config』。 FUSE options: -d -o debug 允許除錯模式,隱含也允許前景操作( -f )。 -f 代表前景操作(foreground operation)。 -s 不允許multi-threaded的操作。 -o allow_other 允許存取其他的使用者(other users)。 -o allow_root 允許存取root。 -o nonempty 允許掛載到非空的檔案/資料夾上。 -o default_permissions 使用kernel來檢查權限(permission)。 -o fsname=NAME 設定檔案系統的名稱。 -o subtype=NAME 設定檔案系統的類型。 -o large_read 發起一個大量讀取(large read)的requests(2.4 only) -o max_read=N 設定讀取requests的最大size。 -o hard_remove 刪除檔案時就馬上刪除,不會做隱藏。 -o use_ino 讓檔案系統來設定你的inode numbers。 -o readdir_ino 會嘗試在用readdir讀取資料夾時,用d_ino填充。 -o direct_io 使用use direct I/O. -o kernel_cache 在kernel裡面使用cache檔。 -o [no]auto_cache 根據修改時間(modification times)來啟動caching。 -o umask=M 設定檔案權限,這邊是使用octal。 -o uid=N 設定檔案的擁有者(owner) uid。 -o gid=N 設定檔案的擁有群組(group) gid。 -o entry_timeout=T 對於names的cache timeout,預設是『1.0s』。 -o negative_timeout=T 對於deleted names的cache timeout,預設是『0.0s』。 -o attr_timeout=T 對於attributes的cache timeout,預設是『1.0s』。 -o ac_attr_timeout=T 對於attributes的auto cache timeout,預設為跟『attr_timeout』一樣 -o intr 允許requests被中斷(interrupt)。 -o intr_signal=NUM 在發起中斷時的預設signal,預設是10。 -o modules=M1[:M2...] 將資料推到檔案系統stack上的modules names。 -o max_write=N 設定寫入requests的最大size。 -o max_readahead=N 設定最大的readahead(kernel的system call)。 -o async_read 設定成非同步讀取,這是預設選項。 -o sync_read 設定成同步讀取。 Module options: 這邊分兩個modules - 『subdir』和『iconv』: [subdir] -o subdir=DIR 將這個資料夾給前置到所有的路徑上。 -o [no]rellinks 將絕對的連結路徑(absolute symlinks)轉換成相對路徑(relative)。 [iconv] -o from_code=CHARSET 檔案名稱的原始原始編碼,預設是『UTF-8』。 -o to_code=CHARSET 這個檔案名稱的新的編碼,預設是『ISO-8859-2』。 Ref:
1. sshfs man page 2. https://github.com/libfuse/sshfs 3. https://www.digitalocean.com/community/tutorials/how-to-use-sshfs-to-mount-remote-file-systems-over-ssh 4. https://tecadmin.net/install-sshfs-on-linux-and-mount-remote-filesystem/

No comments:

Post a Comment