#!/bin/bash ##如果变量未设置用户则配置默认用户为admin if [ -z "$SFTP_USER" ]; then export SFTP_USER='admin' fi ##如果变量未设置用户密码,则随机生成用户密码,可从日志中查看 if [ -z "$SFTP_PASS" ]; then export SFTP_PASS=`cat /dev/urandom | tr -dc A-Z-a-z-0-9 | head -c${1:-16}` fi ##如果变量sftp端口,则默认22端口 if [ -z "$SFTP_PORT" ]; then export SFTP_PORT='22' fi ##如果变量未设置sftp根目录,则默认/home if [ -z "$SFTP_DIR" ]; then export SFTP_DIR='/home' fi ##创建ftp用户目录 mkdir -p "$SFTP_DIR" ##安全配置 sed -i "/AddressFamily/iPort $SFTP_PORT" /etc/ssh/sshd_config echo "Match User $SFTP_USER" >>/etc/ssh/sshd_config echo " ChrootDirectory $SFTP_DIR" >>/etc/ssh/sshd_config echo " PasswordAuthentication yes" >>/etc/ssh/sshd_config echo " X11Forwarding no" >>/etc/ssh/sshd_config echo " AllowTcpForwarding no" >>/etc/ssh/sshd_config echo " ForceCommand internal-sftp" >>/etc/ssh/sshd_config ##创建sftp账户 useradd -m -d $SFTP_DIR/${SFTP_USER} -s /sbin/nologin $SFTP_USER echo $SFTP_PASS | passwd --stdin $SFTP_USER ##获取日志文件 export LOG_FILE='/var/log/secure' # stdout server info: if [ -z $LOG_STDOUT ]; then cat << EOF | tee -a $LOG_FILE ******************************************************************************** * * * * Docker image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/sftp:zy5447 * * * * ******************************************************************************** SERVER SETTINGS --------------- · SFTP User: $SFTP_USER · SFTP Password: $SFTP_PASS · SFTP Dir: $SFTP_DIR/${SFTP_USER} EOF fi # Run ssh-server: ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N '' &>/dev/null /usr/sbin/sshd tail -n+1 -F "$LOG_FILE"