Linux的文件管理、重定向、用户及权限管理

网友投稿 751 2022-09-20

Linux的文件管理、重定向、用户及权限管理

Linux的文件管理、重定向、用户及权限管理

1.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

[root@hhw- etc]# touch {1..5}N62.txt #创建一个1-5开头的N62.txt文件 [root@hhw- etc]# ls -d /etc/ [^[:alpha:]][[:alpha:]]* 1N62.txt 2N62.txt 3N62.txt 4N62.txt 5N62.txt /etc/ [root@hhw- etc]#

2.复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

[root@hhw- etc]# mkdir /tmp/mytest1;cp -r /etc/p*[^0-9] /tmp/mytest1;ll /tmp/mytest1 total 32 drwxr-xr-x. 2 root root 4096 Apr 2 15:44 pam.d -rw-r--r--. 1 root root 1154 Apr 2 15:44 passwd -rw-r--r--. 1 root root 1080 Apr 2 15:44 passwd- drwxr-xr-x. 7 root root 75 Apr 2 15:44 pki drwxr-xr-x. 2 root root 28 Apr 2 15:44 plymouth drwxr-xr-x. 5 root root 52 Apr 2 15:44 pm drwxr-xr-x. 2 root root 6 Apr 2 15:44 popt.d drwxr-xr-x. 2 root root 24 Apr 2 15:44 prelink.conf.d -rw-r--r--. 1 root root 233 Apr 2 15:44 printcap -rw-r--r--. 1 root root 2123 Apr 2 15:44 profile drwxr-xr-x. 2 root root 4096 Apr 2 15:44 profile.d -rw-r--r--. 1 root root 6568 Apr 2 15:44 protocols

3.将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[root@hhw- etc]# tr "[a-z]" "[A-Z]" < issue >/tmp/issue.out;cat /tmp/issue.out \S KERNEL \R ON AN \M

4.总结描述用户和组管理类命令的使用方法

用户创建命令: useradd [options] LOGIN -u UID -o 配合-u 选项,不检查UID的唯一性 -g GID 指明用户所属基本组,可为组名,也可以GID -c "COMMENT“ 用户的注释信息 -d HOME_DIR 以指定的路径(不存在)为家目录 -s SHELL 指明用户的默认shell程序,可用列表在/etc/shells文件中 -G GROUP1[,GROUP2,...] 为用户指明附加组,组须事先存在 -N 不创建私用组做主组,使用users组做主组 -r 创建系统用户 CentOS 6之前: ID<500,CentOS7 以后: ID<1000 -m 创建家目录,用于系统用户 -M 不创建家目录,用于非系统用户 -p 指定加密的密码

修改用户属性命令: usermod [OPTION] login -u UID: 新UID -g GID: 新主组 -G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使 用-a选项 -s SHELL:新的默认SHELL -c 'COMMENT':新的注释信息 -d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使用-m选项 -l login_name: 新的名字 -L: lock指定用户,在/etc/shadow 密码栏的增加 ! -U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉 -e YYYY-MM-DD: 指明用户账号过期日期 -f INACTIVE: 设定非活动期限,即宽限期

删除用户命令: userdel [OPTION]... Login -f, --force  强制 -r, --remove 删除用户家目录和邮箱

创建组命令: groupadd [OPTION]... group_name -g GID 指明GID号;[GID_MIN, GID_MAX] -r 创建系统组,CentOS 6之前: ID<500,CentOS 7以后: ID<1000

修改组命令: groupmod [OPTION]... group -n group_name: 新名字 -g GID: 新的GID

删除组命令: groupdel [options] GROUP -f, --force 强制删除,即使是用户的主组也强制删除组,但会导致无主组的用户不可用无法登录

练习

4.1 创建组distro,其GID为2019

[root@hhw- etc]# groupadd -g 2019 distro

4.2 创建用户mandriva, 其ID号为1005;基本组为distro

[root@hhw- etc]# useradd -u 1005 -g 2019 mandriva;id mandriva uid=1005(mandriva) gid=2019(distro) groups=2019(distro)

4.3 创建用户mageia,其ID号为1100,家目录为/home/linux

[root@hhw- etc]# useradd -u 1100 -d /home/linux mageia

4.4 给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期

[root@hhw- etc]# passwd -x 7 mageia;echo -e 'mageedu' | passwd mageia Adjusting aging data for user mageia. passwd: Success Changing password for user mageia. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: Password change aborted. New password: Password change aborted. New password: Password change aborted. passwd: Have exhausted maximum number of retries for service

4.5 删除mandriva,但保留其家目录

[root@hhw- mandriva]# userdel mandriva ;pwd /home/mandriva

4.6 创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

[root@hhw- mandriva]# groupadd -g 2020 peguin;useradd -u 2002 -g 2019 -G 2020 slackware;id slackware uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)

4.7 修改slackware的默认shell为/bin/tcsh;

[root@hhw- mandriva]# yum -y install csh [root@hhw- mandriva]# usermod slackware -s /usr/bin/tcsh ;getent passwd slackware slackware:x:2002:2019::/home/slackware:/usr/bin/tcsh

4.8 为用户slackware新增附加组admins,并设置不可登陆。

[root@hhw- ~]# groupadd -g 2222 admins;usermod -G 2222 -s /sbin/nologin slackware;getent passwd slackware slackware:x:2022:2019::/home/slackware:/sbin/nologin

5.创建用户user1、user2、user3。在/data/下创建目录test

[root@hhw- data]# useradd user1;useradd user2;useradd user3;mkdir /data/test

5.1 目录/data/test属主、属组为user1

[root@hhw- data]# chown user1:user1 test/;ll -d test/ drwxr-xr-x. 2 user1 user1 6 Apr 2 16:43 test/

5.2 在目录属主、属组不变的情况下,user2对文件有读写权限

[root@hhw- data]# setfacl -m u:user2:rw /data/test/ ;getfacl test/ # file: test/ # owner: user1 # group: user1 user::rwx user:user2:rw- group::r-x mask::rwx other::r-x

5.3 user1在/data/test目录下创建文件a1.sh, a2.sh, a3.sh, a4.sh,设置所有用户都不可删除1.sh,2.sh文件、除了user1及root之外,所有用户都不可删除a3.sh, a4.sh

[user1@hhw- test]$ touch a{1..4}.sh [user1@hhw- test]$ ll total 0 -rw-rw-r--. 1 user1 user1 0 Apr 2 17:01 a1.sh -rw-rw-r--. 1 user1 user1 0 Apr 2 17:01 a2.sh -rw-rw-r--. 1 user1 user1 0 Apr 2 17:01 a3.sh -rw-rw-r--. 1 user1 user1 0 Apr 2 17:01 a4.sh [root@hhw- test]# chattr +i a[1..2].sh [root@hhw- test]# lsattr ----i--------------- ./a1.sh ----i--------------- ./a2.sh -------------------- ./a3.sh -------------------- ./a4.sh [root@hhw- test]# chmod o+t a[3-4].sh

5.4 user3增加附加组user1,同时要求user1不能访问/data/test目录及其下所有文件

[root@hhw- test]# usermod -G user1 user3 ;id user3 ;setfacl -m u:user1:--- /data/test/ uid=2025(user3) gid=2025(user3) groups=2025(user3),2023(user1)

5.5 清理/data/test目录及其下所有文件的acl权限

[root@hhw- test]# setfacl -b /data/test ;getfacl /data/test getfacl: Removing leading '/' from absolute path names # file: data/test # owner: user1 # group: user1 user::rwx group::r-x other::r-x

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:历史上最伟大的12位程序员
下一篇:C#线程处理 :一、线程基础(count)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~