本帖最后由 从小做起 于 2016-3-1 09:31 编辑
今天自己写了一个脚本,用于nfs vsftpd samba的三合一搭建,内容在下面,中间有一个小瑕疵,就是输入\\,会的请告知,非常感谢。 有错误和改进的欢迎大家指出
脚本内容:
#! /bin/bash
## this is the script for setup nfs , vsftpd or samba
## version 1.0
## time : 2016-2-29
## written by xingyys
##防火墙模块##
mod_firewall() {
##关闭iptables##
iptables-save > /etc/sysconfig/iptables_`date+%s`
iptables -F
service iptables save
##关闭selinux##
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
setenforce 0
fi
}
##检错模块##
mod_check() {
if [ $? != 0 ]
then
echo "Error,exit script."
exit 1
fi
}
##安装的需要报模块##
mod_yum() {
if ! rpm -qa |grep -q "^$1"
then
yum install -y $1
mod_check
else
echo -e "\033[32m $1 alreay installed \033[0m"
fi
}
########################## nfs模块 ######################################
mod_nfs() {
read -p "input IP or network segment(like 192.168.130.0/24) of other you want to share:" IP
read -p "input the directory(like /share) you want to share with other:" dir
[ -d $dir ] || mkdir -p $dir
mod_check
chmod 777 $dir
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in nfs-utils rpcbind
do
mod_yum $p
done
##编辑配置文件##
[ -f /etc/exports ] || touch /etc/exports
echo "$dir $IP(rw,sync)" > /etc/exports
mod_check
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##启动服务##
/etc/init.d/rpcbind start && /etc/init.d/nfs start
mod_check
echo -e "\033[32mnfs successful\033[0m\nuse \033[32mshowmount -e $S_IP \033[0mcheck nfs server\nuse \033[32mmount -t nfs -onolock,nfsvers=3 $S_IP:$dir /mnt\033[0m"
}
############################### vsftp模块 #################################
mod_ftp() {
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in vsftpd db4-utils
do
mod_yum $p
done
#建立系统账号##
use=virftp
cat /etc/passwd |grep $use >> /dev/null || useradd $use -s /sbin/nologin
mod_check
##建立虚拟账号##
[ -f /etc/vsftpd/vsftpd_login ] || touch /etc/vsftpd/vsftpd_login
echo -e "test\n123456" > /etc/vsftpd/vsftpd_login
mod_check && chmod 600 /etc/vsftpd/vsftpd_login
##生成库文件密码##
db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db
##创建虚拟用户文件##
[ -d /etc/vsftpd/vsftpd_user_conf ] || mkdir /etc/vsftpd/vsftpd_user_conf
mod_check
cd /etc/vsftpd/vsftpd_user_conf && touch test
mod_check
cat > test << EOF
local_root=/home/virftp/test
anonymous_enable=NO
write_enable=YES
local_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=NO
idle_session_timeout=600
data_connection_timeout=120
max_clients=10
max_per_ip=5
local_max_rate=50000
EOF
mod_check
[ -d /home/virftp/test ] || mkdir -p /home/virftp/test
mod_check
chown -R virftp:virftp /home/virftp
[ -f /etc/pam.d/vsftpd ] || touch /etc/pam.d/vsftpd
mod_check
sed -i '/#%PAM-1.0$/a\account sufficient \/lib\/security\/pam_userdb.so db=\/etc\/vsftpd\/vsftpd_login' /etc/pam.d/vsftpd
mod_check
sed -i '/#%PAM-1.0$/a\auth sufficient \/lib\/security\/pam_userdb.so db=\/etc\/vsftpd\/vsftpd_login' /etc/pam.d/vsftpd
mod_check
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##修改主配置文件##
sed -i 's/^anonymous_enable=YES/anonymous_enable=NO/g' /etc/vsftpd/vsftpd.conf
mod_check
sed -i 's/^#chroot_local_user=YES/chroot_local_user=YES/g' /etc/vsftpd/vsftpd.conf
mod_check
echo -e "guest_enable=YES\nguest_username=virftp\nvirtual_use_local_privs=YES\nuser_config_dir=/etc/vsftpd/vsftpd_user_conf" >> /etc/vsftpd/vsftpd.conf
mod_check
/etc/init.d/vsftpd start
mod_check
echo -e "\033[32mvsftpd successful.\033[0m"
echo -e "use\033[32m yum install -y lftp\033[0m install ftp-client \nuse \033[32mlftp test@$S_IP \033[0mlogin ftp server "
}
################################ Samba模块 #################################
mod_samba(){
read -p "input a directory(like /tmp/sambauser) :" Dir
[ -d $Dir ] || mkdir -p $Dir
mod_check
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in samba samba-client
do
mod_yum $p
done
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##编辑配置文件##
user=smbuser
passwd=123456
echo -e "[$user]\ncomment = share for users\npath = $Dir\nbrowseable = yes\nwriteable = yes\npublic = no" >> /etc/samba/smb.conf
mod_check
##创建用户和文件##
cat /etc/passwd |grep -q $user || useradd -M $user
mod_check
##直接给padedit密码
pdbedit -a smbuser -t << EOF
$passwd
$passwd
EOF
mod_check
/etc/init.d/smb start
mod_check
echo -e "\033[32msamba successful\033[0m"
mod_check
echo -e "if client is linux,use \033[32mmount -t cifs -o username=$user password=$passwd\033[0m"
mod_check
echo -e "if client is windows,use \033[32m\\ \\$S_IP username=$user password=$passwd\033[0m"
mod_check
}
############################## 脚本主程序 ####################################
echo "Please select in 1.nfs 2.ftp 3.samba"
select se in nfs ftp samba
do
case $se in
nfs)
if ps aux|grep -q nfs
then
echo -e "\033[32mnfs running.\033[0m"
exit 1
fi
echo "You select the nfs,install beginning..."
mod_nfs
break
;;
ftp)
if netstat -tnlp|grep -q vsftpd
then
echo -e "\033[32mvsftpd running.\033[0m"
exit 1
fi
echo "You select the ftp,install beginning..."
mod_ftp
break
;;
samba)
if netstat -tnlp|grep -q smbd
then
echo -e "\033[32msamba running.\033[0m"
exit 1
fi
echo "You select the samba,install beginning..."
mod_samba
break
;;
*)
echo "Please input a number(1-3)"
;;
esac
done
今天自己写了一个脚本,用于nfs vsftpd samba的三合一搭建,内容在下面,中间有一个小瑕疵,就是输入\\,会的请告知,非常感谢。 有错误和改进的欢迎大家指出
脚本内容:
#! /bin/bash
## this is the script for setup nfs , vsftpd or samba
## version 1.0
## time : 2016-2-29
## written by xingyys
##防火墙模块##
mod_firewall() {
##关闭iptables##
iptables-save > /etc/sysconfig/iptables_`date+%s`
iptables -F
service iptables save
##关闭selinux##
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
setenforce 0
fi
}
##检错模块##
mod_check() {
if [ $? != 0 ]
then
echo "Error,exit script."
exit 1
fi
}
##安装的需要报模块##
mod_yum() {
if ! rpm -qa |grep -q "^$1"
then
yum install -y $1
mod_check
else
echo -e "\033[32m $1 alreay installed \033[0m"
fi
}
########################## nfs模块 ######################################
mod_nfs() {
read -p "input IP or network segment(like 192.168.130.0/24) of other you want to share:" IP
read -p "input the directory(like /share) you want to share with other:" dir
[ -d $dir ] || mkdir -p $dir
mod_check
chmod 777 $dir
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in nfs-utils rpcbind
do
mod_yum $p
done
##编辑配置文件##
[ -f /etc/exports ] || touch /etc/exports
echo "$dir $IP(rw,sync)" > /etc/exports
mod_check
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##启动服务##
/etc/init.d/rpcbind start && /etc/init.d/nfs start
mod_check
echo -e "\033[32mnfs successful\033[0m\nuse \033[32mshowmount -e $S_IP \033[0mcheck nfs server\nuse \033[32mmount -t nfs -onolock,nfsvers=3 $S_IP:$dir /mnt\033[0m"
}
############################### vsftp模块 #################################
mod_ftp() {
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in vsftpd db4-utils
do
mod_yum $p
done
#建立系统账号##
use=virftp
cat /etc/passwd |grep $use >> /dev/null || useradd $use -s /sbin/nologin
mod_check
##建立虚拟账号##
[ -f /etc/vsftpd/vsftpd_login ] || touch /etc/vsftpd/vsftpd_login
echo -e "test\n123456" > /etc/vsftpd/vsftpd_login
mod_check && chmod 600 /etc/vsftpd/vsftpd_login
##生成库文件密码##
db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db
##创建虚拟用户文件##
[ -d /etc/vsftpd/vsftpd_user_conf ] || mkdir /etc/vsftpd/vsftpd_user_conf
mod_check
cd /etc/vsftpd/vsftpd_user_conf && touch test
mod_check
cat > test << EOF
local_root=/home/virftp/test
anonymous_enable=NO
write_enable=YES
local_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=NO
idle_session_timeout=600
data_connection_timeout=120
max_clients=10
max_per_ip=5
local_max_rate=50000
EOF
mod_check
[ -d /home/virftp/test ] || mkdir -p /home/virftp/test
mod_check
chown -R virftp:virftp /home/virftp
[ -f /etc/pam.d/vsftpd ] || touch /etc/pam.d/vsftpd
mod_check
sed -i '/#%PAM-1.0$/a\account sufficient \/lib\/security\/pam_userdb.so db=\/etc\/vsftpd\/vsftpd_login' /etc/pam.d/vsftpd
mod_check
sed -i '/#%PAM-1.0$/a\auth sufficient \/lib\/security\/pam_userdb.so db=\/etc\/vsftpd\/vsftpd_login' /etc/pam.d/vsftpd
mod_check
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##修改主配置文件##
sed -i 's/^anonymous_enable=YES/anonymous_enable=NO/g' /etc/vsftpd/vsftpd.conf
mod_check
sed -i 's/^#chroot_local_user=YES/chroot_local_user=YES/g' /etc/vsftpd/vsftpd.conf
mod_check
echo -e "guest_enable=YES\nguest_username=virftp\nvirtual_use_local_privs=YES\nuser_config_dir=/etc/vsftpd/vsftpd_user_conf" >> /etc/vsftpd/vsftpd.conf
mod_check
/etc/init.d/vsftpd start
mod_check
echo -e "\033[32mvsftpd successful.\033[0m"
echo -e "use\033[32m yum install -y lftp\033[0m install ftp-client \nuse \033[32mlftp test@$S_IP \033[0mlogin ftp server "
}
################################ Samba模块 #################################
mod_samba(){
read -p "input a directory(like /tmp/sambauser) :" Dir
[ -d $Dir ] || mkdir -p $Dir
mod_check
##关闭防火墙##
mod_firewall
##安装需要的包##
for p in samba samba-client
do
mod_yum $p
done
##服务器IP##
S_IP=`ifconfig |grep -A 1 eth0 |grep inet|awk -F":" '{print $2}'|awk -F" " '{print $1}' `
##编辑配置文件##
user=smbuser
passwd=123456
echo -e "[$user]\ncomment = share for users\npath = $Dir\nbrowseable = yes\nwriteable = yes\npublic = no" >> /etc/samba/smb.conf
mod_check
##创建用户和文件##
cat /etc/passwd |grep -q $user || useradd -M $user
mod_check
##直接给padedit密码
pdbedit -a smbuser -t << EOF
$passwd
$passwd
EOF
mod_check
/etc/init.d/smb start
mod_check
echo -e "\033[32msamba successful\033[0m"
mod_check
echo -e "if client is linux,use \033[32mmount -t cifs -o username=$user password=$passwd\033[0m"
mod_check
echo -e "if client is windows,use \033[32m\\ \\$S_IP username=$user password=$passwd\033[0m"
mod_check
}
############################## 脚本主程序 ####################################
echo "Please select in 1.nfs 2.ftp 3.samba"
select se in nfs ftp samba
do
case $se in
nfs)
if ps aux|grep -q nfs
then
echo -e "\033[32mnfs running.\033[0m"
exit 1
fi
echo "You select the nfs,install beginning..."
mod_nfs
break
;;
ftp)
if netstat -tnlp|grep -q vsftpd
then
echo -e "\033[32mvsftpd running.\033[0m"
exit 1
fi
echo "You select the ftp,install beginning..."
mod_ftp
break
;;
samba)
if netstat -tnlp|grep -q smbd
then
echo -e "\033[32msamba running.\033[0m"
exit 1
fi
echo "You select the samba,install beginning..."
mod_samba
break
;;
*)
echo "Please input a number(1-3)"
;;
esac
done
0
自己后来使用中发现了两处错误:
1.mod_firewall(防火墙模块)的iptables-save > /etc/sysconfig/iptables_`date+%s`行改为
iptables-save > /etc/sysconfig/iptables_`date +%s`
2.在安装nfs时有一个bug,就是ps aux|grep nfs后如果只有
root 3378 0.0 0.0 5984 752 pts/0 S+ 07:53 0:00 grep nfs
只有这行时,还是提示nfs running。所以将主程序nfs部分
ps aux|grep nfs 改为 ps aux|grep "\[nfsd\]"
1.mod_firewall(防火墙模块)的iptables-save > /etc/sysconfig/iptables_`date+%s`行改为
iptables-save > /etc/sysconfig/iptables_`date +%s`
2.在安装nfs时有一个bug,就是ps aux|grep nfs后如果只有
root 3378 0.0 0.0 5984 752 pts/0 S+ 07:53 0:00 grep nfs
只有这行时,还是提示nfs running。所以将主程序nfs部分
ps aux|grep nfs 改为 ps aux|grep "\[nfsd\]"
编辑回复