安全运维 | 日常运维12条必记规则

admin 2023年6月5日23:25:28评论20 views字数 16585阅读55分17秒阅读模式

安装Linux系统最小化,即选包最小化,yum安装软件包也要最小化,无用的包不装。开机自启动服务最小化,即无用的服务不开启。操作命令最小化,例如:能用"rm -f test.txt”就不用" rm -rf test.txt登录Linux用户最小化,平时没有特殊需求用root登录,用普通用户登录即可。普通用户授权权限最小化,即只给用户必需的管理系统的命令。Linux系统文件及目录的权限设置最小化,禁止随意创建、更改、删除文件。


1.避免直接到root用户下操作

在企业生产环境中应尽量避免直接到root用户下操作,除非有超越普通用户权限的系统维护需求。

在生产环境中,删除多余的账户信息。

1)创建普通用户并设置密码

在交互式下添加普通用户并设置密码:

[root@ntp ~]# useradd noodles   #添加用户
[root@ntp ~]# passwd noodles    #修改用户密码
Changing password for user noodles.
New password: 111111
BAD PASSWORD: The password is a palindrome
Retype new password: 111111
passwd: all authentication tokens updated successfully.
[root@ntp ~]

非交互式设置用户密码:

[root@ntp ~]# echo "12345678" | passwd --stdin noodles
Changing password for user noodles.
passwd: all authentication tokens updated successfully.
[root@ntp ~]

上面的设置方法有个问题,当用history命令查看时,能看到设置的密码,所以需要清除历史命令:

[root@ntp ~]# echo "12345678" | passwd --stdin noodles && history -c
Changing password for user noodles.
passwd: all authentication tokens updated successfully.
[root@ntp ~]# history 
    1  history 
[root@ntp ~]
2)切换用户
[root@ntp ~]# whoami
root
[root@ntp ~]# su - noodles
[noodles@ntp ~]$ su - root  #注意这里-的区别是切换到用户目录,否则不切换,还是上次用户的目录
密码:
上一次登录:六 3月 26 10:26:25 CST 2022从 123.183.158.11pts/0 上
[root@ntp ~]
3)更改Linux命令提示符

LINUX命令提示符由PS1环境变量控制,我们可以查看一下:

可以直接用set命令,但是太多。

[root@ntp ~]# set | wc -l
2768
[root@ntp ~]# set | grep PS1
PS1='[u@h W]$ '  #u:user;h:hostname;W:pwd;$:用户符号
[root@ntp ~]# PS1='[u@h Wt]$ '  #增加t时间变量
[root@ntp ~11:07:59]# PS1='[u@h Wd]$ '  #增加d日期变量
[root@ntp ~六 3月 26]

上面的修改只是临时性生效,要永久生效,需修改配置文件.bashrc文件:

[root@ntp ~]# cp ~/.bashrc{,.ori.20220326}  #先做备份
[root@ntp ~]#
[root@ntp ~]# pwd  #当前所在目录
/root
[root@ntp ~]# echo "PS1='[u@h Wt]$ '" >>/root/.bashrc  #在最后一行添加语句
[root@ntp ~]# cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
 . /etc/bashrc
fi
PS1='[u@h Wt]$ '   #这是在最后一行添加的结果
[root@ntp ~]

这时并没有起作用,需要source一下:

[root@ntp ~]# source /root/.bashrc
[root@ntp ~11:20:31]$ 
4)rm命令的使用注意的问题
-f:强制删除文件或目录;
-i:删除已有文件或目录之前先询问用户(默认)
-r或-R:递归处理,将指定目录下的所有文件与子目录一并处理;
-v:显示指令的详细执行过程。
 : 不询问直接操作!

工作中尽量不要用rm命令(粉碎文件),如果必须要删除,可以用mv命令替代rm,建议将要删除的文件,移动到/tmp临时目录中,类似Windows的回收站的功能。

5)关于重启与关机

企业环境中,服务器要慎重重启,必须重启的时候,需要输入以下命令:

[root@localhost ~11:42:59]$ shutdown -r now
#重启命令还有init 6;reboot。

如果要关机,先保存后关机:

[root@localhost ~11:43:59]$ shutdown -h now
#关机命令除上面,还有init 0;halt;

上面的补充:

# shutdown -r now                         #立即重启
# shutdown -r +5                          #5分钟后重启
# shutdown -h now                         #即刻关机
# shutdown -h + 5                         #5分钟后关机

2.关闭SELinux功能

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统,实验和生产环境基本做法还是关闭居多,安全问题由其它方式来解决。

[root@ntp ~11:27:12]$ getenforce  #查看当前SELinux状态
Enforcing
[root@ntp ~11:27:57]$ cp /etc/selinux/config /etc/selinux/config.ori.20220326  #备份原文件
[root@ntp ~11:28:41]$ cat /etc/selinux/config  #查看原文件

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.  #强制开启
#     permissive - SELinux prints warnings instead of enforcing.  #不开启
#     disabled - No SELinux policy is loaded.  #彻底关闭
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@ntp ~11:28:56]$ vim /etc/selinux/config
[root@ntp ~11:29:52]$ cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

上面修改过多成后,进行比较,是否修改了:

[root@ntp ~11:30:01]$ diff /etc/selinux/config.ori.20220326 /etc/selinux/config
7c7
< SELINUX=enforcing
---
> SELINUX=disabled  #表明已修改
[root@ntp ~11:30:55]$ 

上面的修改只是下次启动后才能生效,所以需要临时处理:

[root@ntp ~11:30:55]$ getenforce
Enforcing
[root@ntp ~11:36:08]$ setenforce=0

重启后:

[root@localhost ~11:42:49]$ getenforce
Disabled
[root@localhost ~11:42:56]$ 

3.精简开机自启动服务

CentOS7已不再使用chkconfig管理启动项,使用 systemctl list-unit-files 可以查看启动项

[root@localhost ~11:50:36]$ systemctl list-unit-files | grep enable
cups.path                                     enabled 
abrt-ccpp.service                             enabled   #enabled abrt为auto bug report的缩写 用于bug报告 关闭
abrt-oops.service                             enabled   #关闭
abrt-vmcore.service                           enabled   #关闭
abrt-xorg.service                             enabled   #关闭
abrtd.service                                 enabled   #关闭
accounts-daemon.service                       enabled 
atd.service                                   enabled 
auditd.service                                enabled   #安全审计 保留
[email protected]                               enabled   #登陆相关 保留
avahi-daemon.service                          enabled 
bluetooth.service                             enabled 
chronyd.service                               enabled 
crond.service                                 enabled   #定时任务 保留
cups.service                                  enabled 
dbus-org.bluez.service                        enabled 
dbus-org.fedoraproject.FirewallD1.service     enabled 
dbus-org.freedesktop.Avahi.service            enabled 
dbus-org.freedesktop.ModemManager1.service    enabled 
dbus-org.freedesktop.nm-dispatcher.service    enabled 
display-manager.service                       enabled 
dmraid-activation.service                     enabled 
firewalld.service                             enabled 
gdm.service                                   enabled 
[email protected]                                enabled   #tty控制台相关 保留
initial-setup-reconfiguration.service         enabled 
irqbalance.service                            enabled   #优化系统中断分配 保留
iscsi-onboot.service                          enabled 
iscsi.service                                 enabled 
kdump.service                                 enabled  #内核崩溃信息捕获 自定 
ksm.service                                   enabled 
ksmtuned.service                              enabled 
libstoragemgmt.service                        enabled 
libvirtd.service                              enabled 
lvm2-monitor.service                          enabled 
mcelog.service                                enabled 
mdmonitor.service                             enabled 
microcode.service                             enabled   #处理器稳定性增强 保留
ModemManager.service                          enabled 
multipathd.service                            enabled 
NetworkManager-dispatcher.service             enabled   #网卡守护进程 关闭
NetworkManager-wait-online.service            enabled   #网卡守护进程 关闭
NetworkManager.service                        enabled 
ntpd.service                                  enabled 
postfix.service                               enabled 
qemu-guest-agent.service                      enabled 
rhel-autorelabel-mark.service                 enabled 
rhel-autorelabel.service                      enabled 
rhel-configure.service                        enabled 
rhel-dmesg.service                            enabled 
rhel-domainname.service                       enabled 
rhel-import-state.service                     enabled 
rhel-loadmodules.service                      enabled 
rhel-readonly.service                         enabled 
rngd.service                                  enabled 
rpcbind.service                               enabled 
rsyslog.service                               enabled   #日志服务 保留
rtkit-daemon.service                          enabled 
smartd.service                                enabled 
sshd.service                                  enabled   #ssh登陆 保留
sysstat.service                               enabled 
systemd-readahead-collect.service             enabled   #内核调用--预读取 保留
systemd-readahead-drop.service                enabled   #内核调用--预读取 保留
systemd-readahead-replay.service              enabled   #内核调用--预读取 保留
tuned.service                                 enabled   #保留
udisks2.service                               enabled 
vdo.service                                   enabled 
vgauthd.service                               enabled 
vmtoolsd-init.service                         enabled 
vmtoolsd.service                              enabled 
avahi-daemon.socket                           enabled 
cups.socket                                   enabled 
dm-event.socket                               enabled 
iscsid.socket                                 enabled 
iscsiuio.socket                               enabled 
lvm2-lvmetad.socket                           enabled 
lvm2-lvmpolld.socket                          enabled 
rpcbind.socket                                enabled 
virtlockd.socket                              enabled 
virtlogd.socket                               enabled 
default.target                                enabled 
graphical.target                              enabled 
nfs-client.target                             enabled 
remote-fs.target                              enabled 
runlevel5.target                              enabled 
unbound-anchor.timer                          enabled 
[root@localhost ~11:52:37]$ 

4.根据需要关闭防火墙

内网环境可以关闭,真正的企业环境是不能关闭的,只开启必须开放的服务端口。

下面查看防火墙的状态:

[root@localhost ~13:24:26]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2022-03-26 11:42:26 CST; 1h 42min ago
     Docs: man:firewalld(1)
 Main PID: 811 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─811 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

3月 26 11:42:25 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
3月 26 11:42:26 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ~13:24:36]$ 

下面进行关闭:

[root@localhost ~13:24:36]$ systemctl stop firewalld
[root@localhost ~13:25:41]$ systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~13:25:48]$ 

为了进一步学习,先开启防火墙:

[root@localhost ~13:27:07]$ systemctl start firewalld
[root@localhost ~13:27:15]$ 

企业环境开放必要的端口,比如ssh、应用(如ODOO服务8069)、数据库(postgresql端口5432)等。

先查看当前状态:

[root@localhost ~13:33:29]$ firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ntp ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
 
[root@localhost ~13:33:35]$ 

下面进行配置

[root@localhost ~13:35:07]$ firewall-cmd --zone=public --add-port=22/tcp --permanent 
success
[root@localhost ~13:35:48]$ firewall-cmd --zone=public --add-port=8069/tcp --permanent 
success
[root@localhost ~13:36:33]$ firewall-cmd --zone=public --add-port=5432/tcp --permanent
success
[root@localhost ~13:37:06]$

重载防火墙,查看开放的端口:

[root@localhost ~13:37:38]$ firewall-cmd --reload
success
[root@localhost ~13:37:54]$ firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ntp ssh
  ports: 22/tcp 8069/tcp 5432/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
 
[root@localhost ~13:38:01]$ 

5.更改SSH服务器端口

先对sshd_config备份:

[root@ntp ~]# cp /etc/ssh/sshd_config{,.ori.20220326}
[root@ntp ~]
[root@ntp ~]# ll /etc/ssh/sshd_config*
-rw-------. 1 root root 3907 8月   9 2019 /etc/ssh/sshd_config
-rw-------  1 root root 3907 3月  26 13:57 /etc/ssh/sshd_config.ori.20220326
[root@ntp ~]

下面进行修改:

Port 2222  #更改连接端口
PermitRootLogin no  #不允许root用户登录
PermitEmptyPasswords no  #不允许空密码登录
GSSAPIAuthentication no  #加快连接速度
UseDNS no  #禁止反向解析,加快连接速度

重启服务后生效,这里不做演示。

6.提权命令sudo

su - 致命的缺点是,必须知道root密码,权限最大。sudo 支持授权管理,但是不需告知root密码。通过sudo命令,我们可以把某些用户权限分类,且有针对性(精细)授权给指定的普通用户,被授权的用户无需知道root密码,只有管理员才是真正的root用户,

sudo命令执行的流程或工作原理:

1) sudo可以针对单个命令授予临时权限,sudo仅在需要时授予用户权限,减少了用户因为错误执行命令损坏系统的可能性。
2) sudo也可以用来以其它用户身份执行命令。
3) sudo也可以记录用户执行的命令,以及失败的特权获取。

将上面建立的用户noodles加入轮子组wheel

[root@ntp ~]# id noodles02
uid=1003(noodles02) gid=1003(noodles02) 组=1003(noodles02),10(wheel)
[root@ntp ~]# id noodles
uid=1002(noodles) gid=1002(noodles) 组=1002(noodles)
[root@ntp ~]# vim /etc/sudoers
[root@ntp ~]
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
noodles ALL=(ALL)       NOPASSWD: ALL

或者

[root@ntp ~]# usermod noodles -a -G wheel
[root@ntp ~]# su - noodles
上一次登录:六 3月 26 16:50:11 CST 2022pts/0 上

7.调整SSH客户端CRT的字符集

[root@ntp ~]# echo 'LANG="zh_CN.UTF-8"' > /etc/sysconfig/i18n 
[root@ntp ~]# source /etc/sysconfig/i18n
[root@ntp ~]

8.同步系统时间

每2份钟从58.220.207.226同步一次时间:

[root@ntp ~]# crontab -l
no crontab for root
[root@ntp ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@ntp ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null 
[root@ntp ~]

9.更改历史命令和超时登录时间

查看一下当前的历史记录

[root@ntp ~]# history | wc -l
85

设置只显示5条命令:

[root@ntp ~]# export HISTSIZE=5
[root@ntp ~]# history 
   82  crontab -l
   83  history
   84  history | wc -l
   85  export HISTSIZE=5
   86  history 
[root@ntp ~]

但是,上面的命令只是临时生效,要永久生效,需要下面设置:

[root@ntp ~]# cp /etc/profile{,.ori.20220326}
[root@ntp ~]# vim /etc/profile
在最后加入:
export TMOUT=300
export HISTSIZE=5
export HISTFILESIZE=5

进行比较:

[root@ntp ~]# diff /etc/profile.ori.20220326 /etc/profile
76a77,80
export TMOUT=300
export HISTSIZE=5
export HISTFILESIZE=5

[root@ntp ~]

10.隐藏Linux版本信息

先查看当前版本信息,当前系统为CentOS7.8。

[root@ntp ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@ntp ~]# cat /etc/issue
S
Kernel r on an m

[root@ntp ~]# cat /etc/issue.net
S
Kernel r on an m

备份原始文件:

[root@ntp ~]# cat /etc/redhat-release > /etc/redhat-release.ori.20220326
[root@ntp ~]# cat /etc/issue | tee /etc/issue.ori.20220326
S
Kernel r on an m

[root@ntp ~]# cat /etc/issue.net | tee /etc/issue.net.ori.20220326
S
Kernel r on an m

清空原文件内容:

[root@ntp ~]# >/etc/issue
[root@ntp ~]# >/etc/redhat-release
[root@ntp ~]# >/etc/issue.net

再次查看版本信息,无任何显示:

[root@ntp ~]# cat /etc/redhat-release
[root@ntp ~]

11.定时清理垃圾邮件

写一个定时任务来定期进行清理。

[root@ntp ~]# df -i
文件系统                   Inode 已用(I)  可用(I) 已用(I)% 挂载点
devtmpfs                  480748     408   480340       1% /dev
tmpfs                     484999       1   484998       1% /dev/shm
tmpfs                     484999     665   484334       1% /run
tmpfs                     484999      16   484983       1% /sys/fs/cgroup
/dev/mapper/centos-root 26214400  131254 26083146       1% /
/dev/vda1                 524288     341   523947       1% /boot
/dev/mapper/centos-home 21557248     210 21557038       1% /home
tmpfs                     484999       9   484990       1% /run/user/42
tmpfs                     484999       1   484998       1% /run/user/0
[root@ntp ~]

做定时任务,每天晚上0点执行一次:

[root@ntp ~]# mkdir -p /myscripts
[root@ntp ~]# echo 'find /var/spool/postfix/maildrop/ -type f | xargs rm -f' >/server/scripts/del_file.sh
-bash: /server/scripts/del_file.sh: 没有那个文件或目录
[root@ntp ~]# echo 'find /var/spool/postfix/maildrop/ -type f | xargs rm -f' >/myscripts/del_file.sh
[root@ntp ~]# cat /myscripts/del_file.sh 
find /var/spool/postfix/maildrop/ -type f | xargs rm -f
[root@ntp ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null 
[root@ntp ~]# echo '00 00 * * * /bin/sh /myscripts/del_file.sh >/dev/null 2>&1' >>/var/spool/cron/root

[root@ntp ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null 
00 00 * * * /bin/sh /myscripts/del_file.sh >/dev/null 2>&1
[root@ntp ~]

12.禁止Linux系统被ping

对于要求很高的中小型企业,设置禁止ping也是可以的,从安全的角度说,禁止ping会增加系统的安全性。

[root@ntp ~]# vim /etc/sysctl.conf
[root@ntp ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.icmp_echo_ignore_all = 1

[root@ntp ~]# sysctl -p  #刷新生效。

上面要开放ping功能,只需将1改成0,即:

net.ipv4.icmp_echo_ignore_all = 0

-END-

▎经典文章精选

安全运维 | iptable使用详解
攻防实战之蓝队视角下的战前准备
安全攻防 | 四个有趣的靶场漏洞挖掘案例
安全攻防 | mysql安全问题及修复方式


安全运维 | 日常运维12条必记规则

扫描下方 二维码 加入我们吧!

安全运维 | 日常运维12条必记规则

原文始发于微信公众号(betasec):安全运维 | 日常运维12条必记规则

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年6月5日23:25:28
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   安全运维 | 日常运维12条必记规则https://cn-sec.com/archives/1674089.html

发表评论

匿名网友 填写信息