Linux基础——系统安全及应用

系统安全及应用

系统账号清理

  • 将非登录用户的shell设为/sbin/nologin
    usermod -s /sbin/nologin在这里插入图片描述

  • 锁定长期不使用账号
    usermod -L 用户名或passwd -l 用户名
    passwd -S 用户名

  • 删除无用账号
    在这里插入图片描述

  • 锁定账号文件passwd 、shadow
    chattr +i /etc/passwd /etc/shadow ## 锁定文件并查看状态
    Isattr /etc/passwd /etc/shadow
    chattr -i /etc/passwd /etc/shadow ## 解锁文件
    在这里插入图片描述

  • 检查文件是否改变md5sum
    基本格式:md5sum 目标文件
    在这里插入图片描述

密码安全控制

设置密码有效期
chage -M 天数 用户 ## 适用于已有用户
vim /etc/login.defs ## 修改密码配置文件
PASS_MAX_DAYS 天数 ## 适用于新建用户
强制在下次登录时更改密码
chage -d 0 用户
在这里插入图片描述

命令历史限制

history                    ## 查看历史命令
vim /etc/profile            ## 进入配置文件
export HISTSIZE=5          ## 修改记录的命令条数
source /etc/profile        ## 加载文件
history -c                 ## 清空历史命令 (临时)
登录时自动清空命令历史
vi -/bashrc                ## 通过设置开机自启动清空

在这里插入图片描述

终端自动注销

vim /etc/profile
export TMOUT=时长  
source /etc/profile      ## 加载文件

账号切换

使用su命令切换用户

用途: 使用su命令,可以切换指定的另一个用户,从而具有该用户的所有权限。
格式: su - 目标用户

密码验证
root>任意用户,不验证密码
普通用户>其他用户,验证目标用户的密码

[root@localhost ~]# su zhangsan
su: user zhangsan does not exist
[root@localhost ~]# useradd zhangsan
[root@localhost ~]# suzhangsan
bash: suzhangsan: 未找到命令...
[root@localhost ~]# su zhangsan
[zhangsan@localhost root]$ su root
密码:

限制使用su命令的用户

将允许使用su命令的用户加入wheel组
启用pam_wheel认证模块

[zhangsan@localhost root]$
[zhangsan@localhost root]$ exit
exit
[root@localhost ~]# gpasswd -a zhangsan wheel
正在将用户“zhangsan”加入到“wheel”组中
[root@localhost ~]# su zhangsan
[zhangsan@localhost root]$ su root
密码:

在这里插入图片描述

使用sudo机制提升权限

定义:sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登录 和管理时间,同样也提高了安全性
语法格式:sudo [选项] [-u 新使用者账号] 要执行的命令

sudo命的用途及用法

用途:以其他用户身份 (如root) 执行授权的命令
用法:sudo 授权命令
查询授权的sudo:sudo -l

配置sudo授权

visudo或者vi /etc/sudoers
记录格式:用户名 主机名=命令程序列表 
         user MACHINE=COMMANDS
用户名(user):授权的用户名,或采用 "%组名" 的形式(授权一个组的所有用户)
主机名(MACHINE):使用此配置文件的主机名称
命令程序列表(COMMANDS):允许授权的用户通过 sudo 方式执行的特权命令,需要填写命令程序的完整路径,多个命令之间以逗号 ‘,’ 进行分隔

别名创建
User_Alias 大写别名=用户1,用户2...                              ## 配置用户别名
Host_Alias 大写别名=主机名1,主机名2...                           ## 配置主机别名
Cmnd_Alias 大写别名=程序1,程序2...                              ## 配置程序别名
用户名/用户别名 主机名/主机别名/all=程序列表/程序别名/all            ## 设置用户授权
%组名 主机名/主机别名/all=[(用户)NOPASSWD:]程序列表/程序别名/all      ## 设置组授权

## 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...

实战操作

授权 zhangsan 用户能够执行 ifconfig 命令来修改 ip 地址,而 wheel 组的用户不需要验证密码就可以执行任何命令

在这里插入图片描述

[root@localhost ~]# su zhangsan
[zhangsan@localhost root]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.190.10  netmask 255.255.255.0  broadcast 192.168.190.255
        inet6 fe80::1d4a:f152:57fc:ba1d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:73:3c:ba  txqueuelen 1000  (Ethernet)
        RX packets 5668  bytes 534775 (522.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3305  bytes 492185 (480.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 76  bytes 6612 (6.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 76  bytes 6612 (6.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:b5:d7:70  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[zhangsan@localhost root]$ ifconfig ens33:0 192.168.190.60/24
SIOCSIFADDR: 不允许的操作
SIOCSIFFLAGS: 不允许的操作
SIOCSIFNETMASK: 不允许的操作
[zhangsan@localhost root]$ sudo ifconfig ens33:0 192.168.190.60/24

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:

    #1) 尊重别人的隐私。
    #2) 输入前要先考虑(后果和风险)。
    #3) 权力越大,责任越大。

[sudo] zhangsan 的密码:
[zhangsan@localhost root]$ exit
exit
[root@localhost ~]# vim /etc/sudo
[root@localhost ~]# vim /etc/sudoers
[root@localhost ~]# vim /etc/sudoers

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值