温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux安全策略如何加固

发布时间:2021-11-18 11:18:06 来源:亿速云 阅读:164 作者:小新 栏目:建站服务器

这篇文章主要为大家展示了“linux安全策略如何加固”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux安全策略如何加固”这篇文章吧。

账号管理
一、口令锁定策略
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、执行备份:
    #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、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、如果以下用户(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]为要修改的具体用户名。
        passwd -l lp
passwd -l sync
passwd -l halt
passwd -l news
passwd -l uucp
passwd -l operator
passwd -l games
passwd -l gopher
passwd -l smmsp
passwd -l nfsnobody
passwd -l nobody


usermod -s /bin/false lp        
usermod -s /bin/false sync      
usermod -s /bin/false halt      
usermod -s /bin/false news      
usermod -s /bin/false uucp      
usermod -s /bin/false operator  
usermod -s /bin/false games     
usermod -s /bin/false gopher    
usermod -s /bin/false smmsp     
usermod -s /bin/false nfsnobody 
usermod -s /bin/false nobody  
五、口令重复限制
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、修改策略设置
    #vi /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、首先对/etc/profile进行备份:                  
    #cp /etc/profile /etc/profile.bak            
2、编辑文件/etc/profile,在文件末尾加上如下内容:
    umask 027                                    
3、执行以下命令让配置生效:                      
    #source  /etc/profile 
二、用户最小权限控制
1、赋予用户相关账号文件最小权限                       
    #chmod 644 /etc/passwd     
    #chmod 400 /etc/shadow     
    #chmod 644 /etc/group      
    #chmod 644 /etc/services   
    #chmod 600 /etc/xinetd.conf
    #chmod 600 /etc/security   
三、修改SSH的Banner警告信息
1、如果此项检查不合规,执行以下步骤进行修复。                                               
    执行如下命令创建ssh banner信息文件:                                                    
    #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服务:                                                                            
    #/etc/init.d/sshd restart 
四、设置关键文件的属性      
1、更改该日子文件属性                                                                                           
    #chattr +a /var/log/messages       #如果不存在则忽略 
========================
认证授权
一、启用远程日志功能
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、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)、重启日志服务:                                                                                   
        #/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 
======
系统服务
一、限制root用户SSH远程登录
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服务                                                                                 
        #/etc/init.d/sshd restart                                                                      
4、修改SSH协议版本                                                                                     
    (1)、编辑文件/etc/ssh/sshd_config(vi /etc/ssh/sshd_config),修改Protocol的值为2并去掉注释。      
        Protocol 2                                                                                     
    (2)、重启ssh服务                                                                                 
        #/etc/init.d/sshd restart 
=============
文件权限
一、登陆超时时间设置   
    1、执行备份                                                                                                                    
        #cp -p /etc/profile /etc/profile_bak                         
    2、在/etc/profile文件增加以下两行(如果存在则修改,否则手工添加):
        #vi /etc/profile                                             
        TMOUT=300                                  #TMOUT按秒计算    
        export TMOUT 
二、系统core dump状态 
                1、编辑文件/etc/security/limits.conf(vi /etc/security/limits.conf),在文件末尾加入如下两行(存在则修改,不存在则新增):                               
                    * soft core 0                                                                                                     
                    * hard core 0                                                                                                     
                2、编辑文件/etc/profile(vi /etc/profile)注释掉如下行:                                                                
                    #ulimit -S -c 0 > /dev/null 2>&1  
三、修改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
       可根据实际需要修改该文件的内容。
四、禁止组合键关机
编辑文件/etc/inittab,将以下行删除、注释,或者将此行:
    ca::ctrlaltdel:/sbin/shutdown -r -t 4 now                                                              
修改为:                                            
    ca::ctrlaltdel:/bin/true 
五、禁止ICMP重定向 
1、备份文件                                                                                      
    #cp -p /etc/sysctl.conf /etc/sysctl.conf_bak                           
2、编辑文件 /etc/sysctl.conf,将net.ipv4.conf.all.accept_redirects的值改为0
3、使配置生效#sysctl  -p 
六、使用PAM认证模块禁止wheel组之外的用户su为root
1、编辑文件(vi /etc/pam.d/su)                                                                                             
    (1)、redhat4.x(32位)在文件开头加入如下两行(有则修改,没有则添加):                                                                                                      
        auth       sufficient   /lib/security/$ISA/pam_rootok.so                                                          
        auth       required    /lib/security/$ISA/pam_wheel.so use_uid                                                    
        #注意auth与sufficient之间由两个tab建隔开,sufficient与动态库路径之间使用一个tab建隔开                              
    (2)、redhat4.x(64位)在文件开头加入如下两行(有则修改,没有则添加):                                                    
        auth       sufficient   /lib64/security/$ISA/pam_rootok.so                                                        
        auth       required    /lib64/security/$ISA/pam_wheel.so use_uid                                                  
        #注意auth与sufficient之间由两个tab建隔开,sufficient与动态库路径之间使用一个tab建隔开                              
    (3)、redhat5.x和redhat6.x在文件开头加入如下两行(有则修改,没有则添加):                                               
        auth            sufficient      pam_rootok.so                                                                     
        auth            required        pam_wheel.so use_uid                                                              
        #注意auth与sufficient之间由两个tab建隔开,sufficient与动态库路径之间使用一个tab建隔开                             
说明:(这表明只有wheel组中的用户可以使用su命令成为root用户。你可以把用户添加到wheel组,以使它可以使用su命令成为root用户。)
添加方法:                                                                                                                 
    #usermod -G wheel username            #username为需要添加至wheel组的账户名称。
七、禁止IP源路由 
1、如果此项检查失败,请执行以下命令进行修复:                                                      
    for f in /proc/sys/net/ipv4/conf/*/accept_source_route
    do                                                     
       echo 0 > $f                                         
    done    
八、更改主机解析地址的顺序
1、编辑/etc/host.conf文件
    #vi /etc/host.conf,在空白处加入下面三行:
    order hosts,bind       #第一项设置首先通过DNS解析IP地址,然后通过hosts文件解析。
    multi on                  #第二项设置检测是否"/etc/hosts"文件中的主机是否拥有多个IP地址(比如有多个以太口网卡)。
    nospoof on             #第三项设置说明要注意对本机未经许可的IP欺骗。
九、更改历史命令
1、编辑文件/etc/profile,在文件中加入如下两行(存在则修改):                                               
    HISTFILESIZE=5                                         
    HISTSIZE=5                                             
2、执行以下命令让配置生效                                  
    #source /etc/profile                                   

以上是“linux安全策略如何加固”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI