linux文件处理命令和用户管理和权限管理

文件处理命令
用户管理
权限管理

+ 1.列出当前系统上所有已经登录的用户的用户名,注意:同一用户登录多次,则只显示一次即可
“`
[root@localhost ~]# who |cut -d' ' -f1|sort|uniq
centos
root
“`
+ 2.取出最后登录到当前系统的用户的相关信息
“`
[root@localhost ~]# id `who |tail -1|cut -d' ' -f1`
uid=0(root) gid=0(root) groups=0(root)
“`
+ 3.取出当前系统上被用户当做其默认shell的最多的那个shell
“`
[root@localhost ~]# cut -d':' -f7 /etc/passwd|sort|uniq -c|tr -s ' '|sort -n -t' ' -k2|tail -1|cut -d' ' -f3
/sbin/nologin
“`
+ 4.将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt中
“`
[root@localhost ~]# sort -n -t':' -k3 /etc/passwd|tail|tr 'a-z' 'A-Z' > /tmp/maxusers.txt
“`
+ 5.取出当前主机的ip地址,提示:对ifconfig命令的结果进行划分
“`
[root@localhost ~]# ifconfig eth0|head -2|tail -1|tr -s ' '|cut -d':' -f2|cut -d ' ' -f1
192.168.1.108
“`
+ 6.列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中
“`
[root@localhost ~]# find /etc -name "*.conf" -exec basename {} \; | tr 'a-z' 'A-Z' > /tmp/etc.conf
“`
+ 7.显示/var目录下一级子目录或文件的总个数
“`
[root@localhost ~]# find /var -maxdepth 1 | grep -v "^/var$" | wc -l
17
“`
+ 8.取出/etc/group文件中第三个字段数值最小的10个组的名字
“`
[root@localhost ~]# sort -n -t':' -k3 /etc/group|head|cut -d':' -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
“`
+ 9.将/etc/fstab和/etc/issue文件中的内容合并为同一个内容后保存至/tmp/etc.test文件中。
“`
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
“`
+ 10.请总结描述用户和组管理命令的使用方法并完成以下练习
    + 1.创建组distro,其GID为2016
    + 2.创建用户mandriva,其ID号为1005,基本组为distro
    + 3.创建用户mageia,其ID号为1100,家目录为/home/linux
    + 4.给用户mageia添加密码,密码为mageedu
    + 5.删除mandriva,但保留其家目录
    + 6.创建用户slackware,其ID号为2002,基本组为distro,附加组为peguin
    + 7.修改slackware的默认shell为/bin/tcsh
    + 8.为用户slackware新增附加组admins
#groupadd
+ 创建新组
+ groupadd 组名
+ -g GID 手动指定GID
+ -r 创建系统组
+ -p 指定组密码
+ 安全上下文:进程以其发起者的身份运行,进程对文件的访问权限取决于用户的权限
+ 为了能够让后台进程或服务进程以非管理员的身份运行(最小权限原则),通常创建多个不用登录的系统用户
“`
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# tail -1 /etc/group
mysql:x:306:
“`
#groupmod
+ 修改组
+ -g GID 修改GID
+ -r 修改为系统组
+ -p 修改组密码

+ -n 修改组名

#groupdel
+删除组
“`
[root@localhost ~]# groupdel mysql
“`
#useradd
+ useradd 登录名(默认同时创建同名同ID的私有组,创建邮箱/var/spool/mail/用户名和家目录/home/用户名,shell为/bin/bash)
+ -u 指定UID
+ -g 指定基本组名(必须事先存在)
+ -c 添加注释
+ -G 指定附加组名(多个以,隔开)
+ -d 指定家目录(以指定路径为家目录路径,复制/etc/skel并重命名实现,指定家目录如果存在,则不会复制/etc/skel)
“`
[root@localhost ~]# ls -A /etc/skel
.bash_logout .bash_profile .bashrc
“`
+ -s 指定shell,列表存储在/etc/shells文件
“`
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
“`
+ -r 创建系统用户
+ -M 不创建家目录
+ -D 更改默认 /etc/login.defs的选项 存储在/etc/default/useradd中
#usermod
+ 修改用户属性
+ -u 修改GID
+ -g 修改基本组
+ -G 修改附加组,全覆盖
+ -G -a 追加附加组
+ -c 修改注释信息
+ -d 修改家目录
+ -d -m 移动修改家目录
+ -l 修改用户名
+ -s 修改shell
+ -L 锁定密码
+ -U 解锁密码
#userdel
+ 默认不删除家目录
+ -r 删除用户时一并删除家目录和邮件目录
“`
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -r -g mysql -u 306 -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@localhost ~]# userdel -r mysql
userdel: mysql mail spool (/var/spool/mail/mysql) not found
userdel: mysql home directory (/home/mysql) not found
“`
#passwd修改密码
+ 直接passwd,修改自己的密码
+ passwd USERNAME root可以修改其他用户密码
+ -l 锁定密码
+ -u 解锁密码
+ -d 删除密码
+ -n 最小期
+ -x 最大期
+ -w 警告期
+ -i 非活动宽限期
+ -e 过期
+ –stdin 非交互输入密码
“`
echo "hello" | passwd --stdin docker &> /dev/null
echo $?
“`
#gpasswd 修改组密码和组管理
+ 不带选项 为组添加密码
+ -a 从组中添加用户
+ -d 从组中删除用户
#newgrp 切换基本组
+ newgrp GROUPNAME 临时切换基本组
+ newgrp – GROUPNAME 模拟重新登录切换基本组
+ 如果用户不属于此组,则需要输入组密码
“`
[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@localhost ~]# gpasswd -a mysql root
Adding user mysql to group root
[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@localhost ~]# su - mysql
Last login: Sat May 19 22:14:58 CST 2018 on pts/0
[mysql@localhost ~]$ id
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql),0(root)
[mysql@localhost ~]$ ll
total 4
-rw-rw-r-- 1 mysql mysql 5 May 19 22:15 a.txt
-rw-rw-r-- 1 mysql mysql 0 May 19 22:15 b.txt
[mysql@localhost ~]$ touch c.txt
[mysql@localhost ~]$ ls
a.txt b.txt c.txt
[mysql@localhost ~]$ ll
total 4
-rw-rw-r-- 1 mysql mysql 5 May 19 22:15 a.txt
-rw-rw-r-- 1 mysql mysql 0 May 19 22:15 b.txt
-rw-rw-r-- 1 mysql mysql 0 May 19 22:17 c.txt
[mysql@localhost ~]$ newgrp root
[mysql@localhost ~]$ id
uid=1000(mysql) gid=0(root) groups=0(root),1000(mysql)
[mysql@localhost ~]$ touch d.txt
[mysql@localhost ~]$ ll
total 4
-rw-rw-r-- 1 mysql mysql 5 May 19 22:15 a.txt
-rw-rw-r-- 1 mysql mysql 0 May 19 22:15 b.txt
-rw-rw-r-- 1 mysql mysql 0 May 19 22:17 c.txt
-rw-r--r-- 1 mysql root 0 May 19 22:17 d.txt
“`
#chage
+ 更改查看用户密码信息
+ -l查看信息
“`
[root@localhost ~]# chage -l root
Last password change : never
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
“`
#id 显示用户真实和有效id信息
+ -u UID
+ -g 基本GID
+ -G 所有GID
+ -n 仅显示名称
“`
[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@localhost ~]# id -u
0
[root@localhost ~]# id -g
0
[root@localhost ~]# id -G
0
“`
#su 命令 switch user
+ su – 登录式切换,重新读取shell的环境变量,重新登录
+ su 非登录式切换
+ 管理员无密码可以切换到其他任意用户
+ 非管理员要给出目标用户的密码
+ su – 登录名 -c ‘COMMAND’ 仅以指定用户的身份运行此命令
“`
[root@localhost ~]# su - mysql -c 'id'
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)
“`
#其他命令
+ chsh 修改shell
+ chfn 修改finger信息
+ finger 用户信息
+ whoami 当前登录用户
“`
[root@localhost ~]# whoami
root
“`
+ pwck 检查/etc/passwd
+ grpck 检查/etc/group
“`
[root@localhost ~]# pwck
user 'ftp': directory '/var/ftp' does not exist
pwck: no changes
[root@localhost ~]# grpck
“`
###第10题作业
+ 1.创建组distro,其GID为2016
“`
[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# tail -1 /etc/group
distro:x:2016:
“`
+ 2.创建用户mandriva,其ID号为1005,基本组为distro
“`
[root@localhost ~]# useradd -u 1005 -g distro mandirva
[root@localhost ~]# tail -1 /etc/passwd
mandirva:x:1005:2016::/home/mandirva:/bin/bash
“`
+ 3.创建用户mageia,其ID号为1100,家目录为/home/linux
“`
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia
[root@localhost ~]# tail -1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
“`
+ 4.给用户mageia添加密码,密码为mageedu
“`
[root@localhost ~]# echo "mageedu"|passwd --stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
“`
+ 5.删除mandriva,但保留其家目录
“`
[root@localhost ~]# userdel mandirva
[root@localhost ~]# ll -d /home/mandirva/
drwx------ 2 1005 distro 62 May 28 18:54 /home/mandirva/
“`
+ 6.创建用户slackware,其ID号为2002,基本组为distro,附加组为peguin
“`
[root@localhost ~]# groupadd peguin
[root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware
[root@localhost ~]# tail -1 /etc/group
peguin:x:2017:slackware
[root@localhost ~]# tail -1 /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/bash
“`
+ 7.修改slackware的默认shell为/bin/tcsh
“`
[root@localhost ~]# yum -y install tcsh
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@localhost ~]# usermod -s /bin/tcsh slackware
[root@localhost ~]# grep "slackware" /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh
“`
+ 8.为用户slackware新增附加组admins

“`

[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -a -G admins slackware
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
“`

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/99664

(1)
上一篇 2018-05-28 17:41
下一篇 2018-05-28 20:33

相关推荐

评论列表(1条)

  • 马哥教育
    马哥教育 2018-05-31 20:16

    很不错。命令建议格式化一下。