Shell Script Build FTP server (VSFTPD) _linux shell

Source: Internet
Author: User
Tags chmod ftp mkdir linux mint

Various points of knowledge are written in the notes.

Only need sudo $PATH/ftpsetup.sh, default to test/test for user name password login, after the root directory read-only, subdirectory writable writable.

Test OK on Ubuntu 13 and Linux Mint 15.

Copy Code code as follows:

#!/bin/bash
# by Liuhx 2013-nov-04.
# Script to set up FTP environment. The FTP root directory is read-only, and the writable directory under it is writable

# The following four items can be customized
# FTP User Name
Username= "Test"
# FTP Password
password= "Test"
# FTP root directory, do not add at the end
Ftp_dir= "$HOME/ftp"
# Directory Name of writable directory
Writable= "Writable"


# If no sudo is added, prompt for error and exit
if ["x$ (id-u)"!= x0]; Then
echo "Error:please run this script with ' sudo '."
Exit 1
Fi

# Core Tools, VSFTPD. -Y is answer yes to all prompts
sudo apt-get-y install vsftpd
# Db-util is the tool used to generate the user list database
sudo apt-get-y install Db-util

# The following steps refer to HTTPS://HELP.UBUNTU.COM/COMMUNITY/VSFTPD
# Create a user name and password database, in singular behavior username, double action password record
Cd/tmp
printf "$userName \n$password\n" > Vusers.txt
Db_load-t-T hash-f vusers.txt vsftpd-virtual-user.db
sudo cp-f vsftpd-virtual-user.db/etc/
Cd/etc
chmod vsftpd-virtual-user.db
if [!-e Vsftpd.conf.old]; Then
sudo cp-f vsftpd.conf vsftpd.conf.old
Fi

# Create Pam file. Bash the here-document, directly output these content overwrite the original file
(sudo cat <<eof
Auth Required pam_userdb.so Db=/etc/vsftpd-virtual-user
Account Required Pam_userdb.so Db=/etc/vsftpd-virtual-user
Session Required Pam_loginuid.so
Eof
) > Pam.d/vsftpd.virtual

# get current username, can't use whoami or $logname, otherwise get root
Owner= ' Who am i| awk ' {print '} '

# Create a vsftpd configuration file.
(sudo cat <<eof
Listen=yes
Anonymous_enable=no
Local_enable=yes
Virtual_use_local_privs=yes
Write_enable=yes
local_umask=000
Dirmessage_enable=yes
Use_localtime=yes
Xferlog_enable=yes
Connect_from_port_20=yes
Chroot_local_user=yes
Hide_ids=yes
Secure_chroot_dir=/var/run/vsftpd/empty
Pam_service_name=vsftpd.virtual
Guest_enable=yes
user_sub_token= $USER
Rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
Rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
Eof
) > Vsftpd.conf
sudo echo "local_root= $ftp _dir" >> vsftpd.conf
# Virtual users need to map to the local user, set to themselves, avoid permissions issues, but also make their own FTP root directory is not writable
sudo echo "guest_username= $owner" >> vsftpd.conf


# set up each virtual user can browse only its root and subdirectory (otherwise access to the disk root directory),
# This will be required the root directory is not writable, so create a writable subdirectory
mkdir "$ftp _dir"
mkdir "$ftp _dir/$writable"
sudo chmod a-w "$ftp _dir"
sudo chown-r $owner: $owner $ftp _dir

SUDO/ETC/INIT.D/VSFTPD restart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.