一、账号安全的基本措施
系统账号清理
- 将用户设置为无法登录/sbin/nologin
[root@localhost ~]# useradd lisi #新建lisi用户
[root@localhost ~]# cat /etc/passwd |grep lisi #查看lisi用户信息,当前为“/bin/login"
lisi:x:1001:1001::/home/lisi:/bin/bash
[root@localhost ~]# usermod -s /sbin/login lisi #将lisi用户设置为无法登录
[root@localhost ~]# cat /etc/passwd |grep lisi #再次查看lisi用户信息,变为”/sbin/nologin
lisi:x:1001:1001::/home/lisi:/sbin/login
- 删除用户
[root@localhost ~]# useradd wangwu #新建wangwu用户
[root@localhost ~]# cat /etc/passwd |grep wangwu #查看wangwu用户信息
wangwu:x:1002:1002::/home/wangwu:/bin/bash
[root@localhost ~]# userdel -r wangwu #删除wangwu用户及家目录
[root@localhost ~]# cat /etc/passwd |grep wangwu #再次查看wangwu用户已不存在
- 锁定用户
[root@localhost ~]# cat /etc/passwd |grep zhaosi
zhaosi:x:1002:1002::/home/zhaosi:/bin/bash
[root@localhost ~]# usermod -L zhaosi
[root@localhost ~]# passwd zhaosi
更改用户 zhaosi 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。 #切换至虚拟机登录发现输入正确密码后仍然无法登录zhaosi
- chattr锁定文件/文件夹
[root@localhost /]# mkdir data #新建一个文件夹
[root@localhost /]# chattr +i /data #锁定这个文件夹
[root@localhost /]# cd data #进入文件夹
[root@localhost data]# touch 1 #创建一个文件,发现无法创建
touch: 无法创建"1": 权限不够
[root@localhost /]# rm -rf data #也无法删除该文件夹
rm: 无法删除"data": 不允许的操作
-
chattr解锁文件/文件夹
[root@localhost /]# chattr -i /data #解锁data文件夹 [root@localhost /]# ls bin data etc lib media opt root sbin sys usr #查看根目录下存在data文件夹 boot dev home lib64 mnt proc run srv tmp var [root@localhost /]# cd data #进入data文件夹 [root@localhost data]# touch 1 #成功创建文件 [root@localhost data]# cd / #返回根目录 [root@localhost /]# rm -rf data #删除data文件夹 [root@localhost /]# ls #发现根目录下data被成功删除 bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
二、密码安全
对新建用户设置密码规则
[root@localhost /]# vim /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:指定用户密码的最小长度
- PASS_WARN_AGE:指定在密码过期之前的多少天开始向用户发出警告
对已有用户设置密码规则
chage [选项] 用户名
-m:密码可更改的最小天数,m=0代表任何时候都可以更改密码
-M:密码保持有效的最大天数
-w:用户密码保持有效的最大天数
-E:账号到期的日期。显示的日期就是账号不可用的日期
-d:上一次更改密码的日期
-i:停滞时间,如果一个密码已过期,那么此账号将不可用
-l:列出当前的设置,由非特权用户来确定他们的密码或账号何时过期
[root@localhost /]# chage lisi #单独输入chage命令和用户名可以进入交互界面
正在为 lisi 修改年龄信息
请输入新值,或直接敲回车键以使用默认值
最小密码年龄 [0]:
最大密码年龄 [99999]:
最近一次密码修改时间 (YYYY-MM-DD) [2024-04-18]:
密码过期警告 [7]:
密码失效 [-1]:
帐户过期时间 (YYYY-MM-DD) [-1]:
[root@localhost /]# chage -d 0 lisi #强制指定用户每一次登录要修改密码
自动注销
[root@localhost /]vim etc/profile #对所有用户生效
export TMOUT=60 #设置为60s后自动注销账户
三、命令历史
- 临时清除历史命令
[root@localhost /]# history #历史命令
1 vim /etc/sysconfig/network-scripts/ifcfg-ens33
2 ping 192.168.137.100
3 ip add
4 vim /etc/sysconfig/network-scripts/ifcfg-ens33
5 systemctl restart network.service
6 lsblk
...
[root@localhost /]# history -c #临时清除历史命令
[root@localhost /]# history #命令清除成功
1 history
- 设置历史命令保存数量
[root@localhost /]# vim /etc/profile #进入配置文件
.
. #其他配置信息
.
export HISTSIZE=200 #设置历史命令最大保存为200条
wq
. /etc/profile #刷新profile配置文件
- 退出bash清空历史命令
[root@localhost ~]# history #提前查看history
1
2 history
3 vim /etc/profile
4 . /etc/profile
5 vim /etc/profile
[root@localhost ~]# vim .bash_logout #编辑配置文件
# ~/.bash_logout
echo " " >~/.bash_history #退出后清空
wq #保存退出
[root@localhost ~]# history #再次查看发现历史记录已清空
1
2 history
- 开机后清除历史命令
[root@localhost ~]# bash #新建一个bash(桌面)
[root@localhost ~]# vim .bashrc #编辑bashrc配置文件
# .bashrc
echo " ">~/.bash_history #开机后清除
wq
[root@localhost ~]# history #未关机时查看history
1
2 history
3 vim .bash_logout
4 exit
5 vim .bashrc
6 history
[root@localhost ~]# exit #关闭当前bash
exit
[root@localhost ~]# bash #再次打开bash
[root@localhost ~]# history #发现历史命令被清空
1
2 history
四、切换用户
SU
- su命令即 switch user,命令可以切换用户身份,并且以指定用户的身份执行命令
su [options...] [-] [user [args...]]
- 切换用户的方式:
su 用户名:非登录式切换,即不完全切换 ,这会导致某些命令运行出现问题或错误(例如无法使用 service 命令)
su - 用户名:登录式切换,即完全切换
[root@localhost ~]# su zhangsan
[zhangsan@localhost root]$ exit
exit
[root@localhost ~]# su - zhangsan
上一次登录:四 4月 18 16:30:13 CST 2024pts/1 上
[zhangsan@localhost ~]$ exit
登出
- 超级管理员使用su切换至其他用户无须密码
[root@localhost ~]# su zhangsan
[zhangsan@localhost root]$ exit
exit
- 非超级管理员用户切换至其他用户时需要密码
[zhangsan@localhost root]$ su
密码:
注意:su 切换新用户后,使用 exit 退回至旧的用户身份,而不要再用 su 切换至旧用户,否则会生成很多的bash子进程,环境可能会混乱。
sudo
- 限制使用su命令的用户
[root@localhost ~]# gpasswd -a zhangsan wheel #将用户“zhangsan”加入到“wheel”组中
[root@localhost ~]# cd /etc/pam.d/
[root@localhost pam.d]# ls
atd gdm-pin postlogin-ac su
chfn gdm-smartcard ppp sudo
chsh ksu remote sudo-i
config-util liveinst runuser su-l
crond login runuser-l system-auth
cups other setup system-auth-ac
fingerprint-auth passwd smartcard-auth systemd-user
fingerprint-auth-ac password-auth smartcard-auth-ac vlock
gdm-autologin password-auth-ac smtp vmtoolsd
gdm-fingerprint pluto smtp.postfix xserver
gdm-launch-environment polkit-1 sshd
gdm-password postlogin sssd-shadowutils
[root@localhost /]# vim etc/pam.d/su
2 #auth required pam_wheel.so use_uid
#开启PAM安全认证,控制su命令使用规则
[root@localhost /]# su - lisi
[lisi@localhost ~]$ su root
密码:
五、PAM安全认证
PAM:Pluggable Authentication Modules,插件式的验证模块,Sun公司于1995 年开发的一种与认证相关的通用框架机制。PAM 只关注如何为服务验证用户的 API,通过提供一些动态链接库和一套统一的API 接口,将系统提供的服务和该服务的认证方式分开,使得系统管理员可以灵活地根据需要给不同的服务配置不同的认证方式而无需更改服务程序一种认证框架,自身不做认证。
- 如何查看PAM版本
[root@localhost ~]# rpm -qi pam
Name : pam
Version : 1.1.8
Release : 18.el7
Architecture: x86_64
Install Date: 2024年03月28日 星期四 09时39分35秒
Group : System Environment/Base
Size : 2625254
License : BSD and GPLv2+
Signature : RSA/SHA256, 2016年11月21日 星期一 03时52分37秒, Key ID 24c6a8a7f4a80eb5
Source RPM : pam-1.1.8-18.el7.src.rpm
Build Date : 2016年11月06日 星期日 07时14分20秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.linux-pam.org/
Summary : An extensible library which provides authentication for applications
Description :
PAM (Pluggable Authentication Modules) is a system security tool that
allows system administrators to set authentication policy without
having to recompile programs that handle authentication.
- 如何查看PAM模块
[root@localhost ~]# rpm -ql pam
/etc/pam.d
/etc/pam.d/config-util
/etc/pam.d/fingerprint-auth
/etc/pam.d/other
/etc/pam.d/password-auth
/etc/pam.d/postlogin
/etc/pam.d/smartcard-auth
/etc/pam.d/system-auth
/etc/security
/etc/security/access.conf
/etc/security/chroot.conf
/etc/security/console.apps
- centos7中PAM配置文件存放的位置
绝对路径:/etc/pam.d/
[root@localhost ~]# cd /etc/pam.d
[root@localhost pam.d]# ls
atd gdm-pin postlogin-ac su
chfn gdm-smartcard ppp sudo
chsh ksu remote sudo-i
config-util liveinst runuser su-l
crond login runuser-l system-auth
cups other setup system-auth-ac
fingerprint-auth passwd smartcard-auth systemd-user
fingerprint-auth-ac password-auth smartcard-auth-ac vlock
gdm-autologin password-auth-ac smtp vmtoolsd
gdm-fingerprint pluto smtp.postfix xserver
gdm-launch-environment polkit-1 sshd
gdm-password postlogin sssd-shadowutils
- 这里的文件是应用程序怎么调用模块的
[root@localhost pam.d]# cat su
#%PAM-1.0
#auth required pam_wheel.so use_uid
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
#auth required pam_wheel.so use_uid
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
- 第一列type:指模块类型,即功能
- 第二列控制位:PAM库该如何处理与该服务相关的PAM模块的成功或失败情况,一个关健词实现
- 第三列模块: 用来指明本模块对应的程序文件的路径名
- 第四列模块的参数: 用来传递给该模块的参数
-
模块类型:
- Auth 账号的认证和授权
- Account 帐户的有效性,与账号管理相关的非认证类的功能,如:用来限制/允许用户对某个服务的访问时间,限制用户的位置(例如:root用户只能从控制台登录)
- Password 用户修改密码时密码复杂度检查机制等功能
- Session 用户会话期间的控制,如:最多打开的文件数,最多的进程数等
- -type 表示因为缺失而不能加载的模块将不记录到系统日志,对于那些不总是安装在系统上的模块有用
-
控制位:
- required :一票否决,表示本模块必须返回成功才能通过认证,但是如果该模块返回失败,失败结果也不会立即通知用户,而是要等到同一type中的所有模块全部执行完毕,再将失败结果返回给应用程序,即为必要条件
- requisite :一票否决,该模块必须返回成功才能通过认证,但是一旦该模块返回失败,将不再执行同一type内的任何模块,而是直接将控制权返回给应用程序。是一个必要条件
- sufficient :一票通过,表明本模块返回成功则通过身份认证的要求,不必再执行同一type内的其它模块,但如果本模块返回失败可忽略,即为充分条件,优先于前面的
- equired和requisiteoptional :表明本模块是可选的,它的成功与否不会对身份认证起关键作用,其返回值一般被忽略include: 调用其他的配置文件中定义的配置
- optional 可选项
- PAM模块:
默认在/lib64/security/目录下,如不在需要填写绝对路径。
- shell模块:
功能:检查有效shell
帮助:man pam_shells
案例:不允许使用/bin/csh的用户本地登录
模块: pam_shells.so 只允许 规定的shell类型通过, 是在/etc/shells 文件中存在的 类型通过
[root@localhost /]# vim /etc/pam.d/su
#%PAM-1.0
auth required pam_shells.so #添加本行
#auth required pam_wheel.so use_uid
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
#auth required pam_wheel.so use_uid
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
wq
[root@localhost /]# vim /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh #删除本行
wq
[root@localhost /]# useradd -s /bin/csh lisi
[root@localhost /]# su lisi
密码:
su: 鉴定故障
[root@localhost /]# tail /var/log/secure
Apr 21 22:49:09 localhost passwd: gkr-pam: couldn't update the login keyring password: no old password was entered
- pam_nologin.so模块
功能:如果/etc/nologin文件存在,将导致非root用户不能登陆,当该用户登陆时,会显示/etc/nologin文件内容,并拒绝登陆
范例: 默认此模块可以对ssh等登录有效,但不影响su登录
[root@centos7 pam.d]#grep pam_nologin *
login:account required pam_nologin.so
remote:account required pam_nologin.so
sshd:account required pam_nologin.so
-
limits
- 第一列domain:用户名称,*代表所有用户
- 第二列type:限制类型,包括hard(硬限制),soft(软限制)
- 第三列item:控制类型,比如core,cpu,data,nofile,fsize等
- 第四列value:设置限制的参数值
[root@localhost /]# cd /etc/security/
[root@localhost security]# ls
access.conf console.perms limits.d opasswd time.conf
chroot.conf console.perms.d namespace.conf pam_env.conf
console.apps group.conf namespace.d pwquality.conf
console.handlers limits.conf namespace.init sepermit.conf
[root@localhost security]# cat limits.conf #/etc/security/limits.conf为默认路径
# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
#
#Where:
#<domain> can be: #限制用户名
# - a user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
# - the wildcard %, can be also used with %group syntax,
# for maxlogin limit
#
#<type> can have the two values: #限制类型
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
#<item> can be one of the following: #控制类型
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open file descriptors
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit (KB)
# - maxlogins - max number of logins for this user
# - maxsyslogins - max number of logins on the system
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
# - sigpending - max number of pending signals
# - msgqueue - max memory used by POSIX message queues (bytes)
# - nice - max nice priority allowed to raise to values: [-20, 19]
# - rtprio - max realtime priority
#
#<domain> <type> <item> <value>
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
# End of file
- sudo
sudo 即superuser do
sudo特性:
- sudo能够授权指定用户在指定主机上运行某些命令。如果未授权用户尝试使用 sudo,会提示联系管理员
- sudo提供了丰富的日志,详细地记录了每个用户干了什么。它能够将日志传到中心主机或者日志服务器
- sudo使用时间戳文件来执行类似的“检票”系统。当用户调用sudo并且输入它的密码时,用户获得了一张存活期为5分钟的票
- sudo的配置文件是sudoers文件,它允许系统管理员集中的管理用户的使用权限和使用的主机。它所存放的位置默认是在/etc/sudoers,属性必须为0440
[root@localhost /]# vim /etc/sudoers
[root@localhost /]# visudo
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias FILESERVERS = fs1, fs2
# Host_Alias MAILSERVERS = smtp, smtp2
## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem
## Command Aliases
## These are groups of related commands...
## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool
## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
"/etc/sudoers.tmp" 112L, 3938C
#相较vim /etc/sudoers,visudo可以检查语法错误,而vim无法检查语法,且当发生错误时保存该文档将导致系统错误!
#visudo -c用于直接检查语法
配置文件格式说明:/etc/sudoers, /etc/sudoers.d/
- grub加密
[root@localhost ~]#grub2-setpassword
#直接设置密码