Linux 服务器下安装与配置Samba服务【测试成功】

艺帆风顺 发布于 2025-04-02 17 次阅读


一、基础概念

    Samba是一种实现SMB(Server Messages Block,信息服务块)协议的开源软件,主要用于Samba是一种实现SMB(Server Messages Block,信息服务块)协议的开源软件,主要用于在Linux和Windows之间进行文件和打印机等资源的共享。它是基于Client/Server型的协议,允许Linux系统的文件和打印服务与Windows用户进行资源共享。Samba的核心守护进程是smbd和nmbd。

二、需求场景

    Samba服务端:192.168.1.132     Centos 8.8系统

    Samba服务端192.168.1.132      Centos 8.8系统

三、Samba服务端配置

1、服务安装

    yum -y install samba samba-client

    systemctl start smb nmb

    systemctl status smb nmb

    systemctl enable smb nmb

    ps -ef | grep -E 'smb|nmb'

    netstat -tunlp | grep -E 'smbd|nmbd'

    [root@Master storage]# systemctl status smb nmbsmb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2024-01-05 15:52:35 CST; 14min ago Docs: man:smbd(8) man:samba(7) man:smb.conf(5) Main PID: 62713 (smbd) Status: "smbd: ready to serve connections..." Tasks: 4 (limit: 12150) Memory: 5.9M CGroup: /system.slice/smb.service ├─62713 /usr/sbin/smbd --foreground --no-process-group ├─62717 /usr/sbin/smbd --foreground --no-process-group ├─62718 /usr/sbin/smbd --foreground --no-process-group └─63306 /usr/sbin/smbd --foreground --no-process-group
    1月 05 15:52:35 Master systemd[1]: Starting Samba SMB Daemon...1月 05 15:52:35 Master smbd[62713]: [2024/01/05 15:52:35.037273, 0] ../../source3/smbd/server.c:1741(main)1月 05 15:52:35 Master smbd[62713]: smbd version 4.17.5 started.1月 05 15:52:35 Master smbd[62713]: Copyright Andrew Tridgell and the Samba Team 1992-20221月 05 15:52:35 Master systemd[1]: Started Samba SMB Daemon.
    nmb.service - Samba NMB Daemon Loaded: loaded (/usr/lib/systemd/system/nmb.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2024-01-05 15:52:35 CST; 14min ago Docs: man:nmbd(8) man:samba(7) man:smb.conf(5)
    [root@Master storage]# ps -ef | grep -E 'smb|nmb'root 62713 1 0 15:52 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-grouproot 62715 1 0 15:52 ? 00:00:00 /usr/sbin/nmbd --foreground --no-process-grouproot 62717 62713 0 15:52 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-grouproot 62718 62713 0 15:52 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-grouproot 63306 62713 0 15:59 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-grouproot 63956 63855 0 16:07 pts/1 00:00:00 grep --color=auto -E smb|nmb[root@Master storage]# netstat -tunlp | grep -E 'smbd|nmbd'tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 62713/smbd tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 62713/smbd tcp6 0 0 :::445 :::* LISTEN 62713/smbd tcp6 0 0 :::139 :::* LISTEN 62713/smbd udp 0 0 172.17.255.255:137 0.0.0.0:* 62715/nmbd udp 0 0 172.17.0.1:137 0.0.0.0:* 62715/nmbd udp 0 0 172.18.255.255:137 0.0.0.0:* 62715/nmbd udp 0 0 172.18.0.1:137 0.0.0.0:* 62715/nmbd udp 0 0 192.168.1.255:137 0.0.0.0:* 62715/nmbd udp 0 0 192.168.1.132:137 0.0.0.0:* 62715/nmbd udp 0 0 0.0.0.0:137 0.0.0.0:* 62715/nmbd udp 0 0 172.17.255.255:138 0.0.0.0:* 62715/nmbd udp 0 0 172.17.0.1:138 0.0.0.0:* 62715/nmbd udp 0 0 172.18.255.255:138 0.0.0.0:* 62715/nmbd udp 0 0 172.18.0.1:138 0.0.0.0:* 62715/nmbd udp 0 0 192.168.1.255:138 0.0.0.0:* 62715/nmbd udp 0 0 192.168.1.132:138 0.0.0.0:* 62715/nmbd udp 0 0 0.0.0.0:138 0.0.0.0:* 62715/nmbd

    2、创建用户与目录

      mkdir -p /storage/shareduseradd -s /sbin/nologin adminuseradd -g admin -s /sbin/nologin usersmbpasswd -a adminsmbpasswd -a user

      3、设置目录权限属性

        cd /storage/lschown admin.admin sharedchmod -R 777 shared

        4、修改服务配置文件

            vi /etc/samba/smb.conf

          [global] workgroup = SC.LOCAL server string = Samba Server Version %v netbios name = Linuxidc-Server log file = /var/log/samba/%m.log max log size = 10240 security = user passdb backend = tdbsam
          [shared] # 共享文件目录描述 comment = Shared Directories # 共享文件目录 path = /storage/shared/ # 是否允许guest访问 public = no # 指定管理用户 admin users = admin # 可访问的用户组、用户 valid users = @admin # 是否浏览权限 browseable = yes # 是否可写权限 writable = yes # 文件权限设置 create mask = 0777 directory mask = 0777 force directory mode = 0777 force create mode = 0777

          5、重启服务、确认防火墙都已放行、SeLinux处于放行状态

           systemctl restart smb nmb

          6、服务验证

              smbstatus     

            [root@Master storage]# smbstatus
            Samba version 4.17.5PID Username Group Machine Protocol Version Encryption Signing ----------------------------------------------------------------------------------------------------------------------------------------66086 admin admin 192.168.1.139 (ipv4:192.168.1.139:36756) SMB3_11 - partial(AES-128-CMAC)
            Service pid Machine Connected at Encryption Signing ---------------------------------------------------------------------------------------------shared 66086 192.168.1.139 五 1月 5 16时36分18秒 2024 CST - -
            No locked files

            二、Samba客户端连接测试

            1、安装客户端工具

            yum install samba-client

            2、连接测试

              smbclient //192.168.1.132/shared -U admin然后输入密码进入-U  指定用户名/shared 共享模块

              3、下载文件测试

              下载:    get 文件名

                [root@Node47 dc]# smbclient //192.168.1.132/shared -U adminEnter SAMBAadmin's password: Try "help" to get a list of possible commands.smb: > getget [localname]smb: > get 1getting file 1 of size 0 as 1 (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec)

                上传:put  文件名

                  [root@Node47 dc]# smbclient //192.168.1.132/shared -U adminEnter SAMBAadmin's password: Try "help" to get a list of possible commands.smb: > put 22 does not existsmb: > put 22/ 2.txt smb: > put 2.txt putting file 2.txt as 2.txt (5.2 kb/s) (average 5.2 kb/s)

                  多个上传: mput *

                    [root@Node47 dc]# smbclient //192.168.1.132/shared -U adminEnter SAMBAadmin's password: Try "help" to get a list of possible commands.smb: > put 22 does not existsmb: > put 22/ 2.txt smb: > put 2.txt putting file 2.txt as 2.txt (5.2 kb/s) (average 5.2 kb/s)smb: > mput *Put file 2.txt? yputting file 2.txt as 2.txt (7.8 kb/s) (average 6.2 kb/s)Put file 2? yputting file 2 as 2 (0.0 kb/s) (average 5.2 kb/s)Put file 1? yputting file 1 as 1 (0.0 kb/s) (average 4.5 kb/s)