1. 程式人生 > >CentOS 7 samba共享

CentOS 7 samba共享

背景 原計劃基於Windows(主)+Linux(從)搭建SQL server主從同步,基於共享同一個目錄而嘗試用samba #測試之後發現無法實現,因為Linux和Windows存放資料的資料夾不同,雙發無法識別(個人想法)

環境 [[email protected] ~]# lsb_release -a LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID:    CentOS Description:    CentOS Linux release 7.5.1804 (Core) Release:    7.5.1804 Codename:    Core

檢視是否安裝 rpm -qa samba

安裝 [[email protected] ~]# yum -y install samba samba-client 配置 進入samba配置目錄 [[email protected] ~]# cd /etc/samba/ 備份smb.conf [[email protected] samba]# mv smb.conf smb.conf.origin 新建smb.conf [[email protected] samba]# vim smb.conf 內容如下,儲存並退出 [global]         workgroup = WORKGROUP         server string = Ted Samba Server %v         netbios name = TedSamba         security = user         map to guest = Bad User         passdb backend = tdbsam

[FileShare]         comment = share some files         path = /smb/fileshare         public = yes         writeable = yes         create mask = 0644         directory mask = 0755

[WebDev]         comment = project development directory         path = /smb/webdev         valid users = ted         write list = ted         printable = no         create mask = 0644         directory mask = 0755 註釋: workgroup 項應與 Windows 主機保持一致,這裡是WORKGROUP security、map to guest項設定為允許匿名使用者訪問 再下面有兩個section,實際為兩個目錄,section名就是目錄名(對映到Windows上可以看見)。 第一個目錄名是FileShare,匿名、公開、可寫 第二個目錄嗎是WebDev,限定ted使用者訪問 預設檔案屬性644/755(不然的話,Windows上在這個目錄下新建的檔案會有“可執行”屬性) 建立使用者 [

[email protected] samba]# groupadd co3 [[email protected] samba]# useradd ted -g co3 -s /sbin/nologin [[email protected] samba]# smbpasswd -a ted New SMB password: Retype new SMB password: Added user ted. [[email protected] samba]# 注意這裡smbpasswd將使用系統使用者。設定密碼為1 建立共享目錄 [[email protected] samba]# mkdir -p /smb/{fileshare,webdev} [[email protected] samba]# chown nobody:nobody /smb/fileshare/ [[email protected] samba]# chown ted:co3 /smb/webdev/ 注意設定屬性,不然訪問不了。 啟動Samba服務,設定開機啟動 [[email protected] samba]# systemctl start smb [[email protected] samba]# systemctl enable smb Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service. [[email protected] samba]# 開放埠 [[email protected] samba]# firewall-cmd --permanent --add-port=139/tcp success [[email protected] samba]# firewall-cmd --permanent --add-port=445/tcp success [[email protected] samba]# systemctl restart firewalld [[email protected] samba]# 或者直接把防火牆關了也行。

注意:騰訊雲後臺有個安全組也要開啟響應的埠.

使用本機測試 可以使用testparm測試samba配置是否正確 [[email protected] samba]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[FileShare]" Processing section "[WebDev]" Loaded services file OK. Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters [global]     netbios name = TEDSAMBA     server string = Ted Samba Server %v     map to guest = Bad User     security = USER     idmap config * : backend = tdb

[FileShare]     comment = share some files     path = /smb/fileshare     guest ok = Yes     read only = No

[WebDev]     comment = project development directory     path = /smb/webdev     create mask = 0644

    valid users = ted     write list = ted [[email protected] samba]# root使用者的話,不用密碼可直接檢視samba伺服器情況 [[email protected] samba]# smbclient -L localhost Enter root's password: Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.4.4]

    Sharename       Type      Comment     ---------       ----      -------     FileShare       Disk      share some files     WebDev          Disk      project development directory     IPC$            IPC       IPC Service (Ted Samba Server 4.4.4) Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.4.4]

    Server               Comment     ---------            -------

    Workgroup            Master     ---------            -------  

Windows客戶端訪問\\123.206.207.197

Linux 訪問windows共享檔案 #smbclient //192.168.1.1/smb_share/ smb_user_passwd -U smb_user  #不提示輸入密碼 smbclient //115.159.24.172/DB_backup/ [email protected] -U sqladmin quit