2009年9月18日 星期五

ssh over autofs

SSHFS (Secure SHell FileSystem) is a file system for Linux (and other operating systems with a FUSE implementation, such as Mac OS X or FreeBSD) capable of operating on files on a remote computer using just a secure shell login on the remote computer. On the local computer where the SSHFS is mounted, the implementation makes use of the FUSE (Filesystem in Userspace) kernel module. The practical effect of this is that the end user can seamlessly interact with remote files being securely served over SSH just as if they were local files on his/her computer. On the remote computer the SFTP subsystem of SSH is used.
先引用一下Man page省我一點口舌,SSHFS就是透過ssh連線把對方的檔案拉過來當自己檔案系統的一部分,好處是ssh連線是加密的,而且可以用金鑰認證省去每次打密碼的麻煩。
DESCRIPTION
autofs control the operation of the automount(8) daemons running on the Linux system. Usually autofs is invoked at system boot time with the start parameter and at shutdown time with the stop parameter. The autofs script can also manually be invoked by the system administrator to shutdown, restart or reload the automounters.
再引用autofs的描述,就是一種技術,當你需要檔案系統某部份的時候,系統就會自動根據路徑去自動掛檔案系統(種類很多),讓你根本不用動用系統底層去處理掛載與卸載的問題,專注於你要做的事情之上。
兩者結合在一起,就可以達到安全存取遠端少量且重要的檔案的需求,瞬間拉進來就好像長在本地一樣。

第一步,用金鑰加速連線遠端主機(名$RmoteHost)省去認證
提示:設定$RemoteHost:/etc/ssh/sshd_config
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
將你的金鑰中公鑰放進去信任名單
scp ~/.ssh/id_rsa.pub $RemoteHost:\.ssh/authorized_keys
如果沒有金鑰,你問題好大,麻煩去找鎖匠(man ssh-keygen)打一副再回來重練。

然後遠端設定就完成了(什麼?就這樣?),是的,對方只有設定完accept ssh public-key 就大致完成了,什麼連線要多久中斷之類還可以微調,不過不重要。本機機器設定以UBUNTU 9.04 為示範(本機$LocalHost,因為本人從BSD帶來的壞習慣,都是直接用root不用sudo)。
首先安裝套件sshfs autofs
apt-get install sshfs autofs
autofs有autofs4、autofs5新舊版之分,可以試試看新版,應該不會導致系統爆炸。sshfs是依賴於系統核心fuse模組,因為UBUNTU 9.04是直接把它編在核心裡,不再放在外掛模組,所以也不用多加理會,把自己加進可以使用fuse的群組

adduser $USER fuse
接下來只要設定autofs完成,就可以享受這成果了。

編輯/etc/autofs.master,加入sshfs設定
/mnt/ssh /etc/auto.sshfs uid=1001,gid=1001,--timeout=300,--ghost
/mnt/ssh就是autofs掛載起始點 設定掛進來給uid 1001 與 gid 1001的人用,不知道是誰就趕快翻翻/etc/passwd。設定過300秒就斷線。
然後繼續編寫/etc/auto.sshfs

# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage
skb -fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536 :sshfs\#$RemotUser@$RemoteHost\:
範例看得很清楚,此時只要你要求/mnt/ssh/skb下面檔案,就會自動通到
$RemotUser@$RemoteHost,基本上就是他的家目錄,也就是你一開始拷鑰匙過去的人家,這時候天涯若比鄰,你家就是我家!!
對了,記得叫autofs開機自動跑
/etc/init.d/autofs start

參考資料:Totally Seamless SSHFS under Linux using Fuse and Autofs

1 則留言:

冰的啦 提到...

讓我罵自己一聲!混帳!
考LPIC 2時居然拼錯字!!把ssh-keygen打成sshkeygen!!陳先生你美金多啊,差一點又要把155元丟掉耶!