动态线条
动态线条end

1、系统账号清理

(1)将非登录用户的shell设为/sbin/nologin

     ●    方法一 : usermod -s

     ●    方法二 : chsh 命令,交互式修改

     ●    方法三 : chsh -s

ps:除了上述三种方法,也可以直接 vi 编辑 /etc/passwd 文件进行修改

(2)锁定长期不使用的账号

     ●     方法一 :passwd -l (将在密文前增加2个“!”)解锁 passwd -u 查看 passwd -s

     ●     方法二 :usermod -L (将在密文前增加1个“!”)解锁 usermod -U     

ps:若用 passwd -l 命令对某账号进行锁定,若用 usermod -U 命令解锁,需要进行两次操作才可以

 (3)删除无用的账号

     ●     userdel [r] 用户名

 (4)锁定账号文件  /etc/passwd、/etc/shadow

     ●    锁定:chattr + i /etc/passwd /etc/shadow

     ●    解锁:chattr - i /etc/passwd /etc/shadow

     ●    查看:lsattr /etc/passwd /etc/shadow

ps:锁定后即使是超户,也不能修改该文件,即不能创建、删除、修改用户信息

PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了。chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux系统都是2.6以上内核了。通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录。chattr命令不能保护/、/dev、/tmp、/var目录。lsattr命令是显示chattr命令设置的文件属性。

这两个命令是用来查看和改变文件、目录属性的,与chmod这个命令相比,chmod只是改变文件的读写、执行权限,更底层的属性控制是由chattr来改变的。

chattr命令的用法:chattr [ -RVf ] [ -v version ] [ mode ] files…
最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTtu]这些字符组合的,这部分是用来控制文件的
属性。

+ :在原有参数设定基础上,追加参数。
- :在原有参数设定基础上,移除参数。
= :更新为指定参数设定。
A:文件或目录的 atime (access time)不可被修改(modified), 可以有效预防例如手提电脑磁盘I/O错误的发生。
S:硬盘I/O同步选项,功能类似sync。
a:即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文件安全,只有root才能设定这个属性。
c:即compresse,设定文件是否经压缩后再存储。读取时需要经过自动解压操作。
d:即no dump,设定文件不能成为dump程序的备份目标。
i:设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。
j:即journal,设定此参数使得当通过mount参数:data=ordered 或者 data=writeback 挂 载的文件系统,文件在写入时会先被记录(在journal中)。如果filesystem被设定参数为 data=journal,则该参数自动失效。
s:保密性地删除文件或目录,即硬盘空间被全部收回。
u:与s相反,当设定为u时,数据内容其实还存在磁盘中,可以用于undeletion。
各参数选项中常用到的是a和i。a选项强制只可添加不可删除,多用于日志系统的安全设定。而i是更为严格的安全设定,只有superuser (root) 或具有CAP_LINUX_IMMUTABLE处理能力(标识)的进程能够施加该选项。

应用举例:

1、用chattr命令防止系统中某个关键文件被修改:
# chattr +i /etc/resolv.conf

然后用mv /etc/resolv.conf等命令操作于该文件,都是得到Operation not permitted 的结果。vim编辑该文件时会提示W10: Warning: Changing a readonly file错误。要想修改此文件就要把i属性去掉: chattr -i /etc/resolv.conf

# lsattr /etc/resolv.conf
会显示如下属性
----i-------- /etc/resolv.conf

2、让某个文件只能往里面追加数据,但不能删除,适用于各种日志文件:
# chattr +a /var/log/messages

2、密码安全控制

 (1)设置密码有效期

    ●    修改某个已存在用户的密码有效期:

                chage -M 天数 用户名

               passwd -x 天数 用户名

    ●    设置今后添加用户时的默认密码有效期

 方法一:vi 编辑 /etc/login.defs 文件,修改 “PASS_MAX_DAY” 后面的数值

 (2)要求用户下次登录时修改密码

    ●    方法:chage -d 0 用户名

出于安全考虑设置密码规则

设置密码到期的天数。 
用户必须在天内更改密码。 
此设置仅影响创建用户,而不会影响现有用户。 
如果设置为现有用户,请运行命令“chage -M(days)(user)”。

2、密码策略
2.1编辑/etc/login.defs 指定如下参数的值。

PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7

参数值的解释:

PASS_MAX_DAYS           (设置密码过期日期)
PASS_MIN_DAYS               (设置密码最少更改日期)
PASS_MIN_LEN                  (设置密码最小长度)时指密码设置的最小长度,一般定义为8位以上
PASS_WARN_AGE              (设置过期提前警告天数)
2.2 设置账户锁定登陆失败锁定次数、锁定时间

编辑/etc/pam.d/system- auth

首先 cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak

#vi /etc/pam.d/system-auth

auth required pam_tally.so onerr=fail deny=6 unlock_time=300

解释:设置位密码连续六次锁定,锁定时间300秒

解锁用户 faillog -u <用户名> -r

2.3 设置口令的复杂程度。

编辑/etc/pam.d/system- auth 首先 cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak

#vi /etc/pam.d/system-auth

找到pam_cracklib.so 在后面加一些参数,

例:password requisite pam_cracklib.so minlen=8 ucredit=-2 lcredit=-2 dcredit=-5 ocredit=-1

意思为最少有2个大写字符,2个小写字符,5个数字, 1个符号

2.4 限制su的权限

如果你不想任何人能够用su作为root,可以编辑/etc/pam.d/su文件,增加如下两行:

auth sufficient /lib/security/pam_rootok.so debug

auth required /lib/security/pam_wheel.so group=isd

这时,仅isd组的用户可以用su作为root。此后,如果你希望用户admin能够用su作为root,可以运行如下命令

# usermod -G10 admin

2.5 设置最小密码长度。  用户不能设置小于此参数的密码长度。

# set 8 for minimum password length

[root@linuxprobe~]# authconfig --passminlen=8 --update

# the parameter is set in a config below

[root@linuxprobe~]# grep "^minlen" /etc/security/pwquality.conf

minlen = 8

2.6 在新密码中设置同一类的允许连续字符的最大数目

# set 4 for maximum number of allowed consecutive characters of the same class

[root@linuxprobe~]# authconfig --passmaxclassrepeat=4 --update

# the parameter is set in a config below

[root@linuxprobe~]# grep "^maxclassrepeat" /etc/security/pwquality.conf

maxclassrepeat = 4

2.7 在新密码中至少需要一个小写字符。

[root@linuxprobe~]# authconfig --enablereqlower --update

# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)

[root@linuxprobe~]# grep "^lcredit" /etc/security/pwquality.conf

lcredit = -1

2.8 在新密码中至少需要一个大写字符

[root@linuxprobe~]# authconfig --enablerequpper --update

# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)

[root@linuxprobe~]# grep "^ucredit" /etc/security/pwquality.conf

ucredit = -1

2.9 在新密码中至少需要一个数字

[root@linuxprobe~]# authconfig --enablereqdigit --update

# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)

[root@linuxprobe~]# grep "^dcredit" /etc/security/pwquality.conf

dcredit = -1

2.10 密码包括至少一个特殊字符

[root@linuxprobe~]# authconfig --enablereqother --update

# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)

[root@linuxprobe~]# grep "^ocredit" /etc/security/pwquality.conf

ocredit = -1

2.11 在新密码中设置单调字符序列的最大长度。(ex?'12345','fedcb')

[root@linuxprobe~]# vi /etc/security/pwquality.conf

# add to the end

maxsequence = 3

2.12 设置新密码中不能出现在旧密码中的字符数

[root@linuxprobe~]# vi /etc/security/pwquality.conf

# add to the end

difok = 5

2.13  检查来自用户passwd条目的GECOS字段的长度超过3个字符的字是否包含在新密码中。

[root@linuxprobe~]# vi /etc/security/pwquality.conf

# add to the end

gecoscheck = 1

2.14 设置不能包含在密码中的Ssace分隔的单词列表

[root@linuxprobe~]# vi /etc/security/pwquality.conf

# add to the end

badwords = denywords1 denywords2 denywords3

2.15 为新密码设置hash / crypt算法。 (默认为sha512)

# show current algorithm

[root@linuxprobe~]# authconfig --test | grep hashing

password hashing algorithm is md5

# chnage algorithm to sha512

[root@linuxprobe~]# authconfig --passalgo=sha512 --update

[root@linuxprobe~]# authconfig --test | grep hashing

password hashing algorithm is sha512

3、历史命令限制

 (1)减少历史的命令条数

     ●  方法一:vi 编辑 /etc/profile 文件,修改 “ HISTSIZE = ” 后面的数值   

[root@www ~]#source  /etc/profile         使 /etc/profile 配置文件生效,此时全局生效

ps:查看历史命令条数   echo $HISTSIZE     历史命令调用    history (!+数字序列号 或 !+命令 或!+命令首字符)      Ctrl + R   

[root@FNSHB109 ~]# cat /etc/profile

pathmunge函数的作用是将一个路径添加到PATH变量中,如果PATH变量中已有此路径则不作操作

pathmunge () {
    case ": ${PATH}:" in
        *:"$1":*)
           ;;
        *)
          if [ "$2" = "after" ] ; then
          PATH=$PATH:$1
          else
          PATH=$1:$PATH
          fi
   esac
}

设置用户的EUID, 标识实际的登录用户ID, 对于root用户, id默认为0

if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
       # ksh workaround
       EUID=`id -u`
       UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
MAIL
="/var/spool/mail/$USER"
fi

如果是root用户,则将/usr/sbin /usr/local/sbin添加到PATH变量中

if [ "$EUID" = "0" ]; then
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
   else
        pathmunge /usr/local/sbin after
        pathmunge /usr/sbin after
   fi

初始化HOSTNAME

HOSTNAME=`/usr/bin/hostname 2>/dev/null`

当输入history命令时,最多显示多少命令

HISTSIZE=1000

if [ "$HISTCONTROL" = "ignorespace" ] ; then
       export HISTCONTROL=ignoreboth #在不想被记住的命令前面输入一个空格,该命令就不会出现在历史记录中
  else
      export HISTCONTROL=ignoredups #剔除历史记录的命令中连续的重复条目
  fi

将上边初始化的几个变量设为环境变量

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

将上边初始加粗样式化的几个变量设为环境变量

if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002 普通用户umask
    else
       umask 022 root用户umask
    fi

对/etc/profile.d/下.sh结尾的文件进行循环

for i in /etc/profile.d/*.sh ; do
      if [ -r "$i" ]; then 如果该文件可读
          if [ "${-#*i}" != "$-" ]; then 如果$-变量中包含i
            . "$i" 使用当前SHELL直接执行该文件
          else
            . "$i" >/dev/null 否则将执行结果抛弃
         fi
      fi
   done

unset i 最后删除此脚本中用到的变量和函数
unset -f pathmunge
export LANG=C 和语言相关的环境变量

PATH: 决定了shell将到哪些目录中寻找命令或程序

export PATH=/home/buildmgr/bin5.6/mercurial-scripts/bin:/ap/local/devtools/bin:$PATH 
export HGHOME=/home/buildmgr/bin5.6
export GHS_LMHOST=@fnlicsv2,asblx18,fnlx14
export GHS_LMWHICH=ghs
fi

     ●  方法二:export HISTSIZE = 数值       仅当前用户环境下生效

 (2)注销时自动清空历史命令

     ● 方法:vi 编辑宿主目录下的 “ .bash_logout ” 文件,添加 “ history -c”

ps:清空配置文件 .bash_history       方法一: >.bash_history

                                                        方法二:echo " " >.bash_history

                                                        方法三: vi 进入 " .bash_history " 文件,输入 “ dG ”

4、终端自动注销

(1)方法一:vi 编辑 /etc/profile 文件,添加 “ TMOUT = 数值 ” 

[root@www ~]#source  /etc/profile         使 /etc/profile 配置文件生效,此时全局生效

ps:source = .      unset TMOUT   注销命令

(2)方法二:export TMOUT = 数值     仅当前用户环境下生效

 

 
posted on 2019-08-09 19:50  玉树临风的猪  阅读(315)  评论(0编辑  收藏  举报