centos 系统初始化

PS:服务器刚刚安装完,一般会初始化常用环境变量,预装常用的服务器软件,同步时间,修改防火墙配置,优化内核等,下面整理一下以方便以后使用

  1. 初始化常用环境变量

            # vi /etc/profile.d/custom.sh
            ## 全局自定义alias
            alias  worksrc='cd /usr/local/src;ls'
            
  2. 控制台登录时的欢迎界面

    有两个文件可以修改:/etc/issue和/etc/motd
    1登陆之前显示提示信息:也就是字符界面login输入用户名的前面几行显示的内容

            # vim /etc/issue
            CentOS release 6.2 (Final)
            Kernel \r on an \m
            


    2成功登陆后显示提示信息:该文件默认是空白

            # vim /etc/motd
            ***Wrrning***
            您的IP已经被记录!
            如果你没有得到拥有者××的授权,请不要查看或损坏任何文件!
            未经授权的访问或使用,会被起诉。
            ***Notice***
            得到授权者请严格按照运维操作手册管理操作服务器
            如有疑问或设置问题,请联系***
            ***Notice ***
           增加或者变更端口请联系管理员
            
                       _ooOoo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      O\  =  /O
                   ____/`---'\____
                 .'  \\|     |//  `.
                /  \\|||  :  |||//  \
               /  _||||| -:- |||||-  \
               |   | \\\  -  /// |   |
               | \_|  ''\---/''  |   |
               \  .-\__  `-`  ___/-. /
             ___`. .'  /--.--\  `. . __
          ."" '<  `.___\_<|>_/___.'  >'"".
         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
         \  \ `-.   \_ __\ /__ _/   .-` /  /
    ======`-.____`-.___\_____/___.-`____.-'======
                       `=---='
    
            140908:web服务器
            140910:svn auto update project
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               佛祖保佑       永不死机
               心外无法       法外无心
            

  3. yum预装常用的服务器软件

            #vim  linux编辑器
            #wget 网络自动下载文件的工具,支持通过HTTP、HTTPS、FTP三个最常见的TCP/IP协议下载
            #crontab  cron是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使 用计时器的功能
            #mlocate 基于数据库快速查找文件,经常用updatedb命令更新数据库
            #ntp 时间同步服务组件
            #SecureCRT的sz/rz工具包
            yum -y install vim wget gcc make crontabs mlocate ntp lrzsz gcc-c++ autoconf;
            #
            #sysstat:是一个软件包,包含监测系统性能及效率的一组工具,这些工具对于我们收集系统性能数据,比如CPU使用率、硬盘和网络吞吐数据,这些数据的收集和分析,有利于我们判断系统是否正常运行,是提高系统运行效率、安全运行服务器的得力助手
            #dstat:用来替换 vmstat,iostat,netstat,nfsstat和ifstat这些命令的工具是一个全能彩色系统信息统计工具
            #screen:类似nohup,能同时连接多个本地或远程的命令行会话,并在其间自由切换,适合远程管理终端长时间远程跑的程序
            yum -y install sysstat dstat screen ;
            #
            #top是linux下常用的监控程序,htop相当于其加强版,颜色显示不同参数,且支持鼠标操作
            #安装支持组件
            wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
            tar xvfz ncurses-5.9.tar.gz
            cd ncurses-5.9
            ./configure
            make
            make install
            #安装htop: 主页http://sourceforge.net/projects/htop/files/htop/
            wget http://sourceforge.net/projects/htop/files/htop/1.0.2/htop-1.0.2.tar.gz/download
            tar zxvf htop-1.0.2.tar.gz
            cd htop-1.0.2
            ./configure
            make
            make install
            
  4. 时间和时区设置

    查看当前时区时间

            date -R
            

    开机启动,校准时间

            vim /etc/rc.d/rc.local
             # 开机启动,校准时间
             /usr/sbin/ntpdate time.nist.gov
            

    每隔10分钟同步一下时钟

            echo " */10 * * * * /usr/sbin/ntpdate time.nist.gov >> /home/ntp.log" >> /var/spool/cron/root
            service crond restart
            /usr/sbin/ntpdate time.nist.gov
            

    替换默认时区为上海

            rm -rf /etc/localtime     #删除当前默认时区
            ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #复制替换默认时区为上海
            
  5. 防火墙配置脚本

    iptables.rule:设定最基本的规则,包括清除防火墙规则、载入模組、设定服务可接受等;
    iptables.deny:设定抵挡某些惡意主机的进入;
    iptables.allow:设定允许某些自訂的后門來源主机!

            [root@www ~]# mkdir -p /usr/local/iptables
            [root@www ~]# vim /usr/local/iptables/iptables.rule
            #!/bin/bash
            # 请先輸入您的相关参數,不要輸入错误了!
              EXTIF="eth0"             # 这个是可以连上 Public IP 的网络界面
              INIF="eth1"              # 內部 LAN 的连接介面;若无則写成 INIF=""
              INNET="192.168.100.0/24" # 若无內部网络介面,请填写成 INNET=""
              export EXTIF INIF INNET
            #### 第一部份,针对本机的防火墙设定!####
            # 1. 先设定好核心的网路功能:
              echo "1" > /proc/sys/net/ipv4/tcp_syncookies #开启 TCP Flooding的DoS攻击抵挡机制,但这个设定不适合loading已经很高的主机
              echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts #取消 ping 广播的回应
              #开启逆向路径过滤,以符合 IP 封包与网路介面的设定,开启记录有问题的封包
              for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do
                    echo "1" > $i
              done
              #取消来源路由,这个设定值是可以取消的;取消重新宣告路径的功能;取消传送重新宣告路径的功能
              for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects}; do
                    echo "0" > $i
              done
            # 2. 清除规则、设定預设政策及开放 lo 与相关的设定值
              PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
              #清除已有规则
              iptables -F
              iptables -X
              iptables -Z
              #设置默认策略
              iptables -P INPUT   DROP
              iptables -P OUTPUT  ACCEPT
              iptables -P FORWARD ACCEPT
              #开放lo
              iptables -A INPUT -i lo -j ACCEPT
              #只要是联机成功的数据包或与已发出去请求相关的数据包就予以通过
              iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
            # 3. 启动额外的防火墙 script 模組
              if [ -f /usr/local/iptables/iptables.deny ]; then
                    sh /usr/local/iptables/iptables.deny
              fi
              if [ -f /usr/local/iptables/iptables.allow ]; then
                    sh /usr/local/iptables/iptables.allow
              fi
              if [ -f /usr/local/httpd-err/iptables.http ]; then
                    sh /usr/local/httpd-err/iptables.http
              fi
            # 4. 允许某些类型的 ICMP 封包进入,通常我们会把ICMP type 8拿掉,让远程主机不知道主机是否存在,也不会对ping回应
              AICMP="0 3 3/4 4 11 12 14 16 18"
              for tyicmp in $AICMP
              do
                iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
              done
            # 5. 允许某些服务的进入,请依照你自己的环境开启
            iptables -A INPUT -p TCP -i $EXTIF --dport  22 --sport 1024:65534 -j ACCEPT # SSH
            iptables -A INPUT -p TCP -i $EXTIF --dport  80 --sport 1024:65534 -j ACCEPT # WWW
            iptables -A INPUT -p TCP -i $EXTIF --dport  3690 --sport 1024:65534 -j ACCEPT #svn
            iptables -A INPUT -p TCP -i $EXTIF --dport  3306 --sport 1024:65534 -j ACCEPT #mysql
            # iptables -A INPUT -p TCP -i $EXTIF --dport  21 --sport 1024:65534 -j ACCEPT # FTP
            # iptables -A INPUT -p TCP -i $EXTIF --dport  25 --sport 1024:65534 -j ACCEPT # SMTP
            # iptables -A INPUT -p UDP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
            # iptables -A INPUT -p TCP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
            # iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT # POP3
            # iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT # HTTPS
    
            #### 第二部份,针对后端主机的防火墙设定!####
            # 1. 先载入一些有用的模組
              modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack
            ip_conntrack_ftp ip_conntrack_irc"
              for mod in $modules
              do
                  testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
                  if [ "$testmod" == "" ]; then
                        modprobe $mod
                  fi
              done
            # 2. 清除 NAT table 的规则吧!
              iptables -F -t nat
              iptables -X -t nat
              iptables -Z -t nat
              iptables -t nat -P PREROUTING  ACCEPT
              iptables -t nat -P POSTROUTING ACCEPT
              iptables -t nat -P OUTPUT      ACCEPT
            # 3. 若有內部介面的存在 (雙网卡) 开放成為路由器,且為 IP 分享器!
              if [ "$INIF" != "" ]; then
                iptables -A INPUT -i $INIF -j ACCEPT
                echo "1" > /proc/sys/net/ipv4/ip_forward
                if [ "$INNET" != "" ]; then
                    for innet in $INNET
                    do
                        iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
                    done
                fi
              fi
              # 如果你的 MSN 一直无法连线,或者是某些网站 OK 某些网站不 OK,
              # 可能是 MTU 的问题,那你可以将底下這一行給他取消註解來启动 MTU 限制范围
              # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
              #          --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu
            # 4. NAT 伺服器后端的 LAN 內对外之伺服器设定
            # iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
            #          -j DNAT --to-destination 192.168.1.210:80 # WWW
            # 5. 特殊的功能,包括 Windows 遠端桌面所产生的规则,假设桌面主机為 1.2.3.4
            # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --dport 6000 \
            #          -j DNAT --to-destination 192.168.100.10
            # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --sport 3389 \
            #          -j DNAT --to-destination 192.168.100.20
            # 6. 最终将這些功能儲存下來吧!
            /etc/init.d/iptables save
            

    iptables.allow脚本设置

            [root@www iptables]# vim /usr/local/iptables/iptables.allow
            #!/bin/bash
            # 允许进入本机的其他网络或主机啊!
              iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT
            

    iptables.deny脚本设置

            [root@www iptables]# vim /usr/local/iptables/iptables.deny
            #!/bin/bash
            # 阻挡的主机ip或者主机所在的整个网段
              iptables -A INPUT -i $EXTIF -s 140.116.44.254 -j DROP
            

    脚本权限设置

            [root@www iptables]# chmod 700 iptables.*
            

    开机启动

            [root@www ~]# vim /etc/rc.d/rc.local
            # 1. Firewall
            /usr/local/iptables/iptables.rule
            
  6. 优化内核参数

    优化内核具体要看此服务器安装的软件,实现的功能,参数不是一成不变的,要随着改变

        mv /etc/sysctl.conf /etc/sysctl.conf.`date +"%Y-%m-%d_%H-%M-%S"`
        echo "net.ipv4.ip_forward = 0
        net.ipv4.conf.default.rp_filter = 1
        net.ipv4.conf.default.accept_source_route = 0
        net.ipv6.conf.all.disable_ipv6 = 1
        net.ipv6.conf.default.disable_ipv6 = 1
        kernel.sysrq = 0
        kernel.core_uses_pid = 1
        net.ipv4.tcp_syncookies = 1
        kernel.msgmnb = 65536
        kernel.msgmax = 65536
        kernel.shmmax = 68719476736
        kernel.shmall = 4294967296
        net.ipv4.tcp_max_tw_buckets = 6000
        net.ipv4.tcp_sack = 1
        net.ipv4.tcp_window_scaling = 0
        net.ipv4.tcp_rmem = 4096 87380 16777216
        net.ipv4.tcp_wmem = 4096 16384 16777216
        net.core.wmem_default = 8388608
        net.core.rmem_default = 8388608
        net.core.rmem_max = 16777216
        net.core.wmem_max = 16777216
        net.core.netdev_max_backlog = 262144
        net.core.somaxconn = 262144
        net.ipv4.tcp_max_orphans = 3276800
        net.ipv4.tcp_max_syn_backlog = 262144
        net.ipv4.tcp_timestamps = 0
        net.ipv4.tcp_synack_retries = 1
        net.ipv4.tcp_syn_retries = 1
        net.ipv4.tcp_tw_recycle = 1
        net.ipv4.tcp_tw_reuse = 1
        net.ipv4.tcp_mem = 94500000 915000000 927000000
        net.ipv4.tcp_fin_timeout = 15
        net.ipv4.tcp_keepalive_time = 30
        vm.swappiness = 10" >> /etc/sysctl.conf
        sysctl -p
        

    提示:linux下防御ddos攻击查找攻击源
    显示服务器上每个IP多少个连接数,连接数过大的话就有问题了,几个或几十个还好,成百上千就有问题了

        netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
        

总结:经过上面一系列的配置,服务器初始化大部分完成,尤其注意防火墙设置,一旦处理不好,就有可能把自己关在门外!

参考:
鳥哥的 Linux 私房菜 — Linux 防火墙与 NAT 伺服器
官网2

相关文章
  1. 转载:免费DDOS攻击测试工具大合集
  2. Linux防火墙之服务器安全狗
本站版权
1、本站所有主题由该文章作者发表,该文章作者与尘埃享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和尘埃的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:
二维码
Posted in firewall, linux技术, lnmp源码安装
Comments are closed.