Centos6.5/7.0安装后优化(实验用)

###########################################################################

#Centos6.5/7.0安装后优化(实验用)20150308

###########################################################################

# 优化条目:

# 1.修改ip地址、网关、主机名、DNS

# 2.关闭selinux,清空iptables

# 3.更新yum源及必要软件安装

# 添加普通用户并进行sudo授权管理

# 4.安装常用软件(上传、编译、编辑用)

# 5.定时自动更新服务器时间

 

1   修改ip地址、网关、主机名、DNS

1.1   修改配置文档

vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

IPADDR = 192.168.1.113

NETMASK = 255.255.255.0

ONBOOT = yes #引导时是否激活

GATEWAY = 192.168.1.1

#如设置下面三行就不用再设置DNS

#NM_CONTROLLED="yes"

#DNS1=114.114.114.114

#NDS2=8.8.8.8

vi /etc/resolv.conf   #修改DNS信息

nameserver 114.114.114.114

nameserver 8.8.8.8

vi /etc/sysconfig/network

HOSTNAME = c64 #修改主机名,重启生效

GATEWAY = 192.168.1.1 #修改默认网关,如果上面eth0里面不配置网关的话,默认就使用这里的网关了。

service network restart   #重启网卡,生效

# 重启网卡,也可以用下面的命令

/etc/init.d/network restart

1.2   Centos 7

vi /etc/sysconfig/network-scripts/ifcfg-ens33 #ens33要看具体情况而定。Centos不再是eth1

HWADDR="00:0c:29:13:f8:0e"

TYPE=Ethernet

BOOTPROTO=static#启用静态IP地址

DEFROUTE=yes

PEERDNS=yes

PEERROUTES=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_PEERDNS=yes

IPV6_PEERROUTES=yes

IPV6_FAILURE_FATAL=no

NAME="ens33" #与之前不一样

UUID=9bc6b382-e521-4292-be9a-d0b33218a43c

ONBOOT=yes#开启自动启用网络连接

IPADDR0=10.0.7.2#设置IP地址

PREFIXO0=16#设置子网掩码

GATEWAY0=10.0.1.1#设置网关

DNS1=8.8.8.8#设置主DNS

DNS2=8.8.4.4#设置备DNS

 

1.3   图形界面修改

# 如需使用setup图像配置网络可使用以下命令

使用 yum 源直接下载安装 或者 去下载 setuptool 软件包安装

#安装setuptool

yum install setuptool

#可以发现执行setup后不全,再安装一个用于系统服务管理

yum install ntsysv

#安装setup中配套的防火墙设置

yum install system-config-securitylevel-tui #centos6可用

yum install

#安装setup中配套的 网络 设置

yum install system-config-network-tui

#安装setup中配套的键盘设置

yum install system-config-keyboard

 

2   清空iptables, 关闭selinux

2.1   centos6

iptables -F;iptables -X;iptables -Z;/etc/init.d/iptables save

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2.2   centos7

#禁止firewall开机启动

[root@centos ~]# systemctl disable firewalld.service

#设置防火墙开机启动

[root@centos ~]# systemctl enable iptables.service

#停止firewall

[root@centos ~]# systemctl stop firewalld.service

#重启防火墙使配置生效

[root@centos ~]# systemctl restart iptables.service

1)、配置防火墙,开启80端口、3306端口

CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

 

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

#修改配置文件则永久生效,但是必须要重启系统

setenforce   0 #临时生效命令

getenforce  #查看selinux当前状态

3   更新yum源及必要软件安装

# yum 安装软件,默认获取rpm包的途径从国外官方源,改成国内的源。

3.1   阿里云源,REDHAT源与epel

centos7

##################################

yum install -y wget

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup

mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

wget -O /etc/yum.repos.d/epel-testing.repo http://mirrors.aliyun.com/repo/epel-testing.repo

rpm --import /etc/pki/rpm*

yum clean all

yum makecache

yum update -y

#######################################

centos6

方式一

yum install -y wget

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup

mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

wget -O /etc/yum.repos.d/epel-testing.repo http://mirrors.aliyun.com/repo/epel-testing.repo

rpm --import /etc/pki/rpm*

yum clean all

yum makecache

yum update -y

方式二# 禁用 fastestmirror 插件,安装epel.remi,更换为ali

sed -i.backup 's/^enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

wget -O /etc/yum.repos.d/CentOS-Base-aliyun.repo http://mirrors.aliyun.com/repo/Centos-6.repo

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

if [[ ! -f /etc/yum.repos.d/epel.repo.backup ]]; then

    mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup 2>/dev/null || :

fi

if [[ ! -f /etc/yum.repos.d/epel-testing.repo.backup ]]; then

    mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup 2>/dev/null || :

fi

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

 

 

3.2   本地源

mkdir /mnt/centos

mount /dev/cdrom /mnt/centos

#1)mount -o loop /mnt/*/1.iso /mnt/centos

#2)cp /dev/cdrom /目录/XXXXX.iso ;mount -t iso9660 -o loop /*/XXXXX.iso /mnt/iso

mkdir /etc/yum.repos.d/old

mv /etc/yum.repos.d/ /etc/yum.repos.d/old

touch /etc/yum.repos.d/local.repo

cat > /etc/yum.repos.d/local.repo <<EOF

[local]

name=local_DVD

baseurl=file:///mnt/centos

enabled=1

gpgcheck=0

priority=1

EOF

echo "mount /dev/cdrom /mnt/centos/" >> /etc/rc.d/rc.local

--------------

3.3自制源

yum install createrepo yum-priorities wget -y

mkdir -p /opt/tools/cloudstack

cd  /opt/tools

下载相应的rpm

createrepo .

vi /etc/yum.repo.d/local_cloud.repo

[local_cloud]

name=local_cloud_rpm

baseurl=file:///opt/tools/cloudstack

enabled=1

pgpcheck=0

priority=2

 

yum clean all ; yum makecache ; yum repolist

4   安装常用软件(上传、编译、编辑用)

yum install -y ntpdate sysstat lrzsz vim-enhanced automake autoconf make git  gcc libtool zlib-devel python-devel python-pip

#如要安装软件包可使用组安装

安装桌面 yum groupinstall "X Window System" Desktop -y

yum groupinstall "Development Tools"

yum groupinstall x software devlopment

4.1   #选用vim换成vi

mv /bin/vi /bin/vi.save

ln -s /usr/bin/vim /bin/vi

touch ~/.vimrc

cat > ~/.vimrc <<EOF

:set nu

:colorschem desert

EOF

4.2   python pip源的修改为阿里

mkdir -p ~/.pip

touch ~/.pip/pip.conf

cat > ~/.pip/pip.conf <<EOF

[global]

index-url = http://mirrors.aliyun.com/pypi/simple/

EOF

5   定时自动更新服务器时间

yuminstall -y ntpdate #上面已经安装

echo '1 * * * */usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root

hwclock–w #写入硬件BIOS

#提示: CentOS 6.5 的时间同步命令路径不一样6 /usr/sbin/ntpdate5 /sbin/ntpdate

Tue Jul1 08:58:44 CST 2014# CST (Central Standard Time)

5.1   时区东八区

#如时区就问题可换成东八区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

5.2   语音改中文(可选)

sed -i 's#LANG="en_US.*"#LANG="zh_CN.UTF-8"#' /etc/sysconfig/i18n

#echo 'LANG="zh_CN.GB18030"' > /etc/sysconfig/i18n

source /etc/sysconfig/i18n

6   安装后的基本调优及安全设置

6.1   清理开机自启动的服务

ntsysv

# 关闭所有服务

for offlist in `chkconfig --list|grep 3:on|awk '{print$1}'`;do chkconfig --level 3 $offlist off; done

#打开需要的开机自启动

for onlist in crond network rsyslog sshd sysstat ntpdate; do chkconfig --level 3 $onlist on; done

chkconfig --list|grep 3:on

6.2   将普通账号加入到sudo管理

useradd drudy

passwd drudy

#命令行echo "123456"|passwd --stdin drudy && history -c

visudo            #vi /etc/sudoer

root    ALL=(ALL)       ALL

drudy  ALL=(ALL)       ALL

 

su - drudy

普通用户[drudy@sysadmin ~]$ echo $PATH

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/drudy/bin

[root@sysadmin ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

如普通用户找不到命令,可以添加环境变量。或改环境变量文件

vi ~/.bash_profile

source ~/.bash_profile

echo $PATH

6.3   更改ssh登陆配置

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori

vi /etc/ssh/sshd_config

Port 1378

PermitRootLogin no      #禁止root用户远程登陆

PermitEmptyPasswords no           #禁止空密码登陆

UseDNS no                                #不使用DNS

 

lsof -i tcp:1378

6.4   加大服务器文件描述符

vi /etc/security/limits.conf 

*                -       nofile          65535 #最后一行添加

#echo '*                -       nofile          65535' >>/etc/security/limits.conf

ulimit -n

6.5   调整内核参数文件

/etc/sysctl.conf

net.ipv4.tcp_syncookies = 1            #1是开启SYN Cookies,当出现SYN等待队列溢出时,启用Cookies来处,理,可防范少量SYN攻击,默认是0关闭

net.ipv4.tcp_tw_reuse = 1              #1是开启重用,允许讲TIME_AIT sockets重新用于新的TCP连接,默认是0关闭

net.ipv4.tcp_tw_recycle = 1            #TCP失败重传次数,默认是15,减少次数可释放内核资源

net.ipv4.ip_local_port_range = 4096 65000  #应用程序可使用的端口范围

net.ipv4.tcp_max_tw_buckets = 5000     #系统同时保持TIME_WAIT套接字的最大数量,如果超出这个数字,TIME_WATI套接字将立刻被清除并打印警告信息,默认180000

net.ipv4.tcp_max_syn_backlog = 4096    #进入SYN宝的最大请求队列,默认是1024

net.core.netdev_max_backlog =  10240   #允许送到队列的数据包最大设备队列,默认300

net.core.somaxconn = 2048              #listen挂起请求的最大数量,默认128

net.core.wmem_default = 8388608        #发送缓存区大小的缺省值

net.core.rmem_default = 8388608        #接受套接字缓冲区大小的缺省值(以字节为单位)

net.core.rmem_max = 16777216           #最大接收缓冲区大小的最大值

net.core.wmem_max = 16777216           #发送缓冲区大小的最大值

net.ipv4.tcp_synack_retries = 2        #SYN-ACK握手状态重试次数,默认5

net.ipv4.tcp_syn_retries = 2           #向外SYN握手重试次数,默认4

net.ipv4.tcp_tw_recycle = 1            #开启TCP连接中TIME_WAIT sockets的快速回收,默认是0关闭

net.ipv4.tcp_max_orphans = 3276800     #系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上,如果超出这个数字,孤儿连接将立即复位并打印警告信息

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_mem[0]:低于此值,TCP没有内存压力;

net.ipv4.tcp_mem[1]:在此值下,进入内存压力阶段;

net.ipv4.tcp_mem[2]:高于此值,TCP拒绝分配socket。内存单位是页,可根据物理内存大小进行调整,如果内存足够大的话,可适当往上调。上述内存单位是页,而不是字节。

应用

#可用于apachenginxsquid多种等web应用

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog = 32768

net.core.somaxconn = 32768

 

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

 

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

 

net.ipv4.tcp_tw_recycle = 1

#net.ipv4.tcp_tw_len = 1

net.ipv4.tcp_tw_reuse = 1

 

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

 

#net.ipv4.tcp_fin_timeout = 30

#net.ipv4.tcp_keepalive_time = 120

net.ipv4.ip_local_port_range = 1024 65535

 

7   其它源

7.1   增加PUIAS安装源

PUIAS Linux是面向桌面和服务器的完整的操作系统,它靠编译Red Hat Enterprise Linux的源代码包来创建。除了这些上游的软件包外,该项目还提供一些其他的软件仓库:“Addons”包含了通常的Red Hat发行中未收入的额外软件包,“Computational”提供专门针对科学计算的软件,“Unsupported”则收入各种各样的测试性软件 包。该发行由美国普林斯顿 大学的高等研究所维护。

PUIAS Linux是面向桌面和服务器的完整的操作系统,它靠编译Red Hat Enterprise Linux的源代码包来创建。除了这些上游的软件包外,该项目还提供一些其他的软件仓库:“Addons”包含了通常的Red Hat发行中未收入的额外软件包,“Computational”提供专门针对科学计算的软件,“Unsupported”则收入各种各样的测试性软件 包。该发行由美国普林斯顿 大学的高等研究所维护。

创建/etc/yum.repos.d/PUIAS_6_computational.repo,并添加如下内容:

[PUIAS_6_computational]

name=PUIAS computational Base $releasever - $basearch

mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist

#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias

下载并安装GPG key

$ sudo wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-puias http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias

$ sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias

检验下是否安装成功

$ sudo rpm -qa gpg*

Tips:安装完EPELPUIAS两个源后,可以检测下:

$ sudo yum repolist

7.2   rpmforge源:

rpmforge源包含了RHEL, CentOS Scientific Linuxrpm包,它包含了许多和RHEL各版本兼容的软件包。

linux操作系统对应的rpmforge包下载页面:http://repoforge.org/use/

CentOS

源地址:http://wiki.centos.org/AdditionalResources/Repositories/RPMForge

i686 http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm

x86_64: rpm -ivh  http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

rpm -Uvh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

源地址http://repoforge.org/use/

EL 6: i686, x86_64

EL 5: i386, x86_64, ppc

EL 4: i386, x86_64, ppc

[rpmforge]
name = RHEL $releasever - RPMforge.net - dag
baseurl = http://apt.sw.be/redhat/el6/en/$basearch/rpmforge
mirrorlist = http://apt.sw.be/redhat/el6/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=12
#################################################################

 

rm -rf mysql  //mysql目录

如果是编译过程出错,要重新编译

那就

make clean

然后再

./configure

make && make install

当数据量变大时试用

du -sh/| sort -nr | head 统计最大文件

find ./-mtime 0 :返回最近 24 小时内修改过的文件。

find ./-mtime 1 返回的是前 48 ~ 24 小时修改过的文件。而不是 48 小时以内修改过的文件。

那怎么返回 10 天内修改过的文件?find还可以支持表达式关系运算,所以可以把最近几天的数据一天天的加起来:

find ./-mtime 0 -o -mtime 1 -o -mtime 2 ……虽然比较土,但也算是个方法了。





附件列表

 

转载于:https://my.oschina.net/drudy/blog/487360

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值