您的位置:首页 > 运维架构 > Linux

Linux:系统的基本优化

2016-01-20 18:06 351 查看
前言,在拥有一台服务器的时候,首先第一件事就要根据自己的需求进行初期的优化(装好系统了),以下是关于linux系统的基本优化,内容来源与网络,自己整理了以下,忘记来自哪个网址了,

centos 系统优化
1、内核参数优化

[root@wulaoer ~]# cat /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。内存单位是页,可根据物理内存大小进行调整,如果内存足够大的话,可适当往上调。上述内存单位是页,而不是字节。


2、同步系统时间

[root@wulaoer ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  #设置Shanghai时区,这两个文件是同一个文件。
[root@wulaoer ~]# ntpdate cn.pool.ntp.org && hwclock -w  #同步时间并写入blos硬件时间
[root@wulaoer ~]# crontab -e #设置任务计划每天零点同步一次
0 * * * * /usr/sbin/ntpdate cn.pool.ntp.org ; hwclock -w


更多crontab的使用方法参考,猛点这里  

3、修改history记录

[root@wulaoer ~]# vi /etc/profile #修改记录100个
HISTSIZE=100


4、去除系统相关信息

[root@wulaoer ~]# echo "Welecome to Server" > /etc/issue
[root@wulaoer ~]# echo "Welecome to Server" > /etc/redhat-release


5、调整文件描述符大小

[root@wulaoer ~]# ulimit -n   #默认是65535 -u最大进程
65535
[root@wulaoer ~]# echo "ulimit -SHn 102400" >> /etc/rc.local #设置开机自动生效


表 1. ulimit 参数说明

选项 [options] 含义 例子

-H    设置硬资源限制,一旦设置不能增加。    ulimit – Hs 64;限制硬资源,线程栈大小为 64K。
-S    设置软资源限制,设置后可以增加,但是不能超过硬资源设置。    ulimit – Sn 32;限制软资源,32 个文件描述符。
-a    显示当前所有的 limit 信息。    ulimit – a;显示当前所有的 limit 信息。
-c    最大的 core 文件的大小, 以 blocks 为单位。    ulimit – c unlimited; 对生成的 core 文件的大小不进行限制。
-d    进程最大的数据段的大小,以 Kbytes 为单位。    ulimit -d unlimited;对进程的数据段大小不进行限制。
-f    进程可以创建文件的最大值,以 blocks 为单位。    ulimit – f 2048;限制进程可以创建的最大文件大小为 2048 blocks。
-l    最大可加锁内存大小,以 Kbytes 为单位。    ulimit – l 32;限制最大可加锁内存大小为 32 Kbytes。
-m    最大内存大小,以 Kbytes 为单位。    ulimit – m unlimited;对最大内存不进行限制。
-n    可以打开最大文件描述符的数量。    ulimit – n 128;限制最大可以使用 128 个文件描述符。
-p    管道缓冲区的大小,以 Kbytes 为单位。    ulimit – p 512;限制管道缓冲区的大小为 512 Kbytes。
-s    线程栈大小,以 Kbytes 为单位。    ulimit – s 512;限制线程栈的大小为 512 Kbytes。
-t    最大的 CPU 占用时间,以秒为单位。    ulimit – t unlimited;对最大的 CPU 占用时间不进行限制。
-u    用户最大可用的进程数。    ulimit – u 64;限制用户最多可以使用 64 个进程。
-v    进程最大可用的虚拟内存,以 Kbytes 为单位。    ulimit – v 200000;限制最大可用的虚拟内存为 200000 Kbytes。


6、关闭重启ctl-alt-delete组合键

[root@wulaoer ~]# vi /etc/init/control-alt-delete.conf
#exec /sbin/shutdown -r now "Control-Alt-Deletepressed" #注释掉


7、删除不必要的系统用户

[root@wulaoer ~]# userdel 用户名
[root@wulaoer ~]# groupdel 组名
[root@wulaoer ~]# usermod -G
[root@wulaoer ~]# vi /etc/group #找到GROUP1那一行,删除 A 或者用命令 gpasswd -d A GROUP
[root@wulaoer ~]# vi /etc/shadow
[root@wulaoer ~]# vi /etc/passwd


8、关闭不必要开机自启动服务

chkconfig --list | grep 3:on#查看开机自启动服务
chkconfig 服务名称 off #关闭自动的服务


9、禁用root远程登录

[root@wulaoer ~]# vi /etc/ssh/sshd_config
PermitRootLoginno
PermitEmptyPasswords no #禁止空密码登录
UseDNSno #关闭DNS查询


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

[root@wulaoer ~]# useradd user
[root@wulaoer ~]# echo "123456" | passwd --stdin user #设置密码
[root@wulaoer ~]# vi /etc/sudoers #或visudo打开,添加user用户所有权限
root ALL=(ALL) ALL
user ALL=(ALL) ALL


11、清空防火墙并设置规则

[root@wulaoer ~]# iptables -F #清楚防火墙规则
[root@wulaoer ~]# iptables -L #查看防火墙规则
[root@wulaoer ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许通过的端口
[root@wulaoer ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@wulaoer ~]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT
[root@wulaoer ~]# iptables -A INPUT -p udp --dport 53 -j ACCEPT
[root@wulaoer ~]# iptables -A INPUT -p udp --dport 123 -j ACCEPT
[root@wulaoer ~]# iptables -A INPUT -p icmp -j ACCEPT
[root@wulaoer ~]# iptables -P INPUT DROP
[root@wulaoer ~]# /etc/init.d/iptables save #保存设置


12、关闭SELinux

[root@wulaoer ~]# vi /etc/selinux/config
SELINUX=disabled
[root@wulaoer ~]# setenforce #临时生效
[root@wulaoer ~]# getenforce #查看selinux状态


13、主机名设置

[root@wulaoer ~]# vi /etc/sysconfig/network
HOSTNAME=wulaoer
[root@wulaoer ~]# hostname wulaoer  #临时生效


14、禁止无用服务

#! /bin/bash
service acpid off
service atd stop
service auditd stop
service avahi-daemon stop
service avahi-dnsconfd stop
service bluetooth stop
service conman stop
service cpuspeed stop
service cups stop
service dnsmasq stop
service dund stop
service firstboot stop
service hidd stop
service httpd stop
service ibmasm stop
service ip6tables stop
service irda stop
service kdump stop
service lm_sensors stop
service mcstrans stop
service messagebus stop
service microcode_ctl stop
service netconsole stop
service netfs stop
service netplugd stop
service nfs stop
service nfslock stop
service nscd stop
service ntpd stop
service oddjobd stop
service pand stop
service pcscd stop
service portmap stop
service psacct stop
service rdisc stop
service restorecond stop
service rpcgssd stop
service rpcidmapd stop
service rpcsvcgssd stop
service saslauthd stop
service sendmail stop
service setroubleshoot stop
service smb stop
service vncserver stop
service winbind stop
service wpa_supplicant stop
service xfs stop
service ypbind stop
service yum-updatesd stop
chkconfig acpid off
chkconfig atd off
chkconfig auditd off
chkconfig avahi-daemon off
chkconfig avahi-dnsconfd off
chkconfig bluetooth off
chkconfig conman off
chkconfig cpuspeed off
chkconfig cups off
chkconfig dnsmasq off
chkconfig dund off
chkconfig firstboot off
chkconfig hidd off
chkconfig httpd off
chkconfig ibmasm off
chkconfig ip6tables off
chkconfig irda off
chkconfig kdump off
chkconfig lm_sensors off
chkconfig mcstrans off
chkconfig messagebus off
chkconfig microcode_ctl off
chkconfig netconsole off
chkconfig netfs off
chkconfig netplugd off
chkconfig nfs off
chkconfig nfslock off
chkconfig nscd off
chkconfig ntpd off
chkconfig oddjobd off
chkconfig pand off
chkconfig pcscd off
chkconfig portmap off
chkconfig psacct off
chkconfig rdisc off
chkconfig restorecond off
chkconfig rpcgssd off
chkconfig rpcidmapd off
chkconfig rpcsvcgssd off
chkconfig saslauthd off
chkconfig sendmail off
chkconfig setroubleshoot off
chkconfig smb off
chkconfig vncserver off
chkconfig winbind off
chkconfig wpa_supplicant off
chkconfig xfs off
chkconfig ypbind off
chkconfig yum-updatesd off
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: