今日内容:
1、高级权限 setfacl
2、sudo
su
sudo
一、高级权限setfacl
概念:可以更精确的控制权限的分配
PS:chmod命令可以把权限分为u,g,o三个组,而setfacl可以对每一个文件、目录设置更精确的文件权限,可针对单一用户、单一文件、单一目录来进行r,w,x的权限控制,对需要特殊权限的使用状况有一定帮助。
[[email protected] ~]# getfacl 1.txt #查看用户、组、其他人对文件的权限 # file: 1.txt # owner: root # group: root user::rw- group::r-- other::r-- 附加参考 umask #设置新文件的默认权限 umask u=rwx g=r o=w 2.txt
1、setfacl 主要将权限细化管理
1>通常先把other权限置空 setfacl -m o::- ;
2>再来针对用户单独设置权限,就不会两边的权限混到一起(用户本身权限、默认配置权限)
权限优先级:先看单独配置权限>>>再看默认配置权限 选项 例:setfacl -m d:u::rw a.txt -m #对属主或属组或其他用户,修改对文件的某个权限 -g # 后续选项 d #递归,继承,针对目录,目录下的子文件都有该目录的权限 修改属主的权限 setfacl -m u::权限 a.txt 修改属组的权限 setfacl -m g::权限 a.txt 修改其他人的权限 setfacl -m o::权限 a.txt 修改具体某一个用户的权限 setfacl -m u:用户名:权限 a.txt 修改具体某一个组的权限 setfacl -m g:组名:权限 a.txt
应用场景练习题:
前提:创建需要的用户egon01、egon02,1.txt文件
1、其他人对文件没有任何权限,然后单独设置其他人里的egon01对文件有r权限、egon02对文件有w权限 setfacl -m o::- 1.txt setfacl -m u:egon01::r 1.txt setfacl -m u:egon02::w 1.txt 2、其他人对文件有rw权限,然后单独设置其他人里的egon01对文件有r权限、egon02对文件有w权限 setfacl -m o::rw 1.txt setfacl -m u:egon01::r 1.txt setfacl -m u:egon02::w 1.txt
实验演示:
2、查看、改变文件属性
1>lsattr #用于查看文件属性 格式 lsattr 1.txt 2>chattr #用于改变文件属性 选项 +a #只允许追加内容 +i #禁止任何修改 #常用 +A #不更改访问时间 例: [[email protected] home]# touch /opt/{1..3}.txt [[email protected] home]# lsattr /opt/ ---------------- /opt/1.txt ---------------- /opt/2.txt ---------------- /opt/3.txt [[email protected] home]# chattr +a /opt/1.txt [[email protected] home]# chattr +i /opt/2.txt [[email protected] home]# chattr +A /opt/3.txt [[email protected] home]# lsattr /opt/ -----a---------- /opt/1.txt ----i----------- /opt/2.txt -------A-------- /opt/3.txt
二、su与sudo
su 概念:切换用户身份,从一个用户切换到另一个用户
PS:推荐用 su - 来切换 su 与 su - 最大的本质区别就是: su :即非登录式,是切换了root身份,但Shell环境仍然是普通用户的Shell; su -:即登录式,则连用户和Shell环境一起切换成root身份了; 只有切换了Shell环境才不会出现PATH环境变量错误!
1、切换登录级别方式 1>登录式shell**
输入账号密码登录 su - egon #登录式shell 登录式shell优先级: /etc/profile--->/etc/profile.d/*.sh--->~/.bash_profile--->~/.bashrc--->/etc/bashrc 1>输入账号密码登录进来; 2>管理员root切换到普通用户下无需密码,反之需要
2>非登录式shell
登录式shell优先级: ~/.bashrc---->/etc/bashrc----->/etc/profile.d/*.sh su egon #非登录式shell 交互式 非交互式 exprofile #vim编辑完在末行添加,环境变量全局生效
如果想针对所有用户以及登录与非登录shell设置统一的配置-,则编辑/etc/bashrc文:
[[email protected] ~]# vim /etc/bashrc [[email protected] ~]# source /etc/bashrc # 当前环境让该文件生效(或重启也可生效) PS:/etc/profile------------->此配置文件为:更改登录级别的所有用户
ifconfig ens33:0 1.1.1.1 制作一块虚拟网卡
二、sudo提权
概念:sudo是在普通用户身份不变的情况下,可以执行root管理员的部分权限,而不需知道root管理员的密码,起到提权的作用。
格式 sudo cat /root/1.txt sudo -k 清除缓存密码,再登录时需再输密码
配置的两种方式:
方式1>visudo
在管理员下修改配置文件/etc/sudoers来分配权限 vim /etc/sudoers # 风险高 visudo -c # 修改完后检查语法是否有错误,visudo回车,不需要加文件路径
方式2>vim /etc/sudoers
[[email protected] ~]# cat /etc/sudoers ## Sudoers allows particular users to run various commands as ## the root user, without needing the root password. 该文件允许特定用户像root用户一样使用各种各样的命令,而不需要root用户的密码 ## 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. 该文件必须使用"visudo"命令编辑 ## Host Aliases 主机别名 ## Groups of machines. You may prefer to use hostnames (perhap using ## wildcards for entire domains) or IP addresses instead. 对于一组服务器,你可能会更喜欢使用主机名(可能是全域名的通配符)或IP地址代替,这时可以配置主机别名. # 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... 指定一系列相互关联的命令(当然可以是一个)的别名,通过赋予该别名sudo权限, 可以通过sudo调用所有别名包含的命令,下面是一些示例 ## 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 ## Services 服务相关命令别名 Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig ## Updating the locate database 本地数据库升级命令别名 Cmnd_Alias LOCATE = /usr/sbin/updatedb ## Storage 磁盘操作相关命令别名 Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount ## Delegating permissions 代理权限相关命令别名 Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp ## Processes 进程相关命令别名 Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall ## Drivers 驱动命令别名 Cmnd_Alias DRIVERS = /sbin/modprobe 环境变量的相关配置 # Defaults specification # # Disable "ssh hostname sudo <cmd>", because it will show the password in clear. # You have to run "ssh -t hostname sudo <cmd>". # Defaults requiretty Defaults env_reset Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \ LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \ LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \ LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \ LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \ _XKB_CHARSET XAUTHORITY" ## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems). 下面是规则配置:什么用户在哪台服务器上可以执行哪些命令(sudoers文件可以在多个系统上共享) ## Syntax: 语法 ## user MACHINE=COMMANDS 用户 登录的主机=(可以变换的身份) 可以执行的命令 ## The COMMANDS section may have other options added to it. 命令部分可以附带一些其它的选项 ## Allow root to run any commands anywhere 允许root用户执行任意路径下的任意命令 root ALL=(ALL) ALL ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. 允许sys中户组中的用户使用NETWORKING等所有别名中配置的命令 # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ## Allows people in group wheel to run all commands 允许wheel用户组中的用户执行所有命令 # %wheel ALL=(ALL) ALL ## Same thing without a password 允许wheel用户组中的用户在不输入该用户的密码的情况下使用所有命令 %wheel ALL=(ALL) NOPASSWD: ALL ## Allows members of the users group to mount and unmount the ## cdrom as root 允许users用户组中的用户像root用户一样使用mount、unmount、chrom命令 %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom ## Allows members of the users group to shutdown this system 允许users用户组中的用户像root用户一样使用shutdown命令 # %users localhost=/sbin/shutdown -h now
3、su与sudo的优缺点总结
su 特点:直接切换到root账号下进行操作,输入的是root的密码; 缺点:root密码泄露,普通用户获取了所有管理权限; 优点:简单粗暴; sudo 特点:在当前普通用户下操作,无需切换到root下,输入的是普通用户自己的密码; 缺点:相对复杂; 优点:root密码没有泄露,普通用户获取了部分管理权限;