Ubuntu Samba伺服器配置
最近做開源軟體方面的東西。有些時候一個包會涉及到好幾個作業系統,其中就有windows和Linux,這兩個OS之間傳包不是很方便,不能用scp或者ssh上去操作,為方便起見,在自己的ubuntu上搭建了一個samba伺服器,方便windows和linux間檔案的共用。
1. 安裝samba軟體包
brandon@ubuntu:~$ sudo apt-get install samba
brandon@ubuntu:~$ sudo apt-get install smbfs
brandon@ubuntu:~$ smbd --versionVersion 4.1.6-Ubuntu
若要通過圖形介面安裝,還可安裝軟體包:python-glade2, system-config-samba。python-glade2可用於在建立GUI中調用Python的庫,system-config-samba,也是基於GUI的對samba的管理組件。
2.建立要共用的目錄,並修改許可權
brandon@ubuntu:~$ mkdir -p shareWindowsbrandon@ubuntu:~$ sudo chmod 777 shareWindows/
brandon@ubuntu:~$ ls -l | grep shareWindowsdrwxrwxrwx 2 brandon brandon 4096 4月 20 20:19 shareWindows
3.對原來的samba設定檔備份,然後修改samba設定檔
brandon@ubuntu:~$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
brandon@ubuntu:~$ sudo gedit /etc/samba/smb.conf
由於smb.conf檔案是唯讀,所以最好彆強行修改許可權後用vim修改,直接用gedit,文本編輯方式好操作一些。
修改已下欄位:
# Change this to the workgroup/NT-domain name your Samba server will part of
workgroup = VMWARE
display charset = UTF-8
unix charset = UTF-8
[shareWindows]
comment = Ubuntu File Server Share
path = /home/brandon/shareWindows
available = yes
browseable = yes
guest ok = yes
read only = no
public = yes
writable = yes
create mask = 0755
directory mask = 0700
force user = nobody
force group = nogroup
brandon@ubuntu:~$ sudo chown nobody:nogroup /home/brandon/shareWindows/brandon@ubuntu:~$ ls -l | grep shareWindowsdrwxrwxrwx 2 nobody nogroup 4096 4月 20 22:59 shareWindows
如果配置時想填寫workstation name, 到windows機器上查詢一下workstation name:
可以在cmd中輸入命令:net config workstation
不加入同一個workgroup 也是可以的。force user = nobody force group = nogroup 和命令修改作用是一樣的,選擇其中一個就可。
然後重啟一下samba服務:
brandon@ubuntu:~$ sudo service smbd restart
4.在windows機器上訪問該檔案夾
查詢IP:
brandon@ubuntu:~$ ifconfig
然後在windows機器上可以通過ip來訪問samba server上的shareWindows檔案夾:這裡要輸入相應的ip地址
可以看到建立的shareWindows檔案夾了:
5.驗證一下檔案讀寫:
1)在linux機器上建立一個空檔案夾, 在windows機器中查看
brandon@ubuntu:~$ cd shareWindows/brandon@ubuntu:~/shareWindows$ touch new.txtbrandon@ubuntu:~/shareWindows$ ls -ltotal 0-rw-rw-r-- 1 brandon brandon 0 4月 20 23:37 new.txt
在windows機器中查看,其中時間不一樣是因為兩個系統的時間沒有設定統一
2).在windows機器中建立一個test.txt檔案,並寫入:This is a test file. 在ubuntu機器中查看
在ubuntu機器上查看:
brandon@ubuntu:~/shareWindows$ ls -ltotal 4-rw-rw-r-- 1 brandon brandon 0 4月 20 23:37 new.txt-rwxr--r-- 1 nobody nogroup 22 4月 20 23:52 test.txtbrandon@ubuntu:~/shareWindows$ cat test.txtThis is a test file.brandon@ubuntu:~/shareWindows$
6.還可以建立有系統管理權限的使用者和組,並設定訪問密碼,這樣從windows機器上訪問時會提示輸入密碼。
brandon@ubuntu:~$ sudo addgroup smbgrpAdding group `smbgrp' (GID 1001) ...Done.
brandon@ubuntu:~$ ls -l | egrep ?Windowsdrwxrwxr-x 2 brandon smbgrp 4096 4月 21 00:20 secureWindowsdrwxrwxrwx 2 nobody nogroup 4096 4月 20 23:47 shareWindows
brandon@ubuntu:~$ sudo chmod 0770 /home/brandon/secureWindows/brandon@ubuntu:~$ ls -l | egrep ?Windowsdrwxrwx--- 2 brandon smbgrp 4096 4月 21 00:20 secureWindowsdrwxrwxrwx 2 nobody nogroup 4096 4月 20 23:47 shareWindows
brandon@ubuntu:~$ sudo adduser brandon smbgrpAdding user `brandon' to group `smbgrp' ...Adding user brandon to group smbgrpDone.
brandon@ubuntu:~$ sudo smbpasswd -a brandonNew SMB password:Retype new SMB password:Added user brandon.brandon@ubuntu:~$
修改/etc/samba/smb.conf檔案,添加新的samba共用檔案欄位
[secureWindows]
comment = Ubuntu File Server Share
path = /home/brandon/secureWindows
available = yes
valid users = @smbgrp
browseable = yes
guest ok = no
writable = yes
重啟samba服務
brandon@ubuntu:~$ sudo service smbd restartsmbd stop/waitingsmbd start/running, process 13656
這時通過windows機器放問該檔案夾是會提示輸入密碼:
驗證:
輸入密碼進去,建立一個新檔案。
在ubuntu機器上查看
brandon@ubuntu:~/secureWindows$ ls brandon@ubuntu:~/secureWindows$ lsfileFromWin.txtbrandon@ubuntu:~/secureWindows$ cat fileFromWin.txt This is test at windows machine.brandon@ubuntu:~/secureWindows$