Linux基础知识part2

继续上一节,这次主要是用户和权限的一些知识点。

命令创建用户:useradd

手动创建用户:

/etc/passwd

/etc/shadow

/etc/gshadow

/home/用户名/

/var/spool/mail

id user 查看user用户个人信息。

用户家目录的隐藏文件模板  /etc/skel

ls -ld 查看目录详细信息

chown -R oldboy:oldboy /home/oldboy 递归更改

家目录下环境配置文件
env 查询环境变量
set 查询所有变量
环境变量可以被子进程继承 export b=2
自定义变量不能被子进程继承 a=1

环境配置文件(每个用户家目录下都存在)
.bash_history 保存用户的历史命令,当用户退出时保存到文件
.bash_logout 保存用户退出时执行的命令
.bashrc 保存用户别名 (开启新的终端就可以读取该文件)
.bash_profile 保存用户搜索命令的位置

.bashrc shell级别环境变量配置文件
.bash_profile 登录级别环境变量配置文件

su - 登录级别切换
su shell级别切换

全局环境配置文件
/etc/bashrc
/etc/profile

加载顺序
/etc/profile
.bash_profile
.bashrc
/etc/bashrc

定义变量文件顺序
/etc/profile1
/etc/profile2
.bash_profile1
.bashrc1
/etc/bashrc1
/etc/bashrc2
.bashrc2
.bash_profile2

权限(基本权限和高级权限)
[root@localhost ~]# ls -l install.log
-rw-r--r-- 1 robin upup 39751 05-23 11:15 aa.txt
rwx | rwx | rwx
robin upup other
uid=0----->uid=robin----->group=upup----->other

对于文件 r--------cat head tail....
w--------vim,gedit,vi,> >>
x--------执行./ 绝对路径
对于目录 r--------ls r-x查看目录下文件信息
w--------mkdir touhc rm rwx
x--------cd
user u
group g
other o

chmod u+r file/dir
chmod u+r,g+w file/dir
chmod ug-rw file/dir
chmod ugo+rwx file/dir
chmod a+rwx file/dir

chmod u=r file/dir
chmod u=r,g=w file/dir
chmod uo=rw file/dir
chmod ugo=rwx file/dir
chmod a=rwx file/dir

/ rwxr-xr-x 755
/etc/ rwxr-xr-x 755
/usr/ rwxr-xr-x 755
/var/ rwxr-xr-x 755
/tmp rwxrwxrwt 1777
/etc/shadow r-------- 400
/etc/passwd rw-r--r-- 644
/home/robin rwx------ 700
/root rwxr-x--- 750
root创建文件默认权限 rw-r--r-- 644
root创建目录默认权限 rwxr-xr-x 755
普通用户创建文件默认权限 rw-rw-r-- 664
普通用户创建目录默认权限 rwxrwxr-x 775

r------4 w------2 x-----1
777
rwxr----x 741 123--x-w--wx
r-x-wx-w- 532 456r--r-xrw-

默认权限计算方法
666 023
110110110 000010011
111101100 111101100
110100100
644

chmod -R 777 /test

修改拥有者所属组
chown robin tt.txt
chgrp uplooking tt.txt
chown robin.uplooking tt.txt
chown .uplooking tt.txt
.=:
chown -R robin.uplooking dir/

练习:
创建用户tom和jerry
1.在/tmp目录下创建文件tt.txt
要求:
tt.txt权限设置为651 在改为rwxr----x
chmod 651 /tmp/tt.txt
chmod u=rwx,g=r,o=x /tmp/tt.txt

设置tt.txt文件拥有这tom所属组jerry
chown tom.jerry /tmp/tt.txt

2.根据以下要求设置拥有者和权限
在/tmp目录下创建目录test/,在test目录下创建文件aa.txt
tom可以读写文件aa.txt 不能删除
jerry 不可以读写文件aa.txt 可以删除

mkdir /tmp/test/
touch /tmp/test/aa.txt

/tmp/test/ jerry root rwxr-xr-x
/tmp/test/aa.txt tom root rw-r-----

主组附加组
robin robin robin,uplooking
/tmp/test rwxrwx--- root uplooking
robin能不能进去/tmp/test? 为什么?
可以 因为附加组有uplooking
进入后创建文件aa.txt 该文件 拥有者 所属组??
robin robin
如果该用户想创建属于uplooking组的文件 怎么做?
sg uplooking
robin uplooking robin,uplooking
tt.txt robin uplooking

高级权限
1.suid
必须作用于二进制可执行文件
chmod u+s file
作用:任何用户在执行拥有suid权限的命令时,都以该命令拥有者的身份执行

实验:
在不改变/etc/shadow权限的前提下,使普通用户robin可以使用cat查看/etc/shadow文件内容
which cat
chmod u+s /bin/cat
robin用户测试

2.sgid
必须作用于目录
chmod g+s dir
作用:任何用户在拥有sgid的目录下创建的文件,都要继承该目录的组

useradd -g job -G uplooking tom
useradd -g job -G uplooking jerry
useradd -g sal -G uplooking zorro
useradd -g sal -G uplooking shrek

/job zhb uplooking rwxrwsr-x

aa.txt tom job
tt.txt tom uplooking rw-r-----


3.sticky(t 冒险位 粘贴位)
必须作用于目录
chmod o+t dir
作用:任何用户在拥有t权限的目录下创建的文件,都属于该用户自己,其他人无权删除(root,目录拥有者)

robin:$1$Bj0Exbfx$e24mczi0jmsiDR:15847:2:10:3:2::

第一列:用户名
第二列:密码(md5)
第三列:密码最后一次修改时间(天)
第四列:密码最小时间
第五列:密码最大时间(密码有效期)(天)
第六列:密码过期前警告时间
第七列:密码过期后帐号过期时间
第八列:帐号有效期
第九列:保留列

练习:
1.添加组kkk gid为1500
groupadd -g 1500 kkk
2.添加用户 zorro uid 1500 属于kkk组 描述:hello 家目录/mnt shell:bash

useradd -u 1500 -g kkk -c hello -d /mnt/zorro -s /bin/bash zorro

3.通过管理员设置该用户密码为123
终端登录zorro 设置自己密码为123.com


修改用户信息
usermod
usermod -u -g -c -d -s robin

usermod -l abc2abc abc2
usermod -L robin
usermod -U robin
usermod -f 密码过期后帐号过期时间
usermod -e 帐号有效期

修改组信息
groupmod -g 2500 uplooking
groupmod -n upup uplooking

修改密码及信息
passwd -n(最小时间) -x 最大时间 -w 警告时间 -i 密码过期后帐号过期时间

passwd -l robin 锁定密码
passwd -u robin 解锁密码
passwd -S robin  查看密码状态

添加用户tom和jerry 分别设置两个用户密码为123
锁定tom帐号 锁定jerry密码
终端登录测试 返回值是否一致?
查看shedow文件 两个用户有什么区别?
!锁定帐号
!! 锁定密码

删除用户:
userdel -r abc2
删除组
groupdel upup


附加组
groupadd yw
groupadd kf
useradd -G kf,yw wg 覆盖附加组信息
useradd -a -G kf,yw wg 追加附加组信息

groupadd boss
gpasswd boss

手动添加帐号
#####1.groupadd zu
1).vim /etc/group
zu:x:2000:

######2.useradd -g zu zhb
1).vim /etc/passwd
zhb:x:2000:2000::/home/zhb:/bin/bash
2).mkdir /home/zhb
3).cp /etc/skel/.bash* /home/zhb/

#####3.passwd zhb
1).vim /etc/shadow
zhb::15848:0:99999:7:::
2).grub-md5-crypt产生密码 将密码复制到第二列

终端登录测试 创建文件?


多用户导入
1.导入文件(user.txt)内的用户和系统的passwd文件用户信息不能冲突
2.导入文件(user.txt)内用户不能冲突

[root@localhost ~]# cat user.txt
user1:x:3000:3000:ptuser:/home/user1:/bin/bash
user2:x:3001:3001:ptuser:/home/user2:/bin/bash

newusers < user.txt

[root@localhost ~]# cat passwd.txt
user1:123
user2:123

chpasswd < passwd.txt

中天一片无情月,是我平生不悔心
原文地址:https://www.cnblogs.com/ArmoredTitan/p/6898980.html