有時收到文件系統報警,檢查後發現原來是Linux裏掛載的windows共享因爲密碼更改或服務down掉無法連接,就考慮這樣靜態的mount既無必要(就拷備份文件時用一下),又消耗資源,也有安全隱患。還是按需掛載,空閒時斷開的方式比較好,所以利用系統常用的autofs來實現
1.檢查和安裝autofs
- dpkg -l | grep autofs
- sudo apt-get install autofs5
2.編輯/etc/auto.master文件,啓用misc目錄
- root@localhost:~# cat /etc/auto.master
- #
- # Sample auto.master file
- # This is an automounter map and it has the following format
- # key [ -mount-options-separated-by-comma ] location
- # For details of the format look at autofs(5).
- #
- /misc /etc/auto.misc
- #
- # NOTE: mounts done from a hosts map will be mounted with the
- # "nosuid" and "nodev" options unless the "suid" and "dev"
- # options are explicitly given.
- #
- #/net -hosts
- #
- # Include central master map if it can be found using
- # nsswitch sources.
- #
- # Note that if there are entries for /net or /misc (as
- # above) in the included master map any keys that are the
- # same will not be seen as the first read key seen takes
- # precedence.
- #
- +auto.master
3.修改/etc/auto.misc文件,添加cifs掛載項
- root@localhost:~# cat /etc/auto.misc
- backup -fstype=cifs,username=backup_user@domain,password=backup_password ://192.168.1.1/backup
4.重啓autofs服務
- root@localhost:~# service autofs restart
5.檢查是否成功掛載
- root@localhost:~# ls -l /misc/backup
- total 3
- drwxrwxrwx 1 root root 0 2011-02-16 10:25 dns_backup
- drwxrwxrwx 1 root root 0 2011-02-16 09:37 mail_backup
- drwxrwxrwx 1 root root 0 2011-02-16 09:30 system_backup
- root@localhost:~# mount
- ...
- //192.168.1.1/backup on /misc/backup type cifs (rw,mand)
值得注意的是如果通過AD域來認證,則要在用戶名後加@domain(domain是AD域的域名,如company.com)才能成功掛載,嘗試過用戶名的另一種表示方法,domain\username,但不是每一次都能成功,所以選擇前者。雖然掛載選項顯示可讀寫,但要在遠端服務器上設置相應的權限才能操作