shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。


shell需求:

1.能够实现一键安装并配置samba服务,执行该脚本时需要带一个共享的路径,它是共享的目录,目录若存在,自动创建samba。
2.要求任何人都可以访问不需要密码,并且只读。


shell分析:

1.判断用户给出的目录是不是绝对路径,即是否以”/”开头。
2.脚本需要判断samba服务是否已经安装,如果已经安装,则不需要执行yum安装samba服务,直接跳到下一步。
3.预先设置修改配置文件。
4.开启samba服务。



代码:

#!/bin/bash
if [ “$#” -ne 1 ]
then
echo “运行脚本格式为:$0 /dir/”
exit 1
else
if ! echo $1 |grep -q ‘^/.*’
then
echo “请提供一个绝对路径。”
exit 0
fi
fi

if ! rpm -q samba >/dev/null
then
echo “即将安装samba”
sleep 1
yum -y install samba
echo “已完成安装”
if [ $? -ne 0 ]
then
echo “samba 安装失败”
exit 1
fi
fi

echo “正在进行配置…”
dirconf="/etc/samba/smb.conf"
cat >> $dirconf << EOF
[global]
workgroup = workgroup
security = user
map to guest = bad user
[share]
comment= share all
path = $1
browseable = yes
public = yes
writable = no
EOF

if [ ! -d $1 ]
then
mkdir -p $1
fi

chmod 777 $1
chown nobody:nobody $1
echo “welcome to samba” > $1/samba.txt
echo “配置已完成”

echo “正在开启samba服务…”
systemctl start smb
if [ $? -ne 0 ]
then
echo “samba服务启动失败,请检查配置文件是否正常”
else
echo “samba服务启动正常”
fi



运行脚本:
chmod +x samba.sh
./samba.sh /opt/share
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。
Samba服务搭建完成。




验证:

share共享目录已经创建。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。


打开windows此电脑,点击映射网络驱动器。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。



路径填写搭建samba服务那台Linux的IP地址,可以通过ip add命令查看。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。



windows上只能看,不能进行其他操作,完成。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。
shell 一键安装搭建samba服务,任何人都可以访问不需要密码,并且目录只读。