參考來源:
在線書籍-linux就該這么學-第12章
安裝軟件包
配置文件/etc/samba/smb.conf
運維對待配置文件的態度,非必要不增加
安裝完畢后打開Samba服務程序的主配置文件,好在參數并不多,只有37行。其中第17~22行代表共享每位登錄用戶的家目錄內容。雖然在某些情況下這可以更方便地共享文件,但這個默認操作著實有些危險,建議不要共享,將其刪除掉。第24~29行是用SMB協議共享本地的打印機設備,方便局域網內的用戶遠程使用打印機設備。當前我們沒有打印機設備,因此建議也將其刪除掉,不共享。最后的第31~37行依然為共享打印機設備的參數,同樣建議予以刪除。
刪除掉不需要的代碼是常規操作。通過關閉非必要的功能,可以讓服務程序“輕裝前進”,讓服務程序把硬件資源都用到刀刃上,使其具有更好的性能。而且,還能讓運維人員更快地找到所需的代碼。與100行代碼相比,從10行代碼中找到一個參數要容易很多。所以只要對參數有正確的認識,那么就大膽地操作吧!
用戶登錄Samba服務時采用的驗證方式。總共有4種可用參數
share:代表主機無須驗證密碼。這相當于vsftpd服務的匿名公開訪問模式,比較方便,但安全性很差。
user:代表登錄Samba服務時需要使用賬號密碼進行驗證,通過后才能獲取到文件。這是默認的驗證方式,最為常用。
domain:代表通過域控制器進行身份驗證,用來限制用戶的來源域。
server:代表使用獨立主機驗證來訪用戶提供的密碼。這相當于集中管理賬號,并不常用
創建用于訪問共享資源的賬戶信息-pdbedit命令使用
[root@centos7 database]# pdbedit -a -u user1
new password:
retype new password:
Unix username: user1
NT username:
Account Flags: [U ]
User SID: S-1-5-21-1466725758-3623168241-107824347-1000
Primary Group SID: S-1-5-21-1466725758-3623168241-107824347-513
Full Name:
Home Directory: \\centos7\user1
HomeDir Drive:
Logon Script:
Profile Path: \\centos7\user1\profile
Domain: CENTOS7
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: 三, 06 2月 2036 23:06:39 CST
Kickoff time: 三, 06 2月 2036 23:06:39 CST
Password last set: 四, 15 8月 2024 14:48:58 CST
Password can change: 四, 15 8月 2024 14:48:58 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@centos7 database]# pdbedit -L
user1:1000:
配置共享資源
[root@centos7 share]# cat -n /etc/samba/smb.conf1 # See smb.conf.example for a more detailed config file or2 # read the smb.conf manpage.3 # Run 'testparm' to verify the config is correct after4 # you modified it.5 6 [global]7 workgroup = SAMBA8 security = user9 10 passdb backend = tdbsam11 12 [database]13 comment = Do not arbitrarily modify the database file14 path = /home/database15 public = no16 writable = yes
[root@centos7 home]# ls -l /home/database/
總用量 8
-rwxr--r--. 1 user1 user1 22 4月 2 20:59 Memo.txt
檢查安全策略和防火墻
pass
重啟服務使配置生效,并保持開機自啟
[root@centos7 database]# systemctl restart smb
[root@centos7 database]# systemctl enable smb
檢查smb狀態和共享目錄信息
查看Samba服務都共享了哪些目錄,則可以使用smbclient命令來查看共享詳情;-U參數指定了用戶名稱(用哪位用戶掛載了Samba服務,就用哪位用戶的身份進行查看);-L參數列出了共享清單。
[root@centos7 database]# smbclient -U user1 -L 127.0.0.1
Enter SAMBA\user1's password: Sharename Type Comment--------- ---- -------database Disk Do not arbitrarily modify the database fileIPC$ IPC IPC Service (Samba 4.10.16)
Reconnecting with SMB1 for workgroup listing.Server Comment--------- -------Workgroup Master--------- -------
[root@centos7 database]#
驗證共享文件效果
windowns下驗證共享文件效果
linux下共享文件效果驗證
手動掛載
Samba服務程序所在主機(即Samba共享服務器)和Linux客戶端使用的IP地址,然后在客戶端安裝支持文件共享服務的軟件包(cifs-utils)
root@debian:/home/a# apt-get install cifs-utils
root@debian:/home/a# dpkg -l | grep cifs
ii cifs-utils 2:6.11-3.1+deb11u2 amd64 Common Internet File System utilities
root@debian:/home/a# mkdir /opt/smbshare
TIPS,掛載的時候,遠端地址后面跟的是共享目錄名稱(在smb服務器通過smbclient可以看到sharename),而不是具體的共享目錄路徑
root@debian:/home/a# mount -t cifs -o username=user1 '//192.168.255.129/database/' '/opt/smbshare'
Password for user1@//192.168.255.129/database/:
root@debian:/home/a# df -h /opt/smbshare/
文件系統 容量 已用 可用 已用% 掛載點
//192.168.255.129/database/ 35G 6.3G 29G 19% /opt/smbshare
root@debian:/home/a#
開機自動掛載
通過配置文件/etc/fstab實現開機自動掛載,添加在配置文件的最后一行,親測有效
root@debian:/home/a# cat -n auth.smb 1 username=user12 password=03 domain=CENTOS7
root@debian:/home/a# vim /etc/fstab
root@debian:/home/a# cat -n /etc/fstab 1 # /etc/fstab: static file system information.2 #3 # Use 'blkid' to print the universally unique identifier for a4 # device; this may be used with UUID= as a more robust way to name devices5 # that works even if disks are added and removed. See fstab(5).6 #7 # systemd generates mount units based on this file, see systemd.mount(5).8 # Please run 'systemctl daemon-reload' after making changes here.9 #10 # <file system> <mount point> <type> <options> <dump> <pass>11 # / was on /dev/sda1 during installation12 UUID=36799ae3-a97a-448f-9c4c-4c7f4df59d36 / ext4 errors=remount-ro 0 113 # swap was on /dev/sda5 during installation14 UUID=15db51e5-3ba6-4c4b-b7e9-020ed10d7d45 none swap sw 0 015 /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 016 //192.168.255.129/database /opt/smbshare cifs credentials=/home/a/auth.smb 0 0
–END提示–