首页 > 系统相关 >Linux操作系统加固建议

Linux操作系统加固建议

时间:2023-06-30 23:03:09浏览次数:45  
标签:文件 sshd 操作系统 etc ssh conf Linux 加固 pam

1、Linux操作系统加固建议

1.1.1 口令锁定策略
1、执行备份

(1)、redhat执行备份:
        #cp -p /etc/pam.d/system-auth /etc/pam.d/system-auth_bak

    (2)、SUSE9:
        #cp -p /etc/pam.d/passwd /etc/pam.d/passwd.bak

    (3)、SUSE10:
        #cp -p /etc/pam.d/common-password /etc/pam.d/common-password.bak

2、修改策略设,编辑文件/etc/pam.d/system-auth(SUSE:/etc/pam.d/passwd)增加如下内容:

auth required pam_tally2.so deny=5 onerr=fail no_magic_root unlock_time=180    #unlock_time单位为秒
    account  required  pam_tally2.so            #(redhat5.1以上版本支持 pam_tally2.so,其他版本使用pam_tally.so)

1.1.2 口令生存期
1、执行备份:

#cp -p /etc/login.defs /etc/login.defs_bak

2、修改策略设置,编辑文件/etc/login.defs(vi /etc/login.defs),在文件中加入如下内容(如果存在则修改,不存在则添加):

PASS_MAX_DAYS     90
    PASS_MIN_DAYS      10
    PASS_WARN_AGE    7

1.1.3 口令复杂度
1、redhat系统
编辑文件/etc/pam.d/system-auth,在文件中找到如下内容:

password requisite  pam_cracklib.so,将其修改为:
    password requisite  pam_cracklib.so try_first_pass retry=3 dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=8         #至少包含一个数字、一个小写字母、一个大写字母、一个特殊字符、且密码长度>=8

2、suse9编辑/etc/pam.d/passwd、suse10以上编辑/etc/pam.d/common-password,在文件中加入如下内容(如果文件中存在password的行请首先注释掉):

password required       pam_pwcheck.so  nullok
    password requisite      pam_cracklib.so dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=8 use_authtok
    password required       pam_unix2.so    nullok use_first_pass use_authtok

1.1.4 删除无关帐号(需要锁定和删除的用户shell域设置的个数)
1、如果以下用户(lp|sync|halt|news|uucp|operator|games|gopher|smmsp|nfsnobody|nobody)没有被删除或锁定,可以使用如下命令对其进行操作:

(1)、删除用户:
        #userdel username

    (2)、锁定用户:
        #passwd -l username        #锁定用户,只有具备超级用户权限的使用者方可使用。
            #passwd –d username      #解锁用户,解锁后原有密码失效,登录设置新密码才能登录。
        #passwd -u username       #解锁用户后,原密码仍然有效。

    (3)、修改用户shell域为/bin/false
        #usermod -s /bin/false username        #命令来更改相应用户的shell为/bin/false,其中[name]为要修改的具体用户名。

1.1.5 口令重复次数限制
1、执行备份

#cp -p /etc/pam.d/system-auth /etc/pam.d/system-auth.bak
    #cp -p /etc/pam.d/passwd /etc/pam.d/passwd.bak
    #cp -p /etc/pam.d/common-password /etc/pam.d/common-password.bak

2、创建文件/etc/security/opasswd用于存储旧密码,并设置权限。

#touch /etc/security/opasswd
    #chown root:root /etc/security/opasswd
    #chmod 600 /etc/security/opasswd

3、修改策略设置

#vim /etc/pam.d/system-auth((SUSE9:/etc/pam.d/passwd、SUSE10以上/etc/pam.d/common-password)在类似password  sufficient pam_unix.so所在行末尾增加remember=5,中间以空格隔开.如果没有则新增,例如:
    password    sufficient    pam_unix.so md5 shadow nullok try_first_pass use_authtok remember=5

1.1.6 文件与目录缺省权限控制

1、首先对/etc/profile进行备份:
    #cp /etc/profile /etc/profile.bak

2、编辑文件/etc/profile,在文件末尾加上如下内容:
    umask 027

3、执行以下命令让配置生效:
    #source  /etc/profile

1.1.7 修改SSH的Banner警告信息

1、如果此项检查不合规,执行以下步骤进行修复。
    #touch /etc/ssh_banner
    #chown bin:bin /etc/ssh_banner
    #chmod 644 /etc/ssh_banner
    #echo " Authorized only. All activity will be monitored and reported " > /etc/ssh_banner
    可根据实际需要修改该文件的内容。
2、修改/etc/ssh/sshd_config文件,添加如下行:
    Banner /etc/ssh_banner
3、重启sshd服务:
systemctl restart sshd.service
#/etc/init.d/sshd restart

1.1.8 启用远程日志功能

1、linux
    编辑文件 /etc/syslog.conf或者/etc/rsyslog.conf,增加如下内容:
        *.*           @<日志服务器ip或者域名>

2、suse
    编辑文件/etc/syslog-ng/syslog-ng.conf,增加如下内容:
        destination logserver { udp("192.168.56.168" port(514)); };
        log { source(src); destination(logserver); };

        #日志服务器ip视实际情况来确定。
3、重启syslog服务
#/etc/init.d/syslog stop
#/etc/init.d/syslog start

1.1.9 记录安全事件日志

1、redhat5.x之前编辑/etc/syslog.conf,在文件中加入如下内容:
    *.err;kern.debug;daemon.notice /var/adm/messages,其中/var/adm/messages为日志文件。

    (1)、如果该文件不存在,则创建该文件,命令为:
        #touch /var/adm/messages

    (2)、修改权限为666,命令为:
        #chmod 666 /var/adm/messages

    (3)、重启日志服务:
        #/etc/init.d/syslog restart

2、redhat6.x编辑/etc/rsyslog.conf,在文件中加入如下内容:
    *.err;kern.debug;daemon.notice /var/adm/messages,其中/var/adm/messages为日志文件。

    (1)、如果该文件不存在,则创建该文件,命令为:
        #touch /var/adm/messages

    (2)、修改权限为666,命令为:
        #chmod 666 /var/adm/messages

    (3)、重启日志服务:
systemctl restart rsyslog.service

        #/etc/init.d/syslog restart
3、SUSE编辑文件/etc/syslog-ng/syslog-ng.conf,在文件中加入如下内容:
    filter f_msgs { level(err) or facility(kern) and level(debug) or facility(daemon) and level(notice); };
    destination msgs { file("/var/adm/msgs"); };
    log { source(src); filter(f_msgs); destination(msgs); }; 其中/var/adm/msgs为日志文件。

    (1)、如果该文件不存在,则创建该文件,命令为:
        #touch /var/adm/msgs

    (2)、修改权限为666,命令为:
        #chmod 666 /var/adm/msgs

    (3)、重启日志服务:
        #/etc/init.d/syslog restart

1.1.10限制root用户SSH远程登录(禁用root用户远程访问系统)

1、执行备份:
    #cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config_bak

2、新建一个普通用户并设置高强度密码(防止设备上只存在root用户可用时,无法远程访问):
    #useradd username
3、禁止root用户远程登录系统
    (1)、编辑文件/etc/ssh/sshd_config(vi /etc/ssh/sshd_config),修改PermitRootLogin值为no并去掉注释。
        PermitRootLogin no                      #则禁止了root从ssh登录。

    (2)、重启SSH服务
systemctl restart sshd.service

        #/etc/init.d/sshd restart

4、修改SSH协议版本
    (1)、编辑文件/etc/ssh/sshd_config(vi /etc/ssh/sshd_config),修改Protocol的值为2并去掉注释。
        Protocol 2

    (2)、重启ssh服务
systemctl restart sshd.service

1.1.11限制root用户SSH远程登录(ssh协议使用版本2)

1、执行备份:
    #cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config_bak

2、新建一个普通用户并设置高强度密码(防止设备上只存在root用户可用时,无法远程访问):
    #useradd username
    #passwd username

3、禁止root用户远程登录系统
    (1)、编辑文件/etc/ssh/sshd_config(vi /etc/ssh/sshd_config),修改PermitRootLogin值为no并去掉注释。
        PermitRootLogin no                      #则禁止了root从ssh登录。

    (2)、重启SSH服务
systemctl restart sshd.service

        #/etc/init.d/sshd restart
4、修改SSH协议版本
    (1)、编辑文件/etc/ssh/sshd_config(vi /etc/ssh/sshd_config),修改Protocol的值为2并去掉注释。
        Protocol 2

    (2)、重启ssh服务
systemctl restart sshd.service

 #/etc/init.d/sshd restart

1.1.12使用SSH协议进行远程维护(ssh服务状态)

1、关闭TELNET服务(如果设备安装了TELNET服务则执行以下操作关闭,否则无需操作):
    (1)、备份
        #cp -p /etc/xinetd.d/telnet /etc/xinetd.d/telnet_bak

    (2)、编辑文件/etc/xinetd.d/telnet(vi /etc/xinetd.d/telnet),把disable项改为yes,即disable = yes。
    (3)、执行以下命令重启xinetd服务。
        #service xinetd restart

2、开启SSH服务:
    (1)、安装SSH软件包。
    (2)、通过以下命令启动ssh服务:
        #/etc/init.d/sshd start

1.1.13登陆超时时间设置

1、执行备份
    #cp -p /etc/profile /etc/profile_bak

2、在/etc/profile文件增加以下两行(如果存在则修改,否则手工添加):
    #vi /etc/profile
    TMOUT=300                                  #TMOUT按秒计算
    export TMOUT

1.1.14系统core dump状态(/etc/security/limits.conf)

1、编辑文件/etc/security/limits.conf(vi /etc/security/limits.conf),在文件末尾加入如下两行(存在则修改,不存在则新增):
    * soft core 0
    * hard core 0

1.1.15修改SSH的Banner信息

1、修改文件/etc/motd的内容,如没有该文件,则创建它:
    #touch /etc/motd

2、使用如下命令在文件/etc/motd中添加banner信息。
   ### #echo " Authorized users only. All activity may be monitored and reported " > /etc/motd
   vim  /etc/motd
   
   Access denied

1.1.16控制远程访问的IP地址(allowno)

1、执行备份:
    #cp -p /etc/hosts.allow /etc/hosts.allow_bak

2、编辑/etc/hosts.allow文件,增加一行 service: 允许访问的IP(service为服务,例如sshd),举例如下:all:192.168.4.44:allow    #允许单个IP访问所有服务进程
    sshd:192.168.1.*:allow    #允许192.168.1.0的整个网段访问SSH服务进程

1.1.17控制远程访问的IP地址(denyno)

1、执行备份:
    #cp -p /etc/hosts.deny /etc/hosts.deny_bak

2、编辑文件/etc/hosts.deny增加一行(deny优先级高于allow)
    all:all

                        #拒绝一切远程访问配合文件hosts.allow使用。

1.1.18配置NTP(ntp服务状态)

1、编辑ntp 的配置文件:
    #vi /etc/ntp.conf  在server一行添加ntp服务器的IP地址

2、启动ntpd服务:
    #/etc/init.d/ntpd start    #suse9启动ntp服务为:/etc/init.d/xntpd start

1.1.19配置NTP( 为目标主机配置ntp服务器的个数)

1、编辑ntp 的配置文件:
    #vi /etc/ntp.conf  在server一行添加ntp服务器的IP地址

2、启动ntpd服务:
    #/etc/init.d/ntpd start    #suse9启动ntp服务为:/etc/init.d/xntpd start

1.1.20禁止IP源路由
如果此项检查失败,请执行以下命令进行修复:

vim /etc/rc.d/rc.local  添加以下内容
    #for f in /proc/sys/net/ipv4/conf/*/accept_source_route
    do
       echo 0 > $f
    done

1.1.21更改主机解析地址的顺序

1、编辑/etc/host.conf文件
    #vi /etc/host.conf,在空白处加入下面三行:
    order hosts,bind       #第一项设置首先通过DNS解析IP地址,然后通过hosts文件解析。
    multi on                  #第二项设置检测是否"/etc/hosts"文件中的主机是否拥有多个IP地址(比如有多个以太口网卡)。
    nospoof on             #第三项设置说明要注意对本机未经许可的IP欺骗。

1.1.22更改主机解析地址的顺序

1、编辑/etc/host.conf文件
    #vi /etc/host.conf,在空白处加入下面三行:
    order hosts,bind       #第一项设置首先通过DNS解析IP地址,然后通过hosts文件解析。
    multi on                  #第二项设置检测是否"/etc/hosts"文件中的主机是否拥有多个IP地址(比如有多个以太口网卡)。
    nospoof on             #第三项设置说明要注意对本机未经许可的IP欺骗。

1.1.23历史命令设置

1、编辑文件/etc/profile,在文件中加入如下两行(存在则修改):
    HISTFILESIZE=5
    HISTSIZE=5
2、执行以下命令让配置生效
    #source /etc/profile


标签:文件,sshd,操作系统,etc,ssh,conf,Linux,加固,pam
From: https://blog.51cto.com/u_3369545/6595383

相关文章

  • linux存放history命令的文件
    当你在玩Linux的时候,如果你经常使用命令行来控制你的Linux系统,那么有效地使用命令历史机制将会使效率获得极大提升。事实上,一旦你掌握了我在下面给出的15个有关Linuxhistory历史命令的例子,你就会发现使用history历史命令行将更有乐趣。1、使用HISTTIMEFORMAT在历史中显示TIMESTAMP......
  • 手动清除或刷新Linux的Swap分区
    物理内存接近饱和时,系统会自动将不常用的内存文件转储到Swap中,但Swap使用率达30%的时候对系统性能可能有一定影响。但当物理内存重新释放时,储存在Swap分区的其它应用不会重新回到物理内存中,所以会造成一定影响,此时可以考虑手动刷新Swap分区。一、SWAP开关:1、关闭SWAP一般用于大物理......
  • Linux索引节点(Inode)用满导致空间不足
    1、问题:在创建新目录和文件是提示“nospaceleftondevice”!按照以前的情况,很有可能是服务器空间又被塞满了,通过命令查看,发现还有剩余。再用df-i查看了一下/分区的索引节点(inode),发现已经用满(已用=100%),导致系统无法创建新目录和文件。2、问题原因分析:Inode译成中文就是索引节......
  • linux系统使用审计audit查看系统安全情况。
    查看审计报告一旦定义审计规则后,它会自动运行。过一段时间后,我们可以看看auditd是如何帮我们跟踪审计的。Auditd提供了另一个工具叫aureport。从名字上可以猜到,aureport是使用系统审计日志生成简要报告的工具。我们已经配置auditd去跟踪/etc/passwd文件。auditd参数设置后一段......
  • Linux编译过程以及生成中间文件--链接
    1、编译系统C源文件到可执行文件共经历了4个过程。在使用GCC编译程序时,编译过程可以被细分为四个阶段,包括预处理、编译、汇编、链接 1、预处理在预处理阶段,编译器主要作加载头文件、宏替换、条件编译的作用。一般处理带“#”的语句。 我们可以通过gcc的-E选项进行查看......
  • 关于Linux内核-live-patching-更新内核补丁模块
    由于内核补丁模块是通过RPM软件包交付和应用,更新累积内核补丁模块就如同更新任何其他RPM软件包一样。先决条件:系统已订阅实时补丁流,如将当前安装的内核订阅到实时补丁流中所述。流程如下:更新至当前内核的新累计版本:#yumupdate"kpatch-patch=$(uname-r)"以上命令......
  • 关于Linux系统-内核-live-patching-的介绍
    关于Linux,我们可以使用RedHatEnterpriseLinux内核实时修补解决方案在不重启或者重启任何进程的情况下对运行的内核进行补丁先看一下官网的一段关于Applyingpatcheswithkernellivepatching的介绍:YoucanusetheRedHatEnterpriseLinuxkernellivepatchingsol......
  • linux使用宏
    在普通模式下输入qa(a表示使用a寄存器,也可以qb或qc)输入之后会显示录制或者recoding,输入需要记录的指令之后输入q结束录制在普通模式下输入@a执行录制在a寄存器中的宏,10@a表示执行10次......
  • Linux下mount命令出现Host is down的解决方法
    1、Linux下mount命令出现Hostisdown的解决方法https://jingyan.baidu.com/article/cb5d6105cec1a4005c2fe084.html当使用Linux中的mount命令挂载一个Windows的共享目录的时候有时会出现:mounterror(112):HostisdownRefertothemount.cifs(8)manualpage(e.g.manmount.c......
  • linux 在线安装 Redis
    博主介绍:✌博主从事应用安全和大数据领域,有8年研发经验,5年面试官经验,Java技术专家✌Java知识图谱点击链接:体系化学习Java(Java面试专题)......