安装samba服务器软件
查询yum仓库是否存在安装包
[root@localhost ~]# yum list | grep samba
如上图所示为安装包
将其安装
[root@localhost ~]# yum -y install samba
安装完成后,将其配置文件进行备份
[root@localhost ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@localhost ~]# cp /etc/samba/smb.conf.bak /etc/samba/smb.conf
[root@localhost ~]# vim /etc/samba/smb.conf
配置文件中选项内容解释:
- [global]:全局设置
- [homes]:用户目录共享位置
- [printers]:打印机共享位置
- [自定义]:自定义名称的共享目录位置
在配置文件最后一行根据需要添加如下配置
[myshare] //自定义名称
comment = my share is file //说明
path = /samba/share //共享文件的路径
browseable = Yes //是否可查看
valid users = user01, user02 //设置可用账号
#guest ok = Yes //是否可访问
#writable = Yes //是否可写
#read only = Yes //只读
创建共享文件夹
[root@localhost ~]# mkdir /samba/share -p
创建待分享文件
[root@localhost ~]# touch /samba/share/{1..10}.txt
更改共享文件夹的权限
[root@localhost ~]# chmod 777 /samba/share/ -R
开启samba服务器
[root@localhost ~]# systemctl start smb
[root@localhost ~]# systemctl start nmb
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
添加程序用户
[root@localhost ~]# useradd user01
[root@localhost ~]# pdbedit -a useradd
配置好密码
windowns真机测试
按下win+R运行输入:
输入刚才创建的用户
如图所示连接成功
Linux测试
在测试主机上安装samba-client客户端
[root@localhost ~]# yum -y install samba-client
输入如下连接服务器
[root@localhost ~]# smbclient -U user01 //192.168.100.10/myshare
输入ls查看当前文件夹内容
smb: \> ls
如图所示表示连接成功
另附自动化安装脚本
#!/usr/bin/bash
#Author:Zhi-peng He
#Created Time:2021-05-11 8:31
#Script Description:This is a script to automate the deployment of a Samba server
#Release:1.0.0
yum list | grep '^samba\.' #检测yum仓库是否存在安装包
if [ $? -ne 0 ] ;then
echo "您的yum仓库中不存在此包,请配置好yum源"
exit 1
fi
echo "samba服务即将安装------"
sleep 3
yum -y install samba #开始安装samba服务器
if [ $? -ne 0 ];then #判断是否安装成功
echo "安装出错。。。"
exit 2
fi
id user001 #查询待创建账号是否存在,不存在则创建
if [ $? -ne 0 ];then
useradd user001 #创建初始登录用户
fi
if [ ! -b /etc/samba/smb.conf ];then #判断配置文件是否存在
touch /etc/samba/smb.conf #不存在则创建文件
fi
if [ ! -d /samba/share ];then #判断待分享目录是否存在
mkdir /samba/share -p
fi
cat >> /etc/samba/smb.conf <<-EOF
[myshare]
comment = my share is file
path = /samba/share
browseable = Yes
valid users = user001
guest ok = Yes
writable = Yes
EOF
chmod 777 /samba/share -R #更改分享目录的权限为777
which expect
if [ $? -ne 0 ];then #判断是否已经安装expect
yum -y install expect tcl tclx tcl-devel
fi
password='123456'
#创建初始登录用户
/usr/bin/expect <<-EOF
set time 30
spawn pdbedit -a user001
expect {
"*password:" { send "$password\r"; exp_continue }
"*password:" { send "$password\r" }
}
EOF
if [ $? -ne 0 ];then #是否执行成功
echo "错误"
exit 3
fi
systemctl start smb #开启smb服务
systemctl start nmb #开启nmb服务
systemctl stop firewalld #关闭防火墙
setenforce 0 #关闭selinux
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #永久关闭
echo "===========================================+ samba服务安装成功 +====================================================="
echo "===========================================+ 初始登录用户为user001 +================================================="
echo "===========================================+ 初始登录密码为"$password" +============================================="