Linux用户、组管理和正则表达式的基础命令

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何权限

[root@localhost skel]# cp -r /etc/skel /home/tuser1
[root@localhost skel]# ls -al /home/tuser1
total 12
drwxr-xr-x. 3 root root  70 Jul 19 22:39 .
drwxr-xr-x. 9 root root 100 Jul 19 22:38 ..
-rw-r--r--. 1 root root  18 Jul 19 22:38 .bash_logout
-rw-r--r--. 1 root root 193 Jul 19 22:38 .bash_profile
-rw-r--r--. 1 root root 231 Jul 19 22:38 .bashrc
drwxr-xr-x. 2 root root  59 Jul 19 22:39 skel
[root@localhost ~]# chmod -R go= /home/tuser1
[root@localhost ~]# ls -la /home/tuser1
total 16
drwx------.  3 root root   70 Jul 19 22:39 .
drwxr-xr-x. 14 root root 4096 Jul 20 00:51 ..
-rw-------.  1 root root   18 Jul 19 22:38 .bash_logout
-rw-------.  1 root root  193 Jul 19 22:38 .bash_profile
-rw-------.  1 root root  231 Jul 19 22:38 .bashrc
drwx------.  2 root root   59 Jul 19 22:39 ske

2、编辑/etc/group文件,添加组Hadoop

[root@localhost skel]# nano /etc/group

  GNU nano 2.3.1              File: /etc/group                                  

entadmin:x:5002:test
test2:x:4003:
distro:x:2016:
mageia:x:1100:
admins:x:5003:slackware
mariadb:x:994:
magedu:x:5000:gentoo,fedora
fedora:x:4005:
hadoop:x:5004:

3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组为hadoop,其家目录为/home/hadoop 

[root@localhost ~]# nano /etc/passwd

  GNU nano 2.3.1             File: /etc/passwd                                  

test:x:4001:1000:shijl,hs,110,119:/var/tmp/gentoo:/bin/bash
fedors:x:4002:4002:Fedora Core:/home/fedors:/bin/tcsh
test2:x:4003:4003::/home/test2:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/tcsh
mariadb:x:996:994::/home/mariadb:/sbin/nologin
gentoo:x:4004:1000::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
hadoop:x:5004:5004::/home/hadoop:/bin/bash

4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限

[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# chmod go= /home/hadoop
[root@localhost ~]# ls -dl /home/hadoop/
drwx------. 2 root root 59 Jul 19 23:16 /home/hadoop/

5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop

[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop/
drwx------. 2 hadoop hadoop 59 Jul 19 23:16 /home/hadoop/
[root@localhost ~]# ls -al /home/hadoop/
total 16
drwx------.  2 hadoop hadoop   59 Jul 19 23:16 .
drwxr-xr-x. 10 root   root   4096 Jul 19 23:16 ..
-rw-r--r--.  1 hadoop hadoop   18 Jul 19 23:16 .bash_logout
-rw-r--r--.  1 hadoop hadoop  193 Jul 19 23:16 .bash_profile
-rw-r--r--.  1 hadoop hadoop  231 Jul 19 23:16 .bashrc

6、显示/proc/meminfo文件中以大写或小写S开头的行,用两种方式

[root@localhost ~]# grep "^[sS]" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              6844 kB
Slab:              64964 kB
SReclaimable:      26088 kB
SUnreclaim:        38876 kB
[root@localhost ~]# grep -i "^s" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              6844 kB
Slab:              64964 kB
SReclaimable:      26088 kB
SUnreclaim:        38876 kB

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户

[root@localhost ~]# grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
test
fedors
test2
mageia
slackware
gentoo
fedora
hadoop

8、显示/etc/passwd文件中其默认shell为/bin/bash的用户

[root@localhost ~]# grep "/bin/bash$" /etc/passwd | cut -d: -f1
root
test
test2
mageia
gentoo
fedora
hadoop

9、找出/etc/passwd文件中的一位数或两位数

[root@localhost ~]# grep "\<[0-9]\{1,2\}\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

10、显示/boot/grub2/grub.cfg中以至少一个空白字符开头的行

[root@localhost ~]# grep "^[[:space:]]\+" /boot/grub2/grub.cfg
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb

11、显示/etc/rc.d/rc.local文件中以#开头后面跟至少一个空白字符,而后又有至少一个非空白字符的行

[root@localhost ~]# grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.local 
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

12、打出netstat -tan命令执行结果中以“LISTEN“,后或跟空白字符结尾的行

[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$" 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN  

13、添加用户bash,testbash,basher,nologin(此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息

[root@localhost ~]# useradd bash 
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd  -s /sbin/nologin nologin
[root@localhost ~]# grep "\(nologin\).*\1" /etc/passwd
nologin:x:5008:5008::/home/nologin:/sbin/nologin

原创文章,作者:N27_shijinlong,如若转载,请注明出处:http://www.178linux.com/81918

(1)
N27_shijinlongN27_shijinlong
上一篇 2017-07-20
下一篇 2017-07-20

相关推荐

  • N25 第五周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; 3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行; 4、添加用户bash, testbash, basher, nol…

    Linux干货 2017-01-08
  • 硬链接与软链接的区别

    硬链接(Hard Link):   硬连接指通过索引节点来进行链接(即一个指针指向文件索引节点)。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种链接就是硬链接。硬链接的作用是允许一个文件拥有多个有效路径…

    Linux干货 2016-10-20
  • 马哥教育网络班N22期+第6周课程练习

    一、总结vim编辑器的使用方法 vim文本编辑器 全屏编辑器,模式化编辑器 vim的教程文件命令:vimtutor vim /path/to/somefile vim模式: 编辑模式(命令模式),默认模式 输入模式 末行模式 内置的命令行接口; 模式转换: 编辑模式—>输入模式: i:insert, 在光标所在处前方输入,转为输入模式 a:append…

    Linux干货 2016-10-09
  • Dell R720 安装debian系统编译网卡驱动

    最近比较忙,视频也没怎么看,下面是之前工作中遇到的问题,事后做的记录,中间解决过程参考了网上的相关资料,现在一并贴出来。 需求:Dell R720 安装debian系统 环境 硬件:Dell R720  软件:系统有debian 7.0.0和debian 6.0.10 1. debian 7.0.0 : 集成了DELL PCRE H710 Mini…

    Linux干货 2015-08-04
  • Nginx:

    来自为知笔记(Wiz)

    Linux干货 2016-10-26
  • 实现真实的机柜模拟图[原创]

    一般能反映机房设备位置、结构我们都喜欢通过网络拓扑图来展现,但个人感觉还不够直观、明了的表现出自己想要的结果(自己太挑剔了,呵呵)。因此写一个生成真实机柜模拟图平台,实现与真实服务器外观、服务状态、空闲位置等信息。在线效果图http://blog.liuts.com/idc/系统截图1、平台显示某一排截图2、平台显示某台服务器详细信息截图3、状态说明2U服务…

    Linux干货 2015-03-27

评论列表(1条)

  • 马哥教育
    马哥教育 2017-07-27 00:01

    总结的不错,第一题、第四题和第五题可以归纳为一类题目来做,后面可以多总结;继续加油~