通過Samba實現Linux與Windows間的檔案分享權限設定

來源:互聯網
上載者:User

標籤:驗證   linux與   poc   ade   yum   har   intern   模式   mys   

Samba

Samba,是用來讓Linux系列的作業系統與Windows作業系統的SMB/CIFS(Server Message Block/Common Internet File System)網路通訊協定做連結的自由軟體,最大的功能就是可以用於Linux與windows系統直接的檔案分享權限設定和列印共用(Linux與Linux之間的資源共用更多用NFS實現)。
組成Samba啟動並執行有兩個服務,一個是SMB,另一個是NetBIOS。SMB是Samba的核心啟動服務,主要負責建立Samba伺服器與Samba客戶機之間的對話,驗證使用者身份並提供對檔案和列印系統的訪問,監聽139 TCP連接埠(增強版SMB協議CIFS直接監聽445連接埠,CIFS不需要NetBIOS協議);而NetBIOS服務是負責解析,提供瀏覽網路上的共用資源列表,監聽UDP連接埠137和138。

實現Samba
  • 安裝Samba:
[[email protected] ~]# lsb_release -rRelease:    7.2.1511[[email protected] ~]# yum -y install samba[[email protected] ~]# rpm -qi sambaName        : sambaEpoch       : 0Version     : 4.7.1Release     : 6.el7Architecture: x86_64[[email protected] ~]# systemctl stop firewalld.service[[email protected] ~]# setenforce 0
  • Samba相關配置:
  主設定檔:/etc/samba/smb.conf  主程式:nmbd(NetBIOS Name Server Daemon)、smbd(SMB/CIFS Daemon)  Unit檔案:smb.service和nmb.service
其中
/etc/samba/smb.conf檔案常見參數:  全域配置:[global] workgroup=MYGROUP #工作群組名 server string=Samba Server Version %v #伺服器資訊介紹 netbios name=MYSERVER #用netbios名來指定服務 interfaces=[interface1 interface2...|address1 address2...] #用於讓samba服務監聽多個網路介面或IP hosts allow=[address1 address2...] #指定允許訪問的主機IP log file=/var/log/samba/log.%m #指定日誌存放路徑,%m為來訪的主機名稱 max log size=50 #定義記錄檔最大容量為50K security=user #設定samba服務的安全認證方式為user passdb backend=tdbsam #定義使用者背景類型為tdbsam,其他類型還有smbpasswd、ldapsam load prints=yes #設定是否共用印表機 cups options=raw #印表機選項  其它配置:[homes]:為每個samba使用者定義其是否能夠通過samba服務訪問自己的家目錄[printers]:定義列印服務[自訂共用目錄]:定義共用的檔案系統  常用指令:comment=STRING:注釋path=/PATH/TO/FILENAME:當前共用所映射的檔案系統路徑browseable=YES:是否可瀏覽,指是否可被所有使用者查看pulibc=YES:是否允許匿名訪問browseable=No:是否公開目錄writable=YES:是否可寫read only=no|yes:是否為唯讀write list=/PATH/TO/user_list|USERNAME:擁有寫入權限的使用者列表
directory mask=MASK:建立目錄的許可權值
create mask=MASK:建立檔案的許可權值  更多參數可通過命令 man smb.conf 查看。
  • 自訂共用目錄:
[[email protected] ~]# vim /etc/samba/smb.conf[shared_dir]        comment=shared dir through samba        path=/samba_dir        writable=yes[[email protected] ~]# mkdir /samba_dir  #建立共用檔案[[email protected] ~]# testparm  #校正/etc/samba/smb.conf檔案的配置是否正確Load smb config files from /etc/samba/smb.confrlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)Processing section "[homes]"Processing section "[printers]"Processing section "[print$]"Processing section "[shared_dir]"Loaded services file OK.Server role: ROLE_STANDALONE
  • samba服務預設的驗證模式為user,因此需要建立samba使用者資料庫:
samba帳號必須存在於Linux系統中(/etc/passwd),而其密碼卻需要單獨維護。建立samba使用者資料庫有專門的命令:pdbedit 和 smbpasswd。  pdbedit [options] account  options:      -a USERNAME:建立samba使用者      -x:刪除samba使用者      -L:列出samba使用者列表      -Lv:列出使用者詳細資料列表  smbpasswd [options] USERNAME  options:      -a:添加帳號      -x:刪除帳號      -d:禁用帳號      -e:啟用帳號[[email protected] ~]# useradd samba_user1[[email protected] ~]# pdbedit -a samba_user1new password:retype new password:Unix username:        samba_user1[[email protected] ~]# useradd samba_user2[[email protected] ~]# smbpasswd -a samba_user2New SMB password:Retype new SMB password:Added user samba_user2.
  •  windows主機測試:
# 啟動服務[[email protected] ~]# systemctl start smb.service[[email protected] ~]# systemctl start nmb.service
# 用戶端測試
  •  可以看到,使用者登入後無法在/samba_dir下建立目錄,明明已經設定了 writable=yes。這是因/samba_dir目錄的屬主和屬組均為root,其他使用者沒有寫入權限。進行如下修改:
[[email protected] ~]# groupadd samba[[email protected] ~]# usermod -G samba samba_user1[[email protected] ~]# usermod -G samba samba_user2[[email protected] ~]# id samba_user1uid=1005(samba_user1) gid=1005(samba_user1) groups=1005(samba_user1),1007(samba)[[email protected] ~]# id samba_user2uid=1006(samba_user2) gid=1006(samba_user2) groups=1006(samba_user2),1007(samba)[[email protected] ~]# ll -d /samba_dirdrwxr-xr-x. 2 root root 6 Jun 29 17:19 /samba_dir[[email protected] ~]# chown :samba /samba_dir[[email protected] ~]# chmod g+w /samba_dir[[email protected] ~]# ll -d /samba_dirdrwxrwxr-x. 2 root samba 6 Jun 29 17:19 /samba_dir
  • windows主機重新測試:

linux主機進行測試:

# 掛載訪問。注意,此處訪問的是/shared_dir,設定檔/etc/samba/smb.conf中 [] 定義的名稱。[[email protected] ~]# mount -t cifs //192.168.4.119/shared_dir /mnt -o username=samba_user1Password for [email protected]//192.168.4.119/shared_dir:  ******[[email protected] ~]# ls /mnthello.txt  test[[email protected] ~]# touch /mnt/hello[[email protected] ~]# ls /mnthello  hello.txt  test# 使用smbclient用戶端訪問[[email protected] ~]# yum install -y samba-client[[email protected] ~]# smbclient //192.168.4.119/shared_dir -U samba_user2  #互動式訪問Enter SAMBA\samba_user2‘s password: Try "help" to get a list of possible commands.smb: \> ls  .                                   D        0  Tue Jul  3 17:07:35 2018  ..                                 DR        0  Fri Jun 29 17:19:56 2018  test                                D        0  Tue Jul  3 15:42:01 2018  hello.txt                           A        0  Tue Jul  3 17:07:31 2018  hello                               N        0  Tue Jul  3 17:07:35 2018        121055488 blocks of size 1024. 116173648 blocks availablesmb: \> [[email protected] ~]# smbclient -L //192.168.4.119/shared_dir -U samba_user1  #查看共用情況Enter SAMBA\samba_user1‘s password:     Sharename       Type      Comment    ---------       ----      -------    print$          Disk      Printer Drivers    shared_dir      Disk      shared dir through samba    IPC$            IPC       IPC Service (Samba 4.7.1)    samba_user1     Disk      Home DirectoriesReconnecting with SMB1 for workgroup listing.    Server               Comment    ---------            -------    Workgroup            Master    ---------            -------    SAMBA                HAPPINESS    WORKGROUP            DESKTOP-L064DV0

通過Samba實現Linux與Windows間的檔案分享權限設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.