日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux——samba共享以及基础用法

發布時間:2025/3/19 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux——samba共享以及基础用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Samba

Samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,有服務端和客戶端程序構成。隨著Linux的普及,如何共享Linux下的文件成為用戶關心的問題。其實,幾乎所有的Linux發行套件都提供了一個很好的工具Samba——通過它可以輕松實現文件共享

一、SMB文件共享

通用lnternet文件系統(CIFS)也稱為服務器信息塊(SMB),是適用于MicrosoftWindows服務器和客戶端的標準文件和打印機共享系統。
Samba服務可用于將Linux文件系統作為CIFS/SMB網絡文件共享進行共享,并將Linux打印機作為CIFS/SMB打印機共享進行共享。
[root@server ~]# ——服務端
[root@client ~]# ——客戶端

1、安裝以及啟動服務

[root@server ~]# yum install samba samba-client samba-common -y [root@server ~]# systemctl start smb [root@server ~]# systemctl enable smb.service [root@server ~]# systemctl stop firewalld [root@server ~]# systemctl disable firewalld

samba —— 服務器應用程序
samba-client —— 客戶端應用程序
samba-common —— Samba的支持文件

測試
客戶端進行測試,匿名登陸
因為在服務端未設置,所以不用輸入密碼,直接回車,登陸成功

[root@client ~]# yum install samba-client -y [root@client ~]# smbclient -L //172.25.254.227 Enter root's password: Anonymous login successful Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]Sharename Type Comment--------- ---- -------IPC$ IPC IPC Service (Samba Server Version 4.1.1) Anonymous login successful Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]Server Comment--------- -------Workgroup Master--------- ------- [root@client ~]#

2、添加smb用戶

smb用戶必須是本地用戶!!!
添加用戶student、tutu

[root@server ~]# smbpasswd -a student New SMB password: Retype new SMB password: Added user student. [root@server ~]# useradd tutu [root@server ~]# smbpasswd -a tutu New SMB password: Retype new SMB password: Added user tutu. [root@server ~]# pdbedit -L student:1000:Student User tutu:1001: [root@server ~]# smbpasswd 常用參數-a username: 添加用戶為samba用戶 -d username:禁用samba用戶username -e username: 啟用samba用戶username -x username: 刪除samba用戶username

可以用 smbpasswd 添加 smb 用戶
還可以用 pdbedit 添加 smb 用戶

pdbedit 常用參數–a username:新建Samba賬戶。–x username:刪除Samba賬戶。–L:列出Samba用戶列表,讀取passdb.tdb數據庫文件。–Lv:列出Samba用戶列表的詳細信息。–c[D]” –u username:暫停該Samba用戶的賬號。–c[]” –u username:恢復該Samba用戶的賬號。

測試

  • 輸入密碼導致的錯誤

  • 服務端samba_enable_home_dirs布爾值的影響

smbclient(選項)(參數)-L:顯示服務器端所分享出來的所有資源 -U<用戶名稱>:指定用戶名稱
  • samba_enable_home_dirs
    布爾值允許本地Linux主目錄作為CIFS文件共享導出至其他系統

    測試

3、文件的上傳

只能上傳當前所在目錄下的文件
上傳的文件在服務端的 student 用戶的家目錄

- 上傳 /etc/passwd 成功,上傳 /bin/ls 失敗

- 轉到目錄 /bin ,上傳 /bin/ls 成功

注意:這種登陸方式登陸,不能執行新建文件、目錄等!!!

看看上傳的文件(服務端)

4、掛載CIFS共享(客戶端)

  • 手動掛載
  • 開機自動掛載
[root@client ~]# vim /etc/rc.d/rc.local [root@client ~]# cat /etc/rc.d/rc.local | tail -n 1 mount //172.25.254.227/student /mnt -o username=student,password=123 [root@client ~]# chmod +x /etc/rc.d/rc.local [root@client ~]# reboot

5、域名的更改

[root@server ~]# vim /etc/samba/smb.conf 89 workgroup = MYGROUP ---> workgroup = WESTOS ##修改第89行,更改域名為WESTOS [root@server ~]# systemctl restart smb.service

6、smb服務的黒白名單

  • 白名單
    只允許 172.25.254.127 主機登陸
[root@server ~]# vim /etc/samba/smb.conf ##添加第98行內容 [root@server ~]# cat /etc/samba/smb.conf | head -n 98 | tail -n 1hosts allow = 172.25.254.127 [root@server ~]# systemctl restart smb.service

測試

  • 172.25.254.127

  • 172.25.254.50

  • 黑名單
    僅不允許 172.25.254.127 主機登陸

[root@server student]# vim /etc/samba/smb.conf ##修改第98行內容 [root@server ~]# cat /etc/samba/smb.conf | head -n 98 | tail -n 1hosts deny = 172.25.254.127 [root@server student]# systemctl restart smb

測試

  • 172.25.254.127

  • 172.25.254.50

7、共享目錄的基本設定

  • 非系統目錄的共享
[root@server ~]# mkdir /westos [root@server ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?' [root@server ~]# semanage fcontext -l | grep /westos /westos(/.*)? all files system_u:object_r:samba_share_t:s0 [root@server ~]# restorecon -FvvR /westos/ restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0 [root@server ~]# vim /etc/samba/smb.conf [root@server ~]# cat /etc/samba/smb.conf | tail -n 3 ##添加的內容[DIR] ##共享名稱comment = westos dir ##對共享目錄的描述path = /westos ##共享目錄的絕對路徑 [root@server ~]# cat /etc/samba/smb.conf | head -n 98 | tail -n 1 ##修改的內容hosts allow = 172.25.254.127/24 [root@server ~]# touch /westos/file [root@server ~]# ls /westos/ file [root@server ~]# systemctl restart smb.service

測試

  • 系統目錄的共享
[root@server ~]# vim /etc/samba/smb.conf ##添加后三行內容 [root@server ~]# cat /etc/samba/smb.conf | tail -n 3[mnt] ##共享名稱comment = /mnt dir ##對共享目錄的描述path = /mnt ##共享目錄的絕對路徑 [root@server ~]# systemctl restart smb

測試


測試
布爾值對于系統共享目錄的影響

[root@server ~]# touch /mnt/file1 [root@server ~]# ls /mnt/ file1

[root@server ~]# getenforce Enforcing [root@server ~]# setenforce 0

[root@server ~]# setenforce 1 [root@server ~]# setsebool -P samba_export_all_ro on

8、配置文件的參數

  • browseable = no | yes ——更改此參數,不用重啟服務
    no ——將該共享目錄設置為隱藏
    yes——將該共享目錄設置為顯示
[root@server ~]# vim /etc/samba/smb.conf ##添加內容 [root@server ~]# cat /etc/samba/smb.conf | head -n 324 | tail -n 1 browseable = no ##設置目錄DIR隱藏 [root@server ~]#

測試

[root@server ~]# vim /etc/samba/smb.conf ##修改內容 [root@server ~]# cat /etc/samba/smb.conf | head -n 324 | tail -n 1browseable = yes [root@server ~]#

測試

  • writable = yes|no ——更改此參數,需要重啟服務

    no | yes —— 設置用戶是否可寫(所有用戶)

[root@server ~]# vim /etc/samba/smb.conf ##添加內容 [root@server ~]# cat /etc/samba/smb.conf | head -n 325 | tail -n 1writable = yes [root@server ~]# systemctl restart smb.service

測試
寫操作失敗,是因為目錄/westos本身不可寫

[root@server ~]# ll -ld /westos/ drwxr-xr-x. 2 root root 17 Jun 4 06:27 /westos/ [root@server ~]# chmod 777 /westos/ [root@server ~]# ll -ld /westos/ drwxrwxrwx. 2 root root 17 Jun 4 06:27 /westos/ [root@server ~]#

測試
當/westos本身可寫后,寫操作執行成功

  • write list = student ——更改此參數,需要重啟服務

    允許用戶 student 進行寫操作(相當于白名單)

[root@server ~]# vim /etc/samba/smb.conf ##注釋掉第325行,添加第326行 [root@server ~]# cat /etc/samba/smb.conf | head -n 326 | tail -n 2 # writable = yeswrite list = student [root@server ~]# systemctl restart smb.service

測試

  • write list = @student ——更改此參數,需要重啟服務

    只允許屬于 student 組的用戶進行寫操作

[root@server ~]# vim /etc/samba/smb.conf [root@server ~]# cat /etc/samba/smb.conf | head -n 326 | tail -n 2 # writable = yeswrite list = @student [root@server ~]# systemctl restart smb.service [root@server ~]# id tutu uid=1001(tutu) gid=1001(tutu) groups=1001(tutu)

測試

[root@server ~]# usermod -G student tutu ##給用戶 tutu 添加 附加組 student [root@server ~]# id tutu uid=1001(tutu) gid=1001(tutu) groups=1001(tutu),1000(student) [root@server ~]#

測試

  • admin users = 用戶名 ——更改此參數,需要重啟服務

    共享的超級用戶指定

[root@server ~]# id tutu uid=1001(tutu) gid=1001(tutu) groups=1001(tutu),1000(student) [root@server ~]# ls -ld /westos/ drwxrwxrwx. 2 root root 30 Jun 4 08:18 /westos/ [root@server ~]# ll /westos/ total 0 -rw-r--r--. 1 student student 0 Jun 4 08:03 file1 -rw-r--r--. 1 tutu tutu 0 Jun 4 08:18 file2 [root@server ~]# vim /etc/samba/smb.conf [root@server ~]# cat /etc/samba/smb.conf | head -n 327 | tail -n 3writable = yes # write list = @studentadmin users = tutu [root@server ~]# systemctl restart smb.service

測試

9、多用戶掛載

不同用戶創建的文件、目錄等,所有的用戶都可以看
這是不合理的,下面的操作就是消除這種不合理
即設置多用戶掛載,掛載是需要 smb 認證

[root@client ~]# yum install cifs-utils -y [root@client ~]# vim /root/smbpass [root@client ~]# cat /root/smbpass username=student password=123 [root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.227/DIR /mnt/ ##credentials=/root/smbpass 指定掛載時所用到的用戶文件 ##multiuser 支持多用戶認證 ##sec=ntlmssp 認證方式為標準smb認證方式(注意版本不同的認證) ## The default in mainline kernel versions prior to v3.8 was sec=ntlm. Inv3.8, the default was changed to sec=ntlmssp. [root@client ~]# ls /mnt/ file1 file2 file3 [root@client ~]# useradd test [root@client ~]# su - test [test@client ~]$ ls /mnt ls: cannot access /mnt: Permission denied ####沒有smb認證,無法進行共享 [test@client ~]$ cifscreds add -u tutu 172.25.254.227 Password: ##smb用戶tutu的密碼,輸入錯誤的密碼 [test@client ~]$ ls /mnt ls: cannot access /mnt: Permission denied ##仍然沒有權限 [test@client ~]$ cifscreds add -u tutu 172.25.254.227 ##再次認證失敗 You already have stashed credentials for 172.25.254.227 (172.25.254.227) If you want to update them use:cifscreds update [test@client ~]$ cifscreds clearall ##清除認證 [test@client ~]$ cifscreds add -u tutu 172.25.254.227 Password: ##smb用戶tutu的密碼,輸入正確密碼 [test@client ~]$ ls /mnt file1 file2 file3 [test@client ~]$ touch /mnt/file4 ##創建的文件file4屬于root用戶,因為用戶tutu被指定為共享的超級用戶 [test@client ~]$ ll /mnt/ total 0 -rw-r--r-- 1 student student 0 Jun 4 08:03 file1 -rw-r--r-- 1 admin admin 0 Jun 4 08:18 file2 -rw-r--r-- 1 root admin 0 Jun 4 08:30 file3 -rw-r--r-- 1 root admin 0 Jun 4 08:57 file4 [test@client ~]$ logout [root@client ~]# umount /mnt/

10、匿名用戶對于共享目錄的登陸與訪問

測試

[root@server ~]# vim /etc/samba/smb.conf ##添加第125、328行 [root@server ~]# cat /etc/samba/smb.conf | head -n 125 | tail -n 1map to guest = bad user [root@server ~]# cat /etc/samba/smb.conf | head -n 328 | tail -n 1guest ok = yes [root@server ~]# systemctl restart smb.service

測試

[root@client ~]# mount //172.25.254.227/DIR /mnt/ -o username=guest Password for guest@//172.25.254.227/DIR: ##任意輸入

這兩條命令在效果上等價

[root@client ~]# mount //172.25.254.227/DIR /mnt/ -o username=guest,password=""

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的linux——samba共享以及基础用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。