shell脚本一键安装ftp及配置
2017-09-14 10:46
591 查看
#!/bin/bash Stack=$1 if [ "${Stack}" = "" ]; then Stack="install" else Stack=$1 fi install_vsftp() { echo "#######################" echo -e "\033[33mUsage: $0 {install|add|uninstall}\033[0m" echo -e "\033[33msh $0 (default:install)\033[0m" echo -e "\033[33msh $0 add (Add FTP user)\033[0m" echo -e "\033[33msh $0 uninstall (Uninstall FTP)\033[0m" echo "#######################" A=`head -c 500 /dev/urandom | tr -dc a-zA-Z | tr [a-z] [A-Z]|head -c 1` B=`head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 6` C=`echo $RANDOM|cut -c 2` rpm -q vsftpd if [ "$?" -eq "0" ]; then echo "You have to install VSFTPD!" else netstat -ntulp |grep -w 21 if [ "$?" -eq "0" ]; then echo "Other FTP is already installed" else read -p "The FTP access directory(default:/home): " directory if [ "${directory}" != "" ]; then directorys="${directory}" else directorys="/home" fi read -p "Please enter the FTP user: " ftp_user read -p "Enter the FTP password(default:$A$B$C): " ftp_pass if [ "${ftp_pass}" != "" ]; then ftp_passa="${ftp_pass}" else ftp_passa="$A$B$C" fi yum -y install vsftpd if [ "$?" -eq "0" ]; then if [ -d ${directorys} ]; then chmod -R 777 ${directorys} fi useradd -d ${directorys} -g ftp -s /sbin/nologin ${ftp_user} echo "${ftp_passa}" | passwd --stdin ${ftp_user} > /dev/null sed -i 's/^anonymous_enable=YES/anonymous_enable=NO/g' /etc/vsftpd/vsftpd.conf sed -i 's/^#chroot_local_user=YES/chroot_local_user=YES/g' /etc/vsftpd/vsftpd.conf sed -i 's/^#chroot_list_enable=YES/chroot_list_enable=YES/g' /etc/vsftpd/vsftpd.conf echo "userdel ${ftp_user}" >> /etc/vsftpd/user_list.sh echo "" > /etc/vsftpd/chroot_list chkconfig vsftpd on service vsftpd restart echo "###################################" echo "FTP user:${ftp_user}" echo "Ftp password:${ftp_passa}" echo "The FTP directory:${directorys}" echo "-----------------------------------" else echo "VSFTPD installation failed!" fi fi fi } add_ftp() { A=`head -c 500 /dev/urandom | tr -dc a-zA-Z | tr [a-z] [A-Z]|head -c 1` B=`head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 6` C=`echo $RANDOM|cut -c 2` read -p "The FTP access directory(Such as:/home): " directory if [ "${directory}" != "" ]; then directorys="${directory}" else directorys="/home" fi read -p "Please enter the FTP user: " ftp_user read -p "Enter the FTP password(default:$A$B$C): " ftp_pass if [ -d ${directorys} ]; then chmod -R 777 ${directorys} fi useradd -d ${directorys} -g ftp -s /sbin/nologin ${ftp_user} if [ "${ftp_pass}" != "" ]; then ftp_passa="${ftp_pass}" else ftp_passa="$A$B$C" fi echo "${ftp_passa}" | passwd --stdin ${ftp_user} > /dev/null echo "userdel ${ftp_user}" >> /etc/vsftpd/user_list.sh if [ -d ${directorys} ]; then chmod -R 777 ${directorys} fi echo "###################################" echo "FTP user:${ftp_user}" echo "Ftp password:${ftp_passa}" echo "The FTP directory:${directorys}" echo "-----------------------------------" } uninstall_ftp() { yum -y remove vsftpd* sh /etc/vsftpd/user_list.sh echo "" > /etc/vsftpd/user_list.sh } case "${Stack}" in install) install_vsftp ;; add) add_ftp ;; uninstall) uninstall_ftp ;; *) echo "Usage: $0 {install|add|uninstall}" ;; esac
将上面代码复制粘贴到一个.sh文件保存,将文件复复制到虚拟及中.
执行已下操作:
1.安装 (命令执行:sh xxx.sh)
2.添加ftp用户 (命令执行:sh xxx.sh add)
3.卸载vsftpd (命令执行:sh xxx.sh uninstall)
执行完以上步骤并不能完全实现上传操作
需要配置pasv设置
在/etc/vsftpd/vsftpd.conf
最后部分添加
pasv_enable=YES pasv_max_port=30010 pasv_min_port=30000
防火墙配置:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 30000:30010 -j ACCEPT
ps:如果用windows客户端连接出现”500 OOPS: cannot change directory:/home/xxx”
修改selinux
getsebool -a | grep ftp
执行上面命令,再返回的结果看到两行都是off,代表,没有开启外网的访问
.... ftpd_full_access off .... .... ftp_home_dir off
只要把上面都变成on就行
执行
setsebool -P ftpd_full_access 1 setsebool -P ftp_home_dir 1
重启vsftpd
service vsftpd restart
相关文章推荐
- shell脚本一键安装ftp及配置
- 使用shell脚本一键安装zabbix并配置客户端的自动发现选项
- shell脚本一键安装LNMP(liunx+nginx+mysql+php)环境
- shell脚本一键安装lamp(待测试)
- CentOS下nginx一键安装shell脚本
- MySql一键安装shell脚本
- [PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)
- Centos5.8上面用Shell脚本一键安装mysql5.5.25源码包
- shell 脚本一键安装mysql数据库(已打包成图形化安装)
- [置顶] 编写shell脚本以及利用函数实现批量安装Linux服务配置
- Shell脚本一键安装LNMP环境
- shell脚本 一键 安装 php7
- shell脚本实现ipa一键安装(itms-services协议)
- Linux-shell脚本-mysql一键安装
- 一键安装MySQL 主从复制SHELL脚本
- Oracle 11g R2 for Linux 自动安装配置---完整shell脚本
- Centos 6.4上面用Shell脚本一键安装mysql 5.6.15
- 安装完Ubuntu后通过shell脚本一键安装软件
- shell脚本一键安装MySQL5.5
- flink集群一键安装脚本 -- java运行环境配置