CentOS 配置 Samba 服务指南 🖥️

📋 导航目录
🌟 简介
本指南将帮助您在 CentOS 系统上部署和配置 Samba 服务,实现文件共享功能。Samba 允许 Linux/Unix 系统与 Windows 系统之间共享文件和打印机。
🔧 一、Samba 基础设置
1. 更新系统并安装 Samba
1
| yum update -y && yum install samba -y && sudo systemctl enable smb && sudo systemctl restart smb
|
2. 创建 admin 用户并设置密码
1
| useradd admin && smbpasswd -a admin
|
系统会提示您输入并确认 admin 用户的 Samba 密码
⚙️ 二、CentOS Samba 服务端配置
1. 备份原始配置文件
1
| cp -i /etc/samba/smb.conf /etc/samba/smb.conf.bak && cd /etc/samba && ls
|
2. 方法一:覆盖 Samba 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| mkdir -pm 755 /mnt && chmod +x /etc/samba/smb.conf && cat > /etc/samba/smb.conf <<'EOF'
[global] workgroup = WORKGROUP deadtime = 99 server string = Samba on CentOS netbios name = CentOS unix charset = UTF-8 enable core files = no security = user map to guest = Bad User socket options = IPTOS_LOWDELAY TCP_NODELAY max xmit = 131072 min receivefile size = 131072 fake oplocks = Yes use sendfile = Yes aio read size = 0 aio write size = 0 server min protocol = NT1 ntlm auth = ntlmv1-permitted load printers = No printcap name = /dev/null disable spoolss = yes printing = bsd mdns name = mdns veto files = /Thumbs.db/.DS_Store/._.DS_Store/.apdisk/ delete veto files = yes
[CentOS] comment = mysamba path = /mnt browseable = yes writeable = yes read only = no create mask = 0777 directory mask = 0777 guest ok = no force user = root force group = root admin users = admin valid users = admin,root password required = yes available = yes fruit:encoding = native fruit:metadata = stream fruit:veto_appledouble = no vfs objects = catia fruit streams_xattr EOF chown admin /mnt/ && systemctl restart smb && hostname -i
|
3. 方法二:追加 Samba 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| mkdir -pm 755 /mnt && chmod +x /etc/samba/smb.conf && tee -a /etc/samba/smb.conf <<'EOF'
[CentOS] comment = mysamba path = /mnt browseable = yes writeable = yes read only = no create mask = 0777 directory mask = 0777 guest ok = no force user = root force group = root admin users = admin valid users = admin,root password required = yes available = yes fruit:encoding = native fruit:metadata = stream fruit:veto_appledouble = no vfs objects = catia fruit streams_xattr EOF
chown admin /mnt/ && systemctl restart smb && hostname -i
|
4. 方法三:一键部署脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| touch ~/smb.sh && cat > ~/smb.sh <<'EOF'
yum update -y && yum install samba -y mkdir -pm 755 /mnt && cat >>/etc/samba/smb.conf <<'MYENDMARK' [CentOS] comment = mysamba path = /mnt browseable = yes writeable = yes read only = no create mask = 0777 directory mask = 0777 guest ok = no force user = root force group = root admin users = admin valid users = admin,root password required = yes available = yes fruit:encoding = native fruit:metadata = stream fruit:veto_appledouble = no vfs objects = catia fruit streams_xattr MYENDMARK
useradd admin 2>/dev/null || true (echo yifan0719; echo yifan0719) | smbpasswd -a -s admin systemctl restart smb EOF
chmod +x ~/smb.sh && bash ~/smb.sh
|
查看脚本内容:
🔗 三、CentOS 作为 Samba 客户端配置
1. 安装 Samba 客户端工具
1
| yum update -y && yum install samba-client cifs-utils -y
|
2. 创建挂载点并配置自动挂载
1 2 3 4 5 6 7 8
| mkdir -pm 755 /mnt/ARS2-SMB && tee -a /etc/fstab <<'EOF'
//10.10.10.251/ARS2 /mnt/ARS2-SMB cifs username=root,password=yifan0719,uid=1000,gid=1000,file_mode=0777,dir_mode=0777 0 0 EOF
mount -a
|
注意:在实际使用中,建议使用凭据文件而不是直接在 fstab 中存储密码,以提高安全性。
3. 使用凭据文件(更安全的方法)
1 2 3 4 5 6 7 8 9 10 11 12
| echo "username=root" > /root/.smbcredentials echo "password=yifan0719" >> /root/.smbcredentials chmod 600 /root/.smbcredentials
sed -i '/\/\/10.10.10.251\/ARS2/d' /etc/fstab echo "//10.10.10.251/ARS2 /mnt/ARS2-SMB cifs credentials=/root/.smbcredentials,uid=1000,gid=1000,file_mode=0777,dir_mode=0777 0 0" >> /etc/fstab
umount /mnt/ARS2-SMB 2>/dev/null mount -a
|
🔍 四、验证 Samba 服务
1. 安装 Samba 客户端工具
1
| yum update -y && yum install samba-client -y
|
2. 验证 Samba 服务
1
| smbclient //10.10.10.246/CentOS -U admin
|
输入密码后,使用 ls
命令查看共享内容,按 Ctrl+C
退出
3. 检查 Samba 安装状态
4. 查看 Samba 服务状态
1 2
| systemctl status smb systemctl status nmb
|
5. 查看 Samba 用户列表
6. 添加 root 用户到 Samba(可选)
🛡️ 五、防火墙和 SELinux 配置
1. 配置防火墙允许 Samba
1 2 3 4 5 6 7 8 9 10 11 12 13
| systemctl status firewalld
firewall-cmd --permanent --add-service=samba firewall-cmd --reload
firewall-cmd --permanent --add-port=139/tcp firewall-cmd --permanent --add-port=445/tcp firewall-cmd --permanent --add-port=137/udp firewall-cmd --permanent --add-port=138/udp firewall-cmd --reload
|
2. 配置 SELinux(如果启用)
1 2 3 4 5 6 7
| sestatus
chcon -t samba_share_t /mnt setsebool -P samba_enable_home_dirs on setsebool -P samba_export_all_rw on
|
💡 六、故障排除
1. 检查配置文件语法
2. 查看 Samba 日志
1 2
| tail -f /var/log/samba/log.smbd tail -f /var/log/samba/log.nmbd
|
3. 测试网络连通性
1 2 3 4 5
| ping 10.10.10.251
smbclient -L //10.10.10.251 -U root
|
4. 重新启动服务
1
| systemctl restart smb nmb
|
🔒 七、安全建议
- 使用强密码:为 Samba 用户设置复杂密码
- 定期更新:保持系统和 Samba 软件包最新
- 限制访问:使用防火墙限制对 Samba 端口的访问
- 使用专用用户:为 Samba 共享创建专用用户,避免使用 root
- 定期备份配置:备份
/etc/samba/smb.conf
文件
- 监控日志:定期检查 Samba 日志文件
🚀 通过以上步骤,您已经在 CentOS 上成功部署和配置了 Samba 服务,可以实现与 Windows 系统和其他 Linux 系统的文件共享。无论是作为服务器还是客户端,Samba 都提供了强大的文件共享功能!