linux系统基础优化

1.Linux系统基础优化

一、关闭SELinux功能

Selinux是什么?

安全工具,控制太严格,生产环境不用它,使用其他安全手段。

简介:

SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制。

Security-Enhanced Linux (SELinux)由以下两部分组成:

1) Kernel SELinux模块(/kernel/security/selinux)

2) 用户态工具

SELinux是一个安全体系结构,它通过LSM(Linux Security Modules)框架被集成到Linux Kernel 2.6.x中。它是NSA (United States National Security Agency)和SELinux社区的联合项目。

SELinux提供了一种灵活的强制访问控制(MAC)系统,且内嵌于Linux Kernel中。SELinux定义了系统中每个【用户】、【进程】、【应用】和【文件】的访问和转变的权限,然后它使用一个安全策略来控制这些实体(用户、进程、应用和文件)之间的交互,安全策略指定如何严格或宽松地进行检查。

SELinux对系统用户(system users)是透明的,只有系统管理员需要考虑在他的服务器中如何制定严格的策略。策略可以根据需要是严格的或宽松的。

关闭SElinux方式:

1)         通过vi /etc/selinux/config 进入配置文件进入修改

2)         通过sed 命令操作:

实现命令:sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config

输出:

[[email protected] ~]# cp /etc/selinux/config  /etc/selinux/config.ori  操作前的备份

[[email protected] ~]# sed -i  ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config  直接修改源文件

[[email protected] ~]# grep SELINUX=disabled /etc/selinux/config                  SELINUX=disabled  查看修改后的效果

查看对比修改后的文件:

实现命令:(vimdiff)diff /etc/selinux/config.ori /etc/selinux/config

输出:

7c7

< SELINUX=enforcing

---

> SELINUX=disabled

SELINUX=disabled(永久生效)重启系统

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

[[email protected] ~]# getenforce(查看命令行是否关闭selinux)

Enforcing

[[email protected] ~]# setenforce (设置selinux)

usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

[[email protected] ~]# setenforce 0 (命令行关闭selinux)

[[email protected] ~]# getenforce

Permissive(临时生效)

二、运行级别

什么是运行级别:

runlevel linux运行时的一种状态标示,这个标示用数字表示。

运行级别的其中状态:

0

halt,关机状态

1

single  user 单用户,找回root密码

2

multiuser  without nfs 多用户没有NFS网络文件系统

3

文本模式(Full multiuser mode)*******工作模式

4

unused

5

图形。桌面、X11

6

reboot  重启

如何查看linux运行级级别:

实现命令:runlevel

输出:

[[email protected] ~]# runlevel

N(前一次) 3(当前)

更改运行级别:init

三、精简开机系统启动

为什么要设置开机自启动?

1、节省开机时间、加快启动速度

2、节省资源开销

3、减少安全隐患

需要保留的开机自启动:

sshd:远程连接linux服务器

rsyslog:是操作系统提供的一种机制,系统的守护程序通常会使用rsylog将各种信息系统日志文件中

network:激活关闭各个网络接口

crond:用于周期性地执行系统及用户配置的任务计划(周期性的处理一些重复问题的计划任务服务)

Sysstat:是一个软件包,监测系统性能及效率的一组工具

Sysstat软件包集成的主要工具为:

iostat工具提供CPU使用率及硬盘吞吐效率的数据

mpstat工具提供与单个或多个处理器相关的数据

sar工具负责收集、报告并存储系统活跃的信息

如何实现?

第一种方法:通过setup来修改

第二种方法:通过ntsysv来修改

第三种方法:通过chkconfig来实现

a. [[email protected] ~]# chkconfig --list|grep 3:on|awk ‘{print $1}‘|grep -Ev "sshd|network|rsyslog|crond|sysstat"|awk ‘{print "chkconfig " $1 " off"}‘|bash

[[email protected] ~]# chkconfig --list|grep 3:on

crond           0:off   1:off   2:on    3:on    4:on    5:on       6:off

network         0:off   1:off   2:on    3:on    4:on    5:on       6:off

rsyslog         0:off   1:off   2:on    3:on    4:on    5:on       6:off

sshd            0:off   1:off   2:on    3:on    4:on    5:on       6:off

sysstat         0:off   1:on    2:on    3:on    4:on    5:on       6:off

b. [[email protected] ~]# chkconfig --list|grep 3:on|awk ‘{print $1}‘|grep -Ev "sshd|network|rsyslog|crond|sysstat"|sed -r ‘s#(.*)#chkconfig \1 off#g‘|bash

[[email protected] ~]# chkconfig --list|grep 3:on

crond           0:off   1:off   2:on    3:on    4:on    5:on       6:off

network         0:off   1:off   2:on    3:on    4:on    5:on       6:off

rsyslog         0:off   1:off   2:on    3:on    4:on    5:on       6:off

sshd            0:off   1:off   2:on    3:on    4:on    5:on       6:off

sysstat         0:off   1:on    2:on    3:on    4:on    5:on       6:off

c. [[email protected] ~]# for name in `chkconfig --list|grep 3:on|awk ‘{print $1}‘|grep -Ev "sshd|network|rsyslog|crond|sysstat"`;do chkconfig $name off;done

[[email protected] ~]# chkconfig --list|grep 3:on                           crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off

sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

sysstat         0:off   1:on    2:on    3:on    4:on    5:on    6:off

四.关闭iptables防火墙

查看防火墙:iptables -L -n

关闭防火墙:

实现命令:/etc/init.d/iptables stop

输出:

[[email protected] ~]# /etc/init.d/iptables stop

iptables:将链设置为政策 ACCEPT:filter                    [确定]

iptables:清除防火墙规则:                                 [确定]

iptables:正在卸载模块:                                   [确定]

查看防火墙状态:

实现命令:/etc/init.d/iptables status

输出:

[email protected] ~]# /etc/init.d/iptables status

[[email protected] ~]# /etc/init.d/iptables status

Table: filter

Chain INPUT (policy ACCEPT)

num  target     prot opt source               destination

Chain FORWARD (policy ACCEPT)

num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)

num  target     prot opt source               destination

五.linux运维思想最小化原则

原则:多一事不如少一事!

1、安装linux系统最小化,选包最小化,yum安装软件包也要最小化,无用的包不装。

2、开机自启动最小化

3、操作命令最小化

例如:用rm -f test.txt而不用rm -fr test.txt

4、登录linux用户最小化。

平时没有需求不用root登录,用普通用户登录即可

5、普通用户授权权限最小化,

即只给必须的管理系统的命令。

6、linux系统文件及目录的权限设置最小化,禁止随意创建、更改、删除。理论上限制掉。

六. 更改SSH服务端远程登录的配置(配置文件:/etc/ssh/sshd_config)

更改方法:

方法一:通过vi进入配置文件进行修改

vi /etc/ssh/sshd_config

####by oldboy#2011-11-24##

Port 52113

PermitRootLogin no

PermitEmptyPasswords no

UseDNS no

GSSAPIAuthentication no

####by oldboy#2011-11-24##

方法二:通过sed命令实现修改

sed -ir ‘13 i Port 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no‘  sshd_config

重启生效:/etc/init.d/sshd reload【平滑重启不影响用户】(restart)

Linux下SSH远程连接服务慢的解决方案请见老男孩的博客:

http://oldboy.blog.51cto.com/2561410/1300964

八、利用sudo管理文件描述

在visudo进入文件增加普通用户的命令路径使得普通用户环境下利用sudo进行命令操作。

Allow root torun any commands anywhere

root     ALL=(ALL)       ALL

oldboy   ALL=           (ALL)   NOPASSWD: ALL  /bin/ls

用户     用户管理的机器  临时拥有的用户角色      /bin/ls

注意:内置命令无法用到sudo。

九.linux字符显示设置:

字符集是一套文字符号及其编码:GBK 、UTF-8(企业广泛使用)

调整服务器端字符集:调整字符集路径(/etc/sysconfig/i18n)记住

[[email protected]/]# cat /etc/sysconfig/i18n

LANG="en_US.UTF-8"       英文字符

SYSFONT="latarcyrheb-sun16"

[[email protected]/]# cat /etc/sysconfig/i18n

LANG="en_US.UTF-8"

SYSFONT="latarcyrheb-sun16"

[[email protected]/]#cp/etc/sysconfig/i18n /etc/sysconfig/i18n.oldboy.20151003    修改文件前的备份

[[email protected]/]#sed-i ‘s#LANG="en_US.UTF-8"#LANG="zh_CN.UTF-8"#g‘/etc/sysconfig/i18n    利用sed替换字符文件修改为中文字符

[[email protected]/]# cat /etc/sysconfig/i18n

LANG="zh_CN.UTF-8"

SYSFONT="latarcyrheb-sun16"

[[email protected]/]# echo $LANG   查看修改字符后的效果

en_US.UTF-8

[[email protected]/]# source /etc/sysconfig/i18n 使得修改后的文件生效

[[email protected]/]# echo $LANG

zh_CN.UTF-8

十. 设置linux服务器时间同步

互联网同步时间

[[email protected]/]/usr/sbin/ntpdate time.nist.govov   互联网同步时间

[[email protected]/]# date -s "2015/10/3 9:34"

2015年 10月 03日星期六 09:34:00 CST

[[email protected]/]# ntpdate time.nist.gov   set the date and time viaNTP

3 Oct 09:35:21 ntpdate[28135]: adjust timeserver 132.163.4.103 offset 0.286494 sec

[[email protected]/]# date   date 查看时间  -s 修改时间

2015年 10月 03日星期六 09:48:46 CST

[[email protected] /]# hwclock   query and set the hardwareclock

2015年10月03日星期六 08时59分12秒  -0.737654 seconds

crond :定时任务

每5分钟同步一次

[[email protected]/]# echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov /dev/null2>&1" >>/var/spool/cron/root

[[email protected] /]# crontab –l  定时生效

*/5 * * * */usr/sbin/ntpdate time.nist.gov /dev/null 2>&1

十一、设置超时

临时生效

[[email protected] /]# export  TMOUT=300设置超时时间300S

[[email protected]/]# echo "export TMOUT=300" >>/etc/profile

[[email protected]/]# source /etc/profile  使得设置生效

[[email protected]/]# echo $TMOUT

300

十二.history历史记录数

临时生效

[[email protected] /]# export  HISTSIZE=5  定义历史记录数5条

[[email protected] /]# history

728  cat ~/.bash_history

729  HISTFILESIZE=5

730  cat ~/.bash_history

731  HISTSIZE=5

732  history

[[email protected] /]#export HISTFILESIZE=5 定义历史记录文件数5条

[[email protected] /]# cat ~/.bash_history

visudo

su - oldboy

su oldboy

netstat -an|grep EST

su oldboy

永久生效

[[email protected] ~]# echo ‘export TMOUT=300‘>>/etc/profile

[[email protected] ~]# echo ‘exportHISTSIZE=5‘ >>/etc/profile

[[email protected] ~]# echo ‘exportHISTFILESIZE=5‘ >>/etc/profile

[[email protected] ~]# tail -3 /etc/profile

export TMOUT=300

export HISTSIZE=5

export HISTFILESIZE=5

[[email protected] ~]# source /etc/profile  使得文件生效

[[email protected] ~]# echo $TMOUT

300

[[email protected] ~]# echo $HISTSIZE

5

十三.调整linux系统文件描述符数量

文件描述符是由无符号整数表示的句柄,进程使用它来标示打开文件。

文件描述符概念:

1、表示形式为整数数字(0-65535)

2、会占用文件描述符(标示打开文件)

查看默认文件描述符

ulimit-n

3、调整文件描述符

[[email protected] ~]# ulimit -SHn 65535  设置文件描述符数量

[[email protected] ~]# ulimit -n

65535(32768)

[[email protected] ~]#  echo ‘*       -    nofile      65535‘ >>/etc/security/limits.conf    将修改的描述符数量写入文件

[[email protected] ~]# tail -1/etc/security/limits.conf                   *        -    nofile     65535

十四.调整内核参数文件 (/etc/sysctl.conf)

vim/etc/sysctl.conf

linux内核优化参数:

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

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.ip_local_port_range = 4000    65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。

net.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_max =25000000

net.netfilter.nf_conntrack_tcp_timeout_established= 180

net.netfilter.nf_conntrack_tcp_timeout_time_wait= 120

net.netfilter.nf_conntrack_tcp_timeout_close_wait= 60

net.netfilter.nf_conntrack_tcp_timeout_fin_wait= 120

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

网络状态说明及优化命令和优化细节参考资料请看:

http://yangrong.blog.51cto.com/6945369/1321594老男孩教育的优秀学生博文

http://oldboy.blog.51cto.com/2561410/1336488

sysctl -p 使得加载的参数生效

十五.隐藏linux版本号:

[[email protected] ~]# cat /etc/issue

CentOS release 6.7 (Final)

Kernel \r on an \m

[[email protected] ~]# cat /etc/issue.net

CentOS release 6.7 (Final)

Kernel \r on an \m

[[email protected] ~]# >/etc/issue

[[email protected] ~]# >/etc/issue.net

[[email protected] ~]# cat /etc/issue

十六.锁定系统文件

相关的系统文件:/etc/passwd/etc/shadow /etc/group /etc/gshadow  /etc/inittab

[[email protected]~]# chattr +i /etc/passwd /etc/shadow /etc/group  /etc/gshadow /etc/inittab   +i锁定系统文件

[[email protected] ~]# useradd dddd

useradd: cannot open /etc/passwd

[[email protected] ~]# rm -f /etc/passwd

Do not use rm command. -f /etc/passwd

[[email protected] ~]# \rm -f /etc/passwd

rm: 无法删除"/etc/passwd": 不允许的操作

[[email protected] ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab  -i解除系统文件

[[email protected] ~]# useradd dddd

[[email protected] ~]# chattr +i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab

[[email protected] ~]# lsattr /etc/passwd  查看系统文件属性

----i--------e- /etc/passwd

[r[email protected] ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab

[[email protected] ~]# lsattr /etc/passwd

-------------e- /etc/passwd

十七.禁止linux系统被ping

内核中修改禁止ping,缺点是禁止自己ping

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all

[[email protected] ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf

[[email protected] ~]# tail -1 /etc/sysctl.conf

net.ipv4.icmp_echo_ignore_all=1

[[email protected] ~]# sysctl -p

生效:

[[email protected] ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf

[[email protected] ~]# tail -1 /etc/sysctl.conf

net.ipv4.icmp_echo_ignore_all=1

[[email protected] ~]# sysctl -p

还原禁ping:

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all

十八.定时清理邮件服务临时目录垃圾文件

centos5系列的系统默认安装Sen时dmail服务,因此邮件临时存放地点的路径/var/spool/clientmqueue/.

centos6默认情况下没有安转Sendmail服务,而是改装了Posfix服务,因此邮件存放地点的路径为:/var/spool/postfit/maildrop/

以上两个目录很容易被垃圾文件填满导致系统的inode数量不够用,从而导致无地方存放文件

手动清理的方法:

find /var/spool/clientmqueue/ -typef|xargs rm -f适合centOS5的sendmail服务

find /var/spool/postfix/maildrop/ -typef|xargs rm -f适合Centos6的postfix服务

定时清理的方法为:将上述命令写成脚本,然后做定时任务,每天晚上0点执行一次(定时任务再说)

小结:如何优化linux:

1、关闭SElinux

2、关闭防火墙,设定运行级别为3.

3、精简开机自启动服务

4、SSH安全控制(提前建立普通用户)

5、sudo 管理用户授权

6、调整文件描述符

7、更改合适的字符集

8、锁定关键系统文件

9、禁止显示内核版本及系统版本信息

10、设置会话的超时时间及历史记录数

11、禁止PING

12、优化LINUX内核参数

13、特定漏洞yum/rpm升级

14、清楚多余的系统虚拟账号

15、服务器时间同步

16、打补丁下载软件调整为国内的下载地址(调整yum源)

17、定时清理邮件服务临时目录垃圾文件

18、为grub菜单加密码

时间: 2024-08-04 03:44:21

linux系统基础优化的相关文章

Linux 系统基础优化和常用命令

目录 Linux 系统基础优化和常用命令 软连接 tar解压命令 gzip命令 netstart命令 ps命令 kill命令 killall命令 SELinux功能 iptables防火墙 Linux中文显示设置(防止中文乱码) df 命令 tree命令 设置主机名 DNS 查看Linux的dns, 唯一配置文件 本地强制dns解析文件 /etc/hosts nslookup命令 计划任务crond服务 软件包管理 windows程序 macos程序 RPM软件包管理器 实例 rpm安装软件 r

Linux系统基础优化及常用命令

Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和ip等参数 ifup,ifdown    脚本命令,更简单的方式启动关闭网络 ip 符合指令,直接修改上述功能 在我们刚装好linux的时候,需要用xshell进行远程连接,那就得获取ip地址,有时候网卡默认是没启动的,Linux也就拿不到ip地址,因此我们得手动启动网卡 #编辑网卡配置文件 vim

8,Linux系统基础优化及常用命令

Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和ip等参数 ifup,ifdown    脚本命令,更简单的方式启动关闭网络 ip 符合指令,直接修改上述功能 在我们刚装好linux的时候,需要用xshell进行远程连接,那就得获取ip地址,有时候网卡默认是没启动的,Linux也就拿不到ip地址,因此我们得手动启动网卡 #编辑网卡配置文件 vim

linux系统基础优化16条知识汇总

优化的总结: 1.不用root管理,以普通用户的名义通过sudo授权管理. 2.更改默认的远程连接SSH服务端口,禁止root用户远程连接,甚至 要更改只监听内网IP. 3.定时自动更新服务区时间,使其和互联网时间同步. 4.配置yum跟新源,从更新源下载安全软件包. 5.关闭seLinux及iptables(在工作场景中,如果有外部IP一般要打开 6.调整文件描述符的数量,进程及文件的打开都会消耗文件描述. 7.定时自动清理邮件目录垃圾文件,防止inodes节点被占满 (注意Centos6和C

linux系统基础优化和安全重点小结

本文描述一些基本的系统优化知识点,为适应批量配置,所有代码为非交互式配置方法,可在crt中通过命令窗口做简单的批量处理. 不用root登录管理系统,而以普通用户登录通过sudo授权管理.作为一个例子这里新建一个用户,并做sudo提权 useradd gotimmygo echo 123456|passwd --stdin gotimmygo \cp /etc/sudoers{,ori} echo "gotimmygo ALL=(ALL) NOPASSWD:ALL">>/et

Linux系统基础优化以及常用命令

Linux基础系统优化 Linux的网络功能相当强悍, 一时之间我们无法了解所有的网络命令, 在配置服务器基础环境是, 先俩接下网络参数设定命令 : ifconfig 查询, 设置网卡和ip等参数 ifip, ifdown 脚本命令, 更简单的方式启动和关闭网络 ip 符合指令, 直接修改上述功能 在我们刚装好linux的时候, 需要用xshell进行远程连接, 那就得获取ip地址, 有时候网卡默认是没有起动的, linux也就拿不到IP地址, 因此我们必须要手动启动网卡. # 编辑网卡配置文件

9.28 linux系统基础优化

关闭SELinux(是美国安全局对强制访问的实现)功能 [[email protected] ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config #使用sed + i 命令修改 [[email protected] ~]# grep "SELINUX=disabled" /etc/selinux/config #grep查看 SELINUX=disabled [[email protected]

Linux系统基础优化脚本--安装完操作系统必做的操作

#!/bin/sh ###open iptables service, allow this ports access 80, 3307, 21#### function firewall() {         service iptables start         for Port in 21 80 3307            do                  iptables -I INPUT 5  -m state --state NEW -m tcp -p tcp --

Linux服务器企业集群架构部署搭建(一)----环境配置要求与系统基础优化

命运是大海,当你能够畅游时,你就要纵情游向你的所爱,因为你不知道狂流什么会到来,卷走一切希望与梦想. 本文作者:燁未央_Estelle版权声明:测试学习,不足之处,欢迎指正.允许转载,转载时请务必以超链接形式标明文章原始出处.作者信息和本声明 第一章 集群架构搭建部署目标1.1 本次架构部署配置概况提示,本次集群架构所使用的服务器均为VM虚拟机进行模拟搭建测试及学习. ①两台linux服务器作为负载均衡服务器(LVS-01/LVS-02),基本模式:LVS-DR+keepalived.负责分发所