CentOS 7下安装配置proftpd搭建ftp服务器

proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone、xinetd模式运行等。ProFTP已经成为继Wu-FTP之后最为流行的FTP服务器软件,越来越多的站点选用它构筑安全高效的FTP站点,ProFTP配置方便,并有MySQL和Quota模块可供选择,利用它们的完美结合可以实现非系统账号的管理和用户磁盘的限制。<摘抄百度百科>

本章通过下载源码的方式安装,可以到官网下载最新版本:http://www.proftpd.org/

1、首先安装lrzsz方便拷贝文件到要搭建服务器的linux路径下

yum install lrzsz

2、上传文件解压

将下载完成的proftpd-1.3.6.tar.gz文件上传到指定路径,使用tar zxvf proftpd-1.3.6.tar.gz解压

3、由于使用源码安装,所以需要安装gcc编译环境

yum install gcc gcc-c++ autoconf automake

3、配置并制定安装和配置文件路径

./configure --prefix=/usr/local/proftpd --sysconfdir=/usr/local/proftpd

4、安装

make&make install

5、修改配置文件,限制匿名用户可以上传下载,但是不能删除(本章暂不详细描述权限控制部分内容)

vi /usr/local/proftpd/proftpd.conf
# This is a basic ProFTPD configuration file (rename it to 
# 'proftpd.conf' for actual use.  It establishes a single server
# and a single anonymous login.  It assumes that you have a user/group
# "nobody" and "ftp" for normal operation and anon.

ServerName                      "Welcome to FTP Server"
ServerType                      standalone
DefaultServer                   on

# Port 21 is the standard FTP port.
Port                            21

# Don't use IPv6 support by default.
UseIPv6                         off

# Umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
Umask                           022

# To prevent DoS attacks, set the maximum number of child processes
# to 30.  If you need to allow more than 30 concurrent connections
# at once, simply increase this value.  Note that this ONLY works
# in standalone mode, in inetd mode you should use an inetd server
# that allows you to limit maximum number of processes per service
# (such as xinetd).
MaxInstances                    30

# Set the user and group under which the server will run.
User                            nobody
Group                           nobody

# To cause every FTP user to be "jailed" (chrooted) into their home
# directory, uncomment this line.
#DefaultRoot ~

# Normally, we want files to be overwriteable.
AllowOverwrite          on

IdentLookups            off
UseReverseDNS           off

# Bar use of SITE CHMOD by default
<Limit SITE_CHMOD>
  AllowAll
</Limit>

# A basic anonymous configuration, no upload directories.  If you do not
# want anonymous users, simply delete this entire <Anonymous> section.
<Anonymous /home>
  User                          ftp
  Group                         ftp

  # We want clients to be able to login with "anonymous" as well as "ftp"
  UserAlias                     anonymous ftp

  # Limit the maximum number of anonymous logins
  MaxClients                    50

  # We want 'welcome.msg' displayed at login, and '.message' displayed
  # in each newly chdired directory.
  DisplayLogin                  welcome.msg
  DisplayChdir                  .message

  # Limit WRITE everywhere in the anonymous chroot
  <Limit DELE RMD>
    DenyAll
  </Limit>
</Anonymous>

6、关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service

7、启动服务器

/usr/local/proftpd/sbin/proftpd

查看进程是否已经启动

[root@localhost ~]# ps -ef |grep proftpd
nobody    1140    1  0 12:13 ?        00:00:00 proftpd: (accepting connections)
root      3182  2270  0 12:50 pts/0    00:00:00 grep --color=auto proftpd  

8、添加到启动项避免每次重启后都需要手动启动

vi /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/proftpd/sbin/proftpd

由于在CentOS7中/etc/rc.d/rc.local的权限被降低了,所以需要加上可执行的权限:

chmod +x /etc/rc.d/rc.local

Centos硬盘挂载

查看系统有几块硬盘

fdisk -l

找到需要格式化的硬盘

mkfs.xfs -f /dev/vdb

挂载硬盘

新建一个挂载点

mkdir /data

执行以下命令进行挂载:

mount -t xfs /dev/vdb /data/

最后执行以下命令查看挂载是否成功

df -h
/dev/vda1 50G 18G 30G 38% /
/dev/vdb 50G 33M 50G 1% /data

CentOS 7安装Oracle Express

Oracle官方文档说交换分区的大小要设2G或者内存的两倍,但是发现512M内存设置1G还是不给装,所以创建2G的交换分区

dd if=/dev/zero of=/swapfile bs=2048 count=1048576
mkswap /swapfile
swapon /swapfile
echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
swapon -a

安装完后会自动创建oracle用户和dba组之类的,但是自动创建的不会在/home下面创建用户目录,于是手动创建oracle用户,并加-m参数

useradd -m oracle

下载Oracle Express并解压

开始安装

cd Disk1
rpm -ivh oracle-xe-11.2.0-1.0.x86_64.rpm

初始化配置

/etc/init.d/oracle-xe configure

配置环境变量/etc/profile

#Oracle
TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
ORACLE_HOSTNAME=centos; export ORACLE_HOSTNAME
ORACLE_UNQNAME=XE; export ORACLE_UNQNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/xe; export ORACLE_HOME
ORACLE_SID=XE; export ORACLE_SID
PATH=/usr/sbin:$PATH; export PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH

其中ORACLE_HOSTNAME、ORACLE_UNQNAME、ORACLE_HOME、ORACLE_SID要作对应修改,不然会出错

设置环境变量生效,或重新登录

source /etcprofile

切换到oracle用户,如无意外,能连上了

[oracle@centos ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Wed Dec 20 15:02:37 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL>

CentOS 配置部署 NFS

对于服务器的文件共享,可以部署一台文件共享服务器,通过 NFS 去挂载使用。

安装

CentOS 6.x

下载安装

yum -y install rpcbind nfs

配置开机启动

chkconfig rpcbind on
chkconfig nfs on

Centos 7.x

下载安装

yum -y install rpcbind nfs-utils

配置开机启动

systemctl enable nfs-server.service
systemctl enable rpcbind.service

服务端配置

配置

在服务端,编辑配置文件 /etc/exports,添加如下行

/home/uploads 192.168.221.101(rw,all_squash,anonuid=0,anongid=0)

配置说明:

192.168.221.101 客户端 ip
rw 读写 ro 只读
all_squash 无论客户端以何用户连接服务端,对服务端而言均为匿名用户
anonuid 匿名用户的 uid
anongid 匿名用户的 gid

查看共享目录的 uid 和 gid,使用命令:

ls -nd /home/nfs

uid 和 gid 最好为客户端和服务端都存在的 uid 和 gid 值。

可用的配置说明列表:

未分类

exportfs -r # 生效

若在服务启动后修改了 /etc/exports 配置文件,执行该命令后,无需重启 nfs 服务和 rpcbind 服务。对该命令的说明如下:

-a 全部挂载或卸载 /etc/exports中的内容

-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab

-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)

-v 在export的时候,将详细的信息输出到屏幕上。

防火墙端口配置

NFS 除 111 和 2049 外,其它端口均为随机生成,若 NFS 服务端需要启用防火墙,需要固定其它端口。

参考文章:http://linux.vbird.org/linux_server/0330nfs.php#nfsserver_security

# 先添加默认端口,随后添加额外端口
firewall-cmd  --permanent    --add-port=111/tcp
firewall-cmd  --permanent    --add-port=111/udp
firewall-cmd  --permanent    --add-port=2049/tcp
firewall-cmd  --permanent    --add-port=2049/udp
...

重启防火墙,重启 rpcbind:

rpcinfo -p

启动

查看本机共享目录:

showmount -e

CentOS 6.x

service rpcbind start
service nfs start

CentOS 7.x

systemctl start rpcbind.service
systemctl start nfs-server.service

客户端配置

查看服务端共享目录:

showmount -e server_ip

配置自动挂载:

vim /etc/fstab

添加一行

192.168.221.100:/home/nfs/ /home/test/uploads/ nfs defaults 0 0

在客户端,只需要启动 rpcbind 服务,但仍需要安装 nfs

# CentOS 6.x
chkconfig rpcbind on
service rpcbind start

# CentOS 7.x
systemctl enable rpcbind
systemctl start rpcbind

挂载测试

mount -a

查看挂载

nfsstat -m

错误解决

执行 mount 报错

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

关闭防火墙,或者执行如下:

rpcinfo  -p  192.168.221.100

将列出端口都添加至防火墙规则,放行。

Centos 6设置iptables防火墙,简单防护DDOS和CC攻击

这个教程将向您展示如何在Centos内设置一个简单的防火墙。您可以自定义那个端口开放,那个端口关闭。同时还会展示如何防御一些较为简单的攻击。
当然这个教程只是简单的向您展示防火墙的基本用法 比如 开放Apache,SSH,Email的使用端口
iptables 是一个简单的防火墙,它被安装在大多数的Linux发行版上。iptables官方手册说它仅仅是个ipv4的封包过滤,NAT工具

防御攻击

我们添加几个简单的防御规则来阻止比较简单的攻击,如果我们的服务器收到大规模的攻击,这个防御规则也可以减少大部分的垃圾数据。

iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

这条代码就是如果收到的数据包没有tcp标示的那么就丢弃它们。

iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

这条代码就是如果收到的SYN数据包长时间占用服务器资源,就会自动结束掉他。

iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

同样,添加一个XMAS数据包。

开放端口

仅需开放我们需要开放的端口即可(一定要开放SSH连接端口,否则修改完成之后可能无法连接你的linux)

iptables -A INPUT -p tcp -m tcp --dport 需要开放的端口 -j ACCEPT

完成之后,我们仅需输入命令保存即可

iptables -L -n
iptables-save | sudo tee /etc/sysconfig/iptables

然后再输入命令启动服务即可

service iptables restart

重置CentOS 7的Root密码

1 – 在启动grub菜单,选择编辑选项启动

未分类

2 – 按键盘e键,来进入编辑界面

未分类

3 – 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh

未分类

4 – 现在按下 Control+x ,使用单用户模式启动

未分类

5 – 现在,可以使用下面的命令访问系统

chroot /sysroot

6 – 重置密码

passwd root     #需要输入两次新密码

7 – 更新系统信息

touch /.autorelabel

8 – 退出chroot

exit

9 – 重启你的系统

reboot

CentOS 7 Docker 防火墙简单配置

禁用 firewalld 服务

systemctl disable firewalld
systemctl stop firewalld

安装 iptables 防火墙服务

yum install iptables-services

创建 iptables 配置脚本

cat >> /usr/local/bin/fired.sh <<'EOF'
#!/bin/bash

iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p icmp --icmp-type 8 -j ACCEPT
#iptables -A INPUT -p tcp --dport 80 -i eth0 -m state -state NEW -m recent -update -seconds 60  -hitcount 50 -j DROP
#iptables -A OUTPUT -o eth0 -m owner -uid-owner vivek -p tcp --dport 80 -m state -state NEW,ESTABLISHED  -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 1:1023 --dport 1:1023 --syn -j DROP
iptables -A INPUT -p tcp -i eth0 --dport 22     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 80     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 2376   --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306   --sport 1024:65534 -j ACCEPT

# OpenVPN Configuration
# iptables -A POSTROUTING -t nat -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# iptables -A FORWARD -i tun+ -j ACCEPT
# iptables -A INPUT -s 10.8.0.0/24 -j ACCEPT
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -p TCP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
# iptables -A INPUT -p UDP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
EOF

chmod +x /usr/local/bin/fired.sh

加入开机启动项

cat >> /etc/rc.d/rc.local <<EOF

# Firewall & Docker
/usr/bin/systemctl start iptables.service
/usr/local/bin/fired.sh
/usr/bin/systemctl start docker
EOF

chmod +x /etc/rc.d/rc.local

禁用相关服务自启动

# 注: Docker 启动时会自动加入一些

systemctl disable iptables.service
systemctl disable docker

CentOS Linux 6 / 7 离线编译安装 PHP 7

安装

1、先去官网下载最新版的 PHP:

http://php.net

未分类

目前 PHP 7.2 只是第一个版本( PHP 7.2.0 ),我们目前安装的是 PHP 7.1.12 这个较为稳定的版本。如下图所示,复制 PHP 的 tar.gz 包的下载链接:

未分类

2、进入你的 CentOS ,先使用 yum 安装 wget(如果已经安装过,可忽略这步):

[root@host ~]# yum -y install wget

3、Ricky 个人是喜欢把一些临时文件放到 /tmp 目录下,所以进入 /tmp 目录,使用 wget 命令把 PHP 的 tar.gz 包下载到这里,然后解压:

[root@host ~]# cd /tmp
[root@host tmp]# wget http://cn.php.net/distributions/php-7.1.12.tar.gz
[root@host tmp]# tar -zxvf php-7.1.12.tar.gz

4、在进行编译安装之前,还要先安装有关于 PHP 的依赖包:

[root@host tmp]# yum install -y libxml2-devel libtool* curl-devel libjpeg-devel libpng-devel freetype-devel

5、还要再编译安装一个名为 libmcrypt 的包:

[root@host tmp]# wget https://svwh.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@host tmp]# tar -zxvf libmcrypt-2.5.8.tar.gz 
[root@host tmp]# cd libmcrypt-2.5.8
[root@host libmcrypt-2.5.8]# ./configure && make && make install

6、进入第 3 步解压出来的 php-7.1.12 文件夹,在这里执行编译安装操作:

[root@host libmcrypt-2.5.8]# cd ../php-7.1.12
[root@host php-7.1.12]# ./configure --prefix=/usr/local/php-7.1.12 --enable-fpm --enable-opcache --with-config-file-path=/usr/local/php-7.1.12/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-static --enable-sockets --enable-wddx --enable-zip --enable-calendar --enable-bcmath --enable-soap --with-zlib --with-iconv --with-freetype-dir --with-gd --with-jpeg-dir --with-xmlrpc --enable-mbstring --with-sqlite3 --with-curl --enable-ftp --with-mcrypt --with-openssl --disable-safe-mode --with-gettext
[root@host php-7.1.12]# make && make install

7、设置一个软链接:

[root@host php-7.1.12]# cd /usr/local/
[root@host local]# ln -s php-7.1.12/ php

8、添加 PHP 命令到环境变量:

[root@host local]# vi /etc/profile

键入大写字母 G 定位到最后一行,再键入小写字母 o 进入编辑模式,将以下配置复制粘贴进最后一行:

export PATH=$PATH:/usr/local/php/bin

按一次 ESC 键退出编辑模式,然后键入 “ :wq ” 保存并退出。要使改动立即生效,请执行以下命令:

[root@host local]# ./etc/profile

[root@host local]# source /etc/profile

如需查看环境变量,请执行以下命令:

[root@host local]# echo $PATH

如需查看 PHP 版本,请执行以下命令:

[root@host local]# php -v
PHP 7.1.12 (cli) (built: Dec 14 2017 15:45:28) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

9、复制配置文件和 PHP 的启动脚本,再把 PHP 的启动脚本添加到开机自启动(还要给 php-fpm 和 rc.local 文件添加可执行权限),最后执行 PHP 的启动脚本即可:

(1)复制配置文件和 PHP 的启动脚本:
[root@host local]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@host local]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@host local]# cp /tmp/php-7.1.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

(2)给 php-fpm 和 rc.local 文件添加可执行权限:
[root@host local]# chmod +x /etc/init.d/php-fpm
[root@host local]# chmod +x /etc/rc.d/rc.local
[root@host local]# chmod +x /etc/rc.local

(3)把 PHP 的启动脚本添加到开机自启动:
[root@host local]# echo "/etc/init.d/php-fpm start" >> /etc/rc.local

(4)执行 PHP 的启动脚本:
[root@host local]# /etc/init.d/php-fpm start

10、现在 php-7.1.12 和 libmcrypt-2.5.8 这两个文件夹可以删除了:

[root@host local]# cd /tmp
[root@host tmp]# rm -rf php-7.1.12/
[root@host tmp]# rm -rf libmcrypt-2.5.8/

至此,安装完成。

疑问及如何升级到更新版本的 PHP

为什么要设置这个软链接?主要是为了方便升级和记录当前 PHP 的版本号。具体请参见该篇文章 https://ccie.lol/knowledge-base/linux-centos-install-tomcat/ 的 “ 疑问及如何升级到更新版本的 Tomcat ” 部分。

测试

在代码根目录新建一个 index.php 的文本文件,文件内容为:

<?php
phpinfo();
?>

若 PHP 配置成功,浏览器打开该页面将会显示以下内容:

未分类

如何在CentOS 7上安装Nginx

本教程中的步骤要求用户拥有root权限

第一步 – 添加Nginx存储库

要添加CentOS 7 EPEL仓库,请打开终端并使用以下命令:

sudo yum install epel-release

第二步 – 安装Nginx

现在Nginx存储库已经安装在您的服务器上,使用以下yum命令安装Nginx :

sudo yum install nginx

在对提示回答yes后,Nginx将在服务器上完成安装。

第三步 – 启动Nginx

Nginx不会自行启动。要运行Nginx,请输入:

sudo systemctl start nginx

如果您正在运行防火墙,请运行以下命令以允许HTTP和HTTPS通信:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

您将会看到默认的CentOS 7 Nginx网页,这是为了提供信息和测试目的。它应该看起来像这样:

未分类

如果看到这个页面,那么你的Web服务器现在已经正确安装了。

如果想在系统启动时启用Nginx。请输入以下命令:

sudo systemctl enable nginx

恭喜!Nginx现在已经安装并运行了!

将安装了CentOS/RHEL 6/7的机器转变成路由器

在本文中,我们将学习通过使用 NAT 技术将安装有 RHEL/CentOS 6 & 7 的机器转变成路由器来用。 我们都知道,路由器是一个工作在第三层的网络设备,用于将两个或多个网络连接在一起,即,将局域网连接上广域网上或者局域网直接互联。 路由器非常昂贵,尤其对于小型组织来说更是如此,这可能是我们关注路由器的一个原因。 与其使用专用硬件,不如让我们用 Linux 机器转换成路由器来用。
RHEL/CentOS 6 和 7 上的操作过程我们都会讲。但在开始之前, 让我们先看看需要准备那些东西。

前期条件

1、一台装有 RHEL/CentOS 6 或 7 的机器

2、两块分别配有本地 IP 和外网 IP 的网卡

我们需要为两个网卡都分配 IP 地址,一个本地网络的 IP(由我们的网络管理员提供),另一个是互联网 IP(由 ISP 提供)。 像这样:

Ifcfg-en0s3 192.168.1.1 (LAN IP address)
Ifcfg-en0s5 10.1.1.1  (WAN IP address)

注意 不同 Linux 发行版的网卡名是不一样的。

现在准备工作完成了,可以进行配置了。

步骤 1 启用 IP 转发

第一步,我们启用 IP 转发。 这一步在 RHEL/CentOS 6 和 7 上是相同的。 运行

$ sysctl -w net.ipv4.ip_forward=1

但是这样会在系统重启后恢复。要让重启后依然生效需要打开

$ vi /etc/sysctl.conf

然后输入下面内容,

net.ipv4.ip_forward = 1

保存并退出。现在系统就启用 IP 转发了。

步骤 2 配置 IPtables/Firewalld 的规则

下一步我们需要启动 IPtables/firewalld 服务并配置 NAT 规则,

$ systemctl start firewalld (For Centos/RHEL 7)
$ service iptables start  (For Centos/RHEL 6)

然后运行下面命令来配置防火墙的 NAT 规则:

CentOS/RHEL 6
$ iptables -t nat -A POSTROUTING -o XXXX -j MASQUERADE
$ service iptables restart 
CentOS/RHEL 7
$ firewall-cmd  -permanent -direct -passthrough ipv4 -t nat -I POSTROUTING -o XXXX -j MASQUERADE -s 192.168.1.0/24
$ systemctl restart firewalld

这里,XXXX 是配置有外网 IP 的那个网卡名称。 这就将 Linux 机器配置成了路由器了, 下面我们就可以配置客户端然后测试路由器了。

步骤 3 配置客户端

要测试路由器,我们需要在客户端的网关设置成内网 IP, 本例中就是 192.168.1.1。 因此不管客户机是 Windows 还是 Linux, 请先确保网关是 192.168.1.1。 完成后, 打开终端或命令行并 ping 一个网站来测试客户端是否能访问互联网了:

$ ping google.com

我们也可以通过网络浏览器访问网站的方式来检查。