2015年10月22日 星期四

在windows和linux使用網路共享資料夾( samba 和cifs),非NFS

因為在 在Windows 7 使用docker  中要配置Virtual Box和windows 7 的共享資料夾失敗
所以改用samba和cifs的方式

在windows上產生共享資料夾
1. 開啟網路和共用中心 => 變更進階共用設定 => 開啟網路探索  /  開啟檔案及印表機共用 => 保存
2. 右鍵你要共享的資料夾 => 內容 => 共用 => 進階共用 => 勾選"共用此資料夾" => 權限 =>
選 Everyone 後下方勾選 "完全控制"
3. 右鍵你要共享的資料夾 => 內容 => 安全性 => 編輯 => 新增 => 輸入物件名稱來選取 填入 Everyone => 確定

在linux上存取windows的共享資料夾
$ sudo apt-get install cifs-utils
$ mkdir ~/windows_shared
掛載
$ sudo mount.cifs //192.168.169.140/test_shared /home/bear/windows_shared -o gid=1000,uid=1000

$ sudo mount -t cifs -o uid=1000,gid=1000 //192.168.169.140/test_shared /home/bear/windows_shared
注意: -o 後面的參數需以逗號(,)分開,不能用空白
查uid 和 gid
$ id
必須指定uid和gid,否則資料夾權限在linux會變成root:root

卸載
$ sudo umount -a -t cifs -l windows_shared/

NFS和samba的差異
In a closed network (where you know every device), NFS is a fine choice.
在封閉網路中( 你知道每個裝置 ),NFS是不錯的選擇
It might look more complicated than it really is but it's solid, predictable and fast. Something you can't level against Samba... At least, in my experience.
NFS比較複雜,但他可靠的、可預見的和快速的。有些事情無法和Samba放在同一層級比較
Samba will probably be a bit slower but is easy to use, and will work with windows clients as well..
Samba 可能會慢一點,但容易使用。並且能與windows端工作

$ sudo apt-get install nfs-kernel-server => 看字面就知道意思 nfs已經算linux模組了
samba其實就是網路芳鄰

在Linux上產生共享資料夾
安裝samba
# sudo apt-get install samba
建立samba的使用者和密碼
# smbpasswd -a bear
建立共享資料夾
$ mkdir ~/ubuntu_shared
編輯設定檔
# vim /etc/samba/smb.conf
格式:
[<folder_name>]
path = /home/<user_name>/<folder_name>
available = yes
valid users = <user_name>
read only = no
browsable = yes
public = yes
writable = yes
Ex.
[ubuntu_shared]
path = /home/bear/ubuntu_shared
available = yes
valid users = bear
read only = no
browsable = yes
public = yes
writable = yes
重啟samba
# service smbd restart

在windows上存取linux的共享資料夾
(假設要建在桌面)在桌面上點右鍵 => 新增 => 捷徑 =>
輸入項目的位置:
\\192.168.169.147\ubuntu_shared

之後便可在sublime 將該捷徑加入project

Windows開分享資料夾給Windows(以前的網路上的芳鄰、網芳)
https://support.microsoft.com/zh-tw/kb/2702421
先在資料夾上點右鍵 => 內容 => 共用 => 共用(按鈕) => 新增Everyone => 共用(按鈕) => 完成
其他電腦嘗試連接該共用資料夾時,出現詢問帳號密碼的對話視窗,若您沒有共用資料夾電腦上的使用者名稱及密碼是無法順利存取該資料夾。
問題的發生原因
這是因為預設是啟用以密碼保護共用,所以共用對象若沒有共用資料夾所在之電腦的使用者帳戶和密碼,即便是開放 Everyone 仍無法存取共用的檔案。
問題的解決方法
控制台 => 網路和網際網路 => 網路和共用中心 => 變更進階共用設定 => ( 家用或工作場所 => 以密碼保護的共用 )選取"關閉以密碼保護的共用"
取消共用
資料夾上點右鍵 => 內容 => 共用 => 進階共用(按鈕) => 取消勾選"共用此資料夾"

參考資料:
http://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/ How to Share Files Between Windows and Linux
http://stackoverflow.com/questions/74626/how-do-you-force-a-cifs-connection-to-unmount  How do you force a CIFS connection to unmount
http://askubuntu.com/questions/7117/which-to-use-nfs-or-samba  Which to use NFS or Samba?









沒有留言:

張貼留言