Root用户
使用 Root 用户进行日常运维管理操作,存在极大的安全风险:
-
更容易在意外执行有高风险命令时产生不可挽回的操作;
-
需要对 Root 用户的账号信息进行分发,容易出现信息安全泄露的问题。
Sudo命令
sudo 是一个以 root 用户(或其他用户)来控制运行命令访问的程序。它可以配置为允许一个用户像 root 用户一样来运行所有的命令,或者仅仅一些命令。也可以配置为无需密码即可使用 sudo 运行命令。
使用sudo的好处
-
只在执行必要命令的时候使用 root 权限,日常运维使用其他权限,减少出现意外破坏情况的概率;
-
获取 root 权限时,用户需要输入自己的密码来进行安全校验,而不是输入 root 用户的密码来进行校验;
-
sudo 命令可以按照用户进行安全审计,避免全都使用 root 用户时审计不便的问题;
-
关闭了 root 登录并配置 sudo 的情况下,可以减少黑客对 root 用户进行定向爆破成功的可能性。
配置sudo权限
-
编写 /etc/sudoers 文件,可以编辑具体的 sudo 用户权限;
-
使用 vim /etc/sudoers 或者 visudo 命令
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
# Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
配置 sudo 用户只能执行特定的命令
-
可以执行所有命令
bestony ALL=(ALL) ALL
-
只能执行一个命令
bestony ALL=(ALL) /usr/bin/systemctl status sshd
-
可以执行一组命令
bestony ALL=(ALL) /usr/bin/systemctl * sshd
-
可以执行多个命令
bestony ALL=(ALL) /usr/bin/systemctl status sshd, /usr/bin/systemctl restart sshd
-
只能以特定用户执行命令
bestony ALL=(admin) /usr/bin/echo_help
# 执行
sudo -u admin /usr/bin/echo_help
查看当前用户 sudo 权限:sudo -l
[root@wyh ~]# sudo -l
Matching Defaults entries for root on wyh:
!visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG
LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS
_XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User root may run the following commands on wyh:
(ALL) ALL
配置密码有效期
-
使用 change -l root 查看密码有效期
[root@wyh ~]# chage -l root
Last password change : Aug 24, 2023
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
-
可以编辑 /etc/login.defs 设置密码过期时间
# Password aging controls:
#
# PASS_MAX_DAYS Maximum number of days a password may be used.
# PASS_MIN_DAYS Minimum number of days allowed between password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a password expires.
#
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
设置安全提醒信息
-
/etc/motd 文件中存储的信息将会在每一次登陆后提示,可以确保在登陆到服务器后,即可看到相关内容。
[root@wyh ~]# cat /etc/motd
Welcome to Alibaba Cloud Elastic Compute Service !
-
/etc/issue 文件中存储的信息将会在每一次登录前展示,可以将一些信息设定在其中,确保可以让用户登陆时看到。
[root@wyh ~]# cat /etc/issue
\S
Kernel \r on an \m