Installing and configuring the VSFTPd FTP server on Centos 7. Local users
A series of articles on setting up a VSFTPd FTP server on Centos 7
- Installing and configuring the VSFTPd FTP server on Centos 7. Local users
- FTP server VSFTPd and virtual users MySQL on CentOS 7, Web-admin for VSFTP
FTP Server Installation
Installing software:
[[email protected]]# yum install vsftpd nano net-tools -y
Create a directory where user directories will be and set access rights
[[email protected]]# mkdir /home/vsftpd
[[email protected]]# chmod 0777 /home/vsftpd
Save the default config
[[email protected]]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_backup
We write our config
[[email protected]]# nano /etc/vsftpd/vsftpd.conf
# Запуск сервера в режиме службы
listen=YES
# Работа в фоновом режиме
background=YES
# Имя pam сервиса для vsftpd
pam_service_name=vsftpd
# Входящие соединения контроллируются через tcp_wrappers
tcp_wrappers=YES
# Запрещает подключение анонимных пользователей
anonymous_enable=NO
# Каталог, куда будут попадать анонимные пользователи, если они разрешены
#anon_root=/ftp
# Разрешает вход для локальных пользователей
local_enable=YES
# Разрешены команды на запись и изменение
write_enable=YES
# Указывает исходящим с сервера соединениям использовать 20-й порт
connect_from_port_20=YES
# Логирование всех действий на сервере
xferlog_enable=YES
# Путь к лог-файлу
xferlog_file=/var/log/vsftpd.log
# Включение специальных ftp команд, некоторые клиенты без этого могут зависать
async_abor_enable=YES
# Локальные пользователи по-умолчанию не могут выходить за пределы своего домашнего каталога
chroot_local_user=YES
# Разрешить список пользователей, которые могут выходить за пределы домашнего каталога
chroot_list_enable=YES
# Список пользователей, которым разрешен выход из домашнего каталога
chroot_list_file=/etc/vsftpd/chroot_list
# Разрешить запись в корень chroot каталога пользователя
allow_writeable_chroot=YES
# Контроль доступа к серверу через отдельный список пользователей
userlist_enable=YES
# Файл со списками разрешенных к подключению пользователей
userlist_file=/etc/vsftpd/user_list
# Пользователь будет отклонен, если его нет в user_list
userlist_deny=NO
# Директория с настройками пользователей
user_config_dir=/etc/vsftpd/users
# Показывать файлы, начинающиеся с точки
force_dot_files=YES
# Маска прав доступа к создаваемым файлам
local_umask=022
# Порты для пассивного режима работы
pasv_min_port=49000
pasv_max_port=55000
Add user
[[email protected]]# useradd -s /sbin/nologin ftpuser
[[email protected]]# passwd ftpuser
fptpassword
Create a folder where separate user configs will be
[[email protected]]# mkdir /etc/vsftpd/users
[[email protected]]# touch /etc/vsftpd/users/ftpuser
Set the home ftp directory in the config
[[email protected]]# echo 'local_root=/home/vsftpd/ftpuser/' >> /etc/vsftpd/users/ftpuser
Create user directory and set owner
[[email protected]]# mkdir /home/vsftpd/ftpuser
[[email protected]]# chown ftpuser:ftpuser /home/vsftpd/ftpuser
Create a file that will list the list of users who are allowed to leave the home directory. And add the root user to it
[[email protected]]# touch /etc/vsftpd/chroot_list
[[email protected]]# echo 'root' >> /etc/vsftpd/chroot_list
Let’s create a file that will list the list of users who are allowed to connect to the FTP server. Add root and ftpuser users to it
[[email protected]]# touch /etc/vsftpd/user_list
[[email protected]]# echo 'root' >> /etc/vsftpd/user_list && echo 'ftpuser' >> /etc/vsftpd/user_list
Create a file where logs will be written and set the rights to it
[[email protected]]# touch /var/log/vsftpd.log && chmod 600 /var/log/vsftpd.log
Add the vsftpd service to startup, start it, check the status
[[email protected]]# systemctl enable vsftpd
[[email protected]]# systemctl start vsftpd
[[email protected]]# systemctl status vsftpd
See if the process has appeared
[[email protected]]# netstat -tulnp | grep vsftpd
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 13195/vsftpd
Add rules to the firewall: open ports 21, 49000-55000
[[email protected]]# firewall-cmd --permanent --add-port=21/tcp
[[email protected]]# firewall-cmd --permanent --add-port=49000-55000/tcp
[[email protected]]# firewall-cmd --reload
Disable Selinux, reboot
[[email protected]]# setenforce 0
[[email protected]]# nano /etc/selinux/config
SELINUX=disabled
[[email protected]]# reboot
Instructions for adding new users
Add a user to the system
[[email protected]]# useradd -s /sbin/nologin testuser
[[email protected]]# passwd testuser
testpassword
Create user directory and set owner
[[email protected]]# mkdir /home/vsftpd/testuser
[[email protected]]# chown test:test /home/vsftpd/testuser
Create a folder where separate user configs will be
[[email protected]]# touch /etc/vsftpd/users/testuser
We set the home ftp directory in the config
[[email protected]]# echo 'local_root=/home/vsftpd/testuser/' >> /etc/vsftpd/users/testuser
Set allowed users
[[email protected]]# echo 'testuser' >> /etc/vsftpd/user_list
Restart vsftpd
[[email protected]]# systemctl restart vsftpd
UPD 11/27/2018 Bash script for adding users
Create bash script add_ftp_user.sh
[[email protected]]# nano /home/add_ftp_user.sh
#!/bin/bash
NAME=$1
PASS=$2
echo "USAGE: add_ftp_user.sh [username] [password]"
# проверка входных параметров
if [ -z "$NAME" ]; then
echo "Error: username is not set"
exit
fi
if [ -z "$PASS" ]; then
echo "Error: password not set"
exit
fi
# создаем системных пользователей
echo "Creating user: $NAME"
echo "With password: $PASS"
useradd -s /sbin/nologin -p `openssl passwd -1 $PASS` $NAME
# сохраняем данные в файл /etc/vsftpd/new_ftp_users_list
echo "user: $NAME, pass: $PASS" >> /etc/vsftpd/new_ftp_users_list
# создаем ftp-директорию пользователя
mkdir /home/vsftpd/$NAME
# назначаем владельца
chown $NAME:$NAME /home/vsftpd/$NAME
# создаем пустой конфигурационный файл
touch /etc/vsftpd/users/$NAME
# прописываем домашний каталог
echo "local_root=/home/vsftpd/$NAME/" >> /etc/vsftpd/users/$NAME
# добавляем пользователя в список разрешенных для подключения
echo "$NAME" >> /etc/vsftpd/user_list
# задаем права каталога пользователя
chmod 0777 /home/vsftpd/$NAME
# перезапускаем службу vsftp
systemctl restart vsftpd
Making it executable
[[email protected]]# chmod +x /home/add_ftp_user.sh
now, to add a new FTP user, you need to run the command
[[email protected]]# сd /home
[[email protected]]# ./add_ftp_user.sh %user% %pass%
where% user% – login% pass% – password
UPD 09/03/2019 – authorization does not work, error 530
Authorization does not work in vsftpd:
530 Login incorrect
Decision:This error occurs due to the fact that in the file /etc/pam.d/vsftpd there is a line:
[[email protected]]# egrep -v "^#|^$" /etc/pam.d/vsftpd
...
auth required pam_shells.so
...
it means that only users with access to shells should be allowed access And we added the user just with the parameter: -s / sbin / nologin
[[email protected]]# useradd -s /sbin/nologin ftpuser
Comments this line: auth required pam_shells.so and restart vsftpd
[[email protected]]# systemctl restart vsftpd