Archive for 2011年8月24日


一、安装vsftpd及相关组件:
yum -y install vsftpd* pam* db4*

二、修改FTP相关帐户:

1、vsftpd服务的宿主用户
useradd vsftpd -s /sbin/nologin
默认的vsftpd的服务宿主用户是root,但是这不符合安全性的需要。这里建立名字为vsftpd的用户,用他来作为支持vsftpd的服务宿主用户。由于该用户仅用来支持vsftpd服务用,因此没有许可他登陆系统的必要,并设定他为不能登陆系统的用户。

2、vsftpd的虚拟宿主用户

useradd virtual -d /var/www/html -s /sbin/nologin
chown -R virtual:virtual /var/www/html

vsftpd的虚拟用户并不是系统用户,也就是说这些FTP的用户在系统中是不存在的。他们的总体权限其实是集中寄托在一个在系统中的某一个用户身上的,所谓vsftpd的虚拟宿主用户,就是这样一个支持着所有虚拟用户的宿主用户。由于他支撑了FTP的所有虚拟的用户,那么他本身的权限将会影响着这些虚拟的用户,因此,处于安全性的考虑,也要非分注意对该用户的权限的控制,该用户也绝对没有登陆系统的必要,这里也设定他为不能登陆系统的用户。

三、vsftpd.conf基本配置:
vim /etc/vsftpd/vsftpd.conf

# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd’s
# capabilities.
#
# Allow anonymous FTP? (Beware – allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd’s)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages – messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using “root” for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING – changing this filename affects /etc/logrotate.d/vsftpd.log
#xferlog_file=/var/log/xferlog
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command “SIZE /big/file” in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the “-R” option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as “ncftp” and “mirror” assume
# the presence of the “-R” option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When “listen” directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
listen_port=66880
pasv_min_port=30000
pasv_max_port=35000

#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES

pam_service_name=vsftpd.vu
#pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

chroot_local_user=YES
guest_enable=YES
guest_username=virtual

virtual_use_local_privs=YES
#reverse_lookup_enable=NO
user_config_dir=/etc/vsftpd/vsftpd_user_conf

四、生成vsftpd虚拟用户数据库文件:

1、建立虚拟用户名单文件:
vim /etc/vsftpd/ftpuser.txt
内容如下:
ftp1
1234
ftp2
5678
格式很简单:“一行用户名,一行密码!”。

2、生成虚拟用户数据文件:

db_load -T -t hash -f /etc/vsftpd/ftpuser.txt /etc/vsftpd/vsftpd_login.db
chmod 600 /etc/vsftpd/vsftpd_login.db

五、配置PAM验证文件:
vim /etc/pam.d/vsftpd.vu

将以下内容加入到文件最前面(在后面加入无效):
32位系统:

auth required /lib/security/pam_userdb.so db=/etc/vsftpd_login
account required /lib/security/pam_userdb.so db=/etc/vsftpd_login

64位系统:

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

上一步建立的数据库 vsftpd_login 在此处被使用,建立的虚拟用户将采用PAM进行验证,这是通过/etc/vsftpd/vsftpd.conf文件中的语句pam_service_name=vsftpd.vu来启用的。

六、vsftpd虚拟用户的独立配置:

mkdir -p /etc/vsftpd/vsftpd_user_conf
vim /etc/vsftpd/vsftpd_user_conf/jtxm

配置如下:

anon_world_readable_only=NO
write_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_root=/var/www/html/jtxm

七、vsftpd服务器之间的站点对传:
有时候可能需要开启vsftpd服务器之间的站点对传功能,只需在主配置文件 /etc/vsftpd/vsftpd.conf 里加入如下参数即可:

pasv_promiscuous=YES
port_promiscuous=YES

说明:
port_promiscuous=YES|NO
默认值为NO。为YES时,取消PORT安全检查。该检查确保外出的数据只能连接到客户端上。小心打开此选项。

pasv_promiscuous=YES|NO
默认值为NO。为YES时,将关闭PASV模式的安全检查。该检查确保数据连接和控制连接是来自同一个IP地址。小心打开此选项。此选项唯一合理的用法是存在于由安全隧道方案构成的组织中。
由于取消了数据包的安全检查,允许数据流向非客户端,所以站点对传成功。

配置修改完成后,重启vsftpd服务生效:
/etc/init.d/vsftpd restart

总结:以上配置是经过我多次验证的,如果出现问题,请从以下几方面检查:
1、文件权限和文件属主问题;
2、防火墙iptables没开放相关的端口;
3、SELinux导致的权限问题,建议先关闭SELinux再配置ftp,之后再开启到permissive模式。或者运行这条命令:setsebool -P ftp_home_dir=1

#!/bin/sh
# desc: setup linux system security
# author:coralzd
# powered by http://www.freebsdsystem.org
# version 0.1.2 written by 2011.05.03
#account setup

passwd -l xfs
passwd -l news
passwd -l nscd
passwd -l dbus
passwd -l vcsa
passwd -l games
passwd -l nobody
passwd -l avahi
passwd -l haldaemon
passwd -l gopher
passwd -l ftp
passwd -l mailnull
passwd -l pcap
passwd -l mail
passwd -l shutdown
passwd -l halt
passwd -l uucp
passwd -l operator
passwd -l sync
passwd -l adm
passwd -l lp

# chattr /etc/passwd /etc/shadow
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow

# add continue input failure 3 ,passwd unlock time 5 minite
sed -i ‘s#auth required pam_env.so#auth required pam_env.sonauth required pam_tally.so onerr=fail deny=3 unlock_time=300nauth required /lib/security/$ISA/pam_tally.so onerr=fail deny=3 unlock_time=300#’ /etc/pam.d/system-auth
# system timeout 5 minite auto logout
echo “TMOUT=300” >>/etc/profile

# will system save history command list to 10
sed -i “s/HISTSIZE=1000/HISTSIZE=10/” /etc/profile

# enable /etc/profile go!
source /etc/profile

# add syncookie enable /etc/sysctl.conf
echo “net.ipv4.tcp_syncookies=1” >> /etc/sysctl.conf

sysctl -p # exec sysctl.conf enable
# optimizer sshd_config

sed -i “s/#MaxAuthTries 6/MaxAuthTries 6/” /etc/ssh/sshd_config
sed -i “s/#UseDNS yes/UseDNS no/” /etc/ssh/sshd_config

# limit chmod important commands
chmod 700 /bin/ping
chmod 700 /usr/bin/finger
chmod 700 /usr/bin/who
chmod 700 /usr/bin/w
chmod 700 /usr/bin/locate
chmod 700 /usr/bin/whereis
chmod 700 /sbin/ifconfig
chmod 700 /usr/bin/pico
chmod 700 /bin/vi
chmod 700 /usr/bin/which
chmod 700 /usr/bin/gcc
chmod 700 /usr/bin/make
chmod 700 /bin/rpm

# history security

chattr +a /root/.bash_history
chattr +i /root/.bash_history

# write important command md5
cat > list << “EOF” &&
/bin/ping
/bin/finger
/usr/bin/who
/usr/bin/w
/usr/bin/locate
/usr/bin/whereis
/sbin/ifconfig
/bin/pico
/bin/vi
/usr/bin/vim
/usr/bin/which
/usr/bin/gcc
/usr/bin/make
/bin/rpm
EOF

for i in `cat list`
do
if [ ! -x $i ];then
echo “$i not found,no md5sum!”
else
md5sum $i >> /var/log/`hostname`.log
fi
done
rm -f list

#!/bin/sh
# desc: lsm03624 modified by http://www.webnginx.com
#——————-cut begin——————————————-
#welcome
cat << EOF
+————————————————————–+
| === Welcome to Centos System init === |
+————–http://www.linuxtone.org————————+
+———————-Author:NetSeek————————–+
EOF
#disable ipv6
cat << EOF
+————————————————————–+
| === Welcome to Disable IPV6 === |
+————————————————————–+
EOF
echo “alias net-pf-10 off” >> /etc/modprobe.conf
echo “alias ipv6 off” >> /etc/modprobe.conf
/sbin/chkconfig –level 35 ip6tables off
echo “ipv6 is disabled!”

#disable selinux
sed -i ‘/SELINUX/s/enforcing/disabled/’ /etc/selinux/config
echo “selinux is disabled,you must reboot!”

#vim
sed -i “8 s/^/alias vi=’vim’/” /root/.bashrc
echo ‘syntax on’ > /root/.vimrc

#zh_cn
sed -i -e ‘s/^LANG=.*/LANG=”zh_CN.UTF-8″/’ /etc/sysconfig/i18n
# configure file max to 52100
echo “* soft nofile 52100
* hard nofile 52100” >> /etc/security/limits.conf

#tunoff services
#——————————————————————————–
cat << EOF
+————————————————————–+
| === Welcome to Tunoff services === |
+————————————————————–+
EOF
#———————————————————————————
for i in `ls /etc/rc3.d/S*`
do
CURSRV=`echo $i|cut -c 15-`

echo $CURSRV
case $CURSRV in
cpuspeed | crond | irqbalance | microcode_ctl | mysqld | network | nginx | php-fpm | sendmail | sshd | syslog )
#这个启动的系统服务根据具体的应用情况设置,其中network、sshd、syslog是三项必须要启动的系统服务!
echo “Base services, Skip!”
;;
*)
echo “change $CURSRV to off”
chkconfig –level 235 $CURSRV off
service $CURSRV stop
;;
esac
done

rm -rf /etc/sysctl.conf
echo “net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 134217728
net.ipv4.ip_local_port_range = 1024 65536
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_fin_timeout = 3
net.ipv4.tcp_tw_recycle = 1
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save = 1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
vm.swappiness = 6” >> /etc/sysctl.conf
echo “optimizited kernel configure was done!”

VMware + RHEL6

复制给别人*.vmdk文件后,发现网卡不能用了。重启网络服务,报以下错误:
Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization. [FAILED]

复制虚拟机MAC会重新生成,但是操作系统的MAC却写在ifcfg-ethx里,造成了不一致,所以不能启动网络接口,在RHEL5里可以使用kudzu或者注释网卡配置文件的MAC字段来解决这个问题。但是在RHEL6里,kudzu已经被hal服务取代了。虽然lspci能够正常认到网卡,但是却无法使用/etc/init.d/network restart来启动服务。尝试注释ifcfg-eth0的MAC字段,还是报错。查看了下udev的规则,发现了问题的所在。

方案一:
rm -fr /etc/udev/rules.d/70-persistent-net.rules
reboot

方案二:
ifconfig eth1 up
启动新生成的网卡也行