centos作为服务器开放的服务多了,难免一些服务软件有漏洞,开放的端口号越多,上线的服务器越危险,所以我们必须在服务器上线之前把centos里面不必要的服务全部干掉,不让坏人有可乘之机。

首先看一下机器里面运行了哪些服务:(我的机器运行级别是3,只看3:on的服务就可以了)

[root@centos ~]# chkconfig --list | grep "3:on"
NetworkManager :off :off :on :on :on :on :off
abrt-ccpp :off :off :on :on :on :on :off
abrtd :off :off :on :on :on :on :off
acpid :off :off :on :on :on :on :off
atd :off :off :on :on :on :on :off
auditd :off :off :on :on :on :on :off
autofs :off :off :on :on :on :on :off
blk-availability :off :on :on :on :on :on :off
certmonger :off :off :on :on :on :on :off
cgconfig :off :off :on :on :on :on :off
cgred :off :off :on :on :on :on :off
cpuspeed :off :on :on :on :on :on :off
crond :off :off :on :on :on :on :off
cups :off :off :on :on :on :on :off
dnsmasq :off :off :on :on :on :on :off
haldaemon :off :off :on :on :on :on :off
ip6tables :off :off :on :on :on :on :off
ipsec :off :off :on :on :on :on :off
iptables :off :off :on :on :on :on :off
irqbalance :off :off :on :on :on :on :off
kdump :off :off :on :on :on :on :off
lvm2-monitor :off :on :on :on :on :on :off
mcelogd :off :off :on :on :on :on :off
mdmonitor :off :off :on :on :on :on :off
messagebus :off :off :on :on :on :on :off
netconsole :off :off :on :on :on :on :off
netfs :off :off :on :on :on :on :off
network :off :off :on :on :on :on :off
nfs :off :off :on :on :on :on :off
nfslock :off :off :on :on :on :on :off
ntpd :off :off :on :on :on :on :off
ntpdate :off :off :on :on :on :on :off
numad :off :off :on :on :on :on :off
oddjobd :off :off :on :on :on :on :off
portreserve :off :off :on :on :on :on :off
postfix :off :off :on :on :on :on :off
pppoe-server :off :off :on :on :on :on :off
psacct :off :off :on :on :on :on :off
quota_nld :off :off :on :on :on :on :off
rdisc :off :off :on :on :on :on :off
restorecond :off :off :on :on :on :on :off
rngd :off :off :on :on :on :on :off
rpcbind :off :off :on :on :on :on :off
rpcgssd :off :off :on :on :on :on :off
rpcsvcgssd :off :off :on :on :on :on :off
rsyslog :off :off :on :on :on :on :off
saslauthd :off :off :on :on :on :on :off
smartd :off :off :on :on :on :on :off
sshd :off :off :on :on :on :on :off
sssd :off :off :on :on :on :on :off
svnserve :off :off :on :on :on :on :off
sysstat :off :on :on :on :on :on :off
udev-post :off :on :on :on :on :on :off
winbind :off :off :on :on :on :on :off
wpa_supplicant :off :off :on :on :on :on :off
ypbind :off :off :on :on :on :on :off

开的服务这么多,这要是直接放到互联网怎么了得,所以我们第一步先把所有的服务统统关掉,第二步再把要必须保留的服务开启。
第一步,关掉系统所有的服务,这么多内容只能用循环脚本了,一条一条chkconfig service off 猴年马月去了,直接看命令:
我把所有开着的服务名称 通过awk取出来,再用for循环 chkconfig service off

[root@centos ~]# for n in `chkconfig --list  | grep "3:on" | awk '{print $1}'`;do chkconfig $n off;done
[root@centos ~]# chkconfig --list | grep :on
[root@centos ~]#
[root@centos ~]#
[root@centos ~]# //这会儿发现服务都被我一下子kill掉了

这会儿问题来了,我们的服务器哪些服务必须保留呢?

  1. network提供网络的服务,服务器不上网怎么能行呢?
  2. crond时间计划任务服务,服务器日常的计划执行离不开这个服务
  3. sshd 我们需要通过ssh 才能远程连接到我们的Linux,总不能天天在idc机房拿kvm来工作吧
  4. rsyslog 服务器做了哪些事情都需要靠日志才能知道,rsyslog就是用来记录日志的,原来名字叫syslog
  5. sysstat 监控系统性能的服务,对服务器掌控怎么能离得了它,sar,mpstat,iostat,vmstat都是非常有用的工具,都在这个服务里面
    总结一下,系统必须开启的服务有network,sshd,crond,rsyslog,sysstat五个

我们要做的是开启这些服务,然后验证收工,go...

[root@centos ~]# for n in crond sshd network rsyslog sysstat ;do chkconfig $n on ; done
[root@centos ~]# chkconfig --list | grep :on
crond :off :off :on :on :on :on :off
network :off :off :on :on :on :on :off
rsyslog :off :off :on :on :on :on :off
sshd :off :off :on :on :on :on :off
sysstat :off :on :on :on :on :on :off

另外一种思路:我把该留下的留下,其他全部干掉
直接给答案:

[root@centos ~]# chkconfig --list | grep :on | egrep -v "sshd|network|rsyslog|sysstat|crond" | awk '{print "chkconfig",$1,"off"}'
chkconfig NetworkManager off
chkconfig abrt-ccpp off
chkconfig abrtd off
chkconfig acpid off
chkconfig atd off
chkconfig auditd off
chkconfig autofs off
chkconfig blk-availability off
chkconfig certmonger off
chkconfig cgconfig off
chkconfig cgred off
chkconfig cpuspeed off
chkconfig cups off
chkconfig dnsmasq off
chkconfig haldaemon off
chkconfig ip6tables off
chkconfig ipsec off
chkconfig iptables off
chkconfig irqbalance off
chkconfig kdump off
chkconfig lvm2-monitor off
chkconfig mcelogd off
chkconfig mdmonitor off
chkconfig messagebus off
chkconfig netconsole off
chkconfig netfs off
chkconfig nfs off
chkconfig nfslock off
chkconfig ntpd off
chkconfig ntpdate off
chkconfig numad off
chkconfig oddjobd off
chkconfig portreserve off
chkconfig postfix off
chkconfig pppoe-server off
chkconfig psacct off
chkconfig quota_nld off
chkconfig rdisc off
chkconfig restorecond off
chkconfig rngd off
chkconfig rpcbind off
chkconfig rpcgssd off
chkconfig rpcsvcgssd off
chkconfig saslauthd off
chkconfig smartd off
chkconfig sssd off
chkconfig svnserve off
chkconfig udev-post off
chkconfig winbind off
chkconfig wpa_supplicant off
chkconfig ypbind off

[root@centos ~]# chkconfig --list | grep 3:on | egrep -v "sshd|network|rsyslog|sysstat|crond" | awk '{print "chkconfig",$1,"off"}' | bash

一条命令关掉centos所有不必要的服务和端口号的更多相关文章

  1. CentOS 7.0 更改SSH 远程连接 端口号

    许多学习过redhat 7的同学们,在使用centos的时候总会遇到一些问题,因为centos在安装时会默认开启一些服务,今天我们就来更改下centos 7.0的SSH端口. 操作步骤: 远程登录到c ...

  2. CentOS下常用的 19 条命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...

  3. centos使用上一条命令的快捷键

    使用上一条的最后一个参数 有时需要连续多个命令操作一个路径很长的文件: cat /usr/share/doc/centos-release/GPL 下一个命令可能还要使用这个路径,即使有命令补全也会很 ...

  4. 版本控制-svn服务器搭建和常用命令(centos 6.3)

    Svn是比较优秀的版本控制工具,虽然功能和性能上无法和Git媲美,但由于其容易搭建和使用的特性,所以在各个小公司还是很受欢迎的.使用Git可参考<版本控制-Git服务器搭建和常用命令使用> ...

  5. 版本控制-https svn服务器搭建和常用命令(centos 6.3)

    Svn是比较优秀的版本控制工具,虽然功能和性能上无法和Git媲美,但由于其容易搭建和使用的特性,所以在各个小公司还是很受欢迎的.使用Git可参考<版本控制-Git服务器搭建和常用命令使用> ...

  6. linux常用60条命令 转

    Linux必学的60个命令   Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命 ...

  7. https://www.jqhtml.com/30047.html strace + 命令: 这条命令十分强大,可以定位你程序到底是哪个地方出了问题

    https://www.jqhtml.com/30047.html 我的Linux手册 服务器 浏览数:72 2019-1-30 原文链接 基础安装 # CentOS sudo yum install ...

  8. 初窥Linux 之 我最常用的20条命令

    魏公 SecureCRTuname -avisftppartition,fsshell kshell,bshelluser,groupIPTables文件数,内核参数tail,less/var/log ...

  9. linux最常用的20条命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...

随机推荐

  1. topcoder srm 640 div1

    problem1 link 首先使用两个端点颜色不同的边进行连通.答案是$n-1-m$.其中$m$是联通分量的个数. problem2 link 首先构造一个最小割的模型.左边的$n_{1}$个点与源 ...

  2. Python模块1

    序列化模块: 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 序列化的目的 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性. jso ...

  3. UVA11107 Life Forms

    思路 后缀数组 先都拼在一起 二分+height分段 按照小于x的为分界,判断是否有一个分段中包含超过n/2个串 代码 #include <cstdio> #include <cst ...

  4. 前端基础面试题(JS部分)

    1.几种基本数据类型?复杂数据类型?值类型和引用数据类型?堆栈数据结构? 基本数据类型:Undefined.Null.Boolean.Number.String 值类型:数值.布尔值.null.und ...

  5. 洛谷 P3381 【【模板】最小费用最大流】

    题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的 ...

  6. Getting a handle on

    Getting a handle on 丑闻处理 Corporate crises drive the media and politicians wild.But do they damage sh ...

  7. radio为什么不能选择。急急急

    <div class="control-group"> <label class="control-label" for="&quo ...

  8. particular.js

    参数 键值 参数选项/ 说明 实例 particles.number.value number   数量 40 particles.number.density.enable boolean    t ...

  9. How to view the DNS address assigned by DHCP

    nmcli connection show clear-corporate | grep IP4 IP4.ADDRESS[1]:                         101.8.112.9 ...

  10. WSGI协议主要包括server和application两部分:

    WSGI server负责从客户端接收请求,将request转发给application,将application返回的response返回给客户端:WSGI application接收由server ...