欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux系统用户管理与grep正则表达式示例教程

程序员文章站 2023-09-09 19:15:24
前言 本文主要给大家介绍了关于linux系统用户管理与grep正则表达式的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 linux系统用户...

前言

本文主要给大家介绍了关于linux系统用户管理与grep正则表达式的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

linux系统用户管理与grep正则表达式

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

[root@suywien ~]# cp -rpv /etc/skel/ /home/tuser1/
‘/etc/skel/' -> ‘/home/tuser1/'
‘/etc/skel/.mozilla' -> ‘/home/tuser1/.mozilla'
‘/etc/skel/.mozilla/extensions' -> ‘/home/tuser1/.mozilla/extensions'
‘/etc/skel/.mozilla/plugins' -> ‘/home/tuser1/.mozilla/plugins'
‘/etc/skel/.bash_logout' -> ‘/home/tuser1/.bash_logout'
‘/etc/skel/.bash_profile' -> ‘/home/tuser1/.bash_profile'
‘/etc/skel/.bashrc' -> ‘/home/tuser1/.bashrc'
[root@suywien ~]# useradd tuser -d /home/tuser1/
useradd: warning: the home directory already exists.
not copying any file from skel directory into it.
[root@suywien ~]# chmod -r go-rwx /home/tuser1/
[root@suywien ~]# ll -d /home/tuser1/
drwx------ 3 root root 74 feb 12 03:04 /home/tuser1/

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

[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029

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

[root@suywien ~]# vim /etc/passwd
[root@suywien ~]# tail -1 /etc/passwd
hadoop:x:1029:1029::/home/hodoop/:/bin/bash

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

[root@suywien ~]# cp -fpv /etc/skel/ /home/hadoop
cp: omitting directory ‘/etc/skel/'
[root@suywien ~]# cp -fpvr /etc/skel/ /home/hadoop
‘/etc/skel/' -> ‘/home/hadoop'
‘/etc/skel/.mozilla' -> ‘/home/hadoop/.mozilla'
‘/etc/skel/.mozilla/extensions' -> ‘/home/hadoop/.mozilla/extensions'
‘/etc/skel/.mozilla/plugins' -> ‘/home/hadoop/.mozilla/plugins'
‘/etc/skel/.bash_logout' -> ‘/home/hadoop/.bash_logout'
‘/etc/skel/.bash_profile' -> ‘/home/hadoop/.bash_profile'
‘/etc/skel/.bashrc' -> ‘/home/hadoop/.bashrc'
[root@suywien ~]# chmod go-rwx /home/hadoop/
[root@suywien ~]# ls -ld /home/hadoop/
drwx------ 3 root root 74 feb 12 03:04 /home/hadoop/

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

[root@suywien ~]# chown -r hadoop:hadoop /home/hadoop/
[root@suywien ~]# ll -al /home/hadoop/
total 16
drwx------ 3 hadoop hadoop 74 feb 12 03:04 .
drwxr-xr-x. 10 root root 4096 mar 25 10:14 ..
-rw-r--r-- 1 hadoop hadoop 18 nov 20 2015 .bash_logout
-rw-r--r-- 1 hadoop hadoop 193 nov 20 2015 .bash_profile
-rw-r--r-- 1 hadoop hadoop 231 nov 20 2015 .bashrc
drwxr-xr-x 4 hadoop hadoop 37 feb 12 03:03 .mozilla

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

1、

[root@suywien ~]# grep ^[s,s] /proc/meminfo 
 swapcached:   0 kb
 swaptotal:  2097148 kb
 swapfree:  2097148 kb
 shmem:    9100 kb
 slab:    89476 kb
 sreclaimable:  53176 kb
 sunreclaim:  36300 kb

2、

[root@suywien ~]# grep -i "^s" /proc/meminfo 
 swapcached:   0 kb
 swaptotal:  2097148 kb
 swapfree:  2097148 kb
 shmem:    9100 kb

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

[root@suywien ~]# grep \/sbin\/nologin$ /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
......

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

[root@suywien ~]# grep \/bin\/bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
roo:x:1000:1000:root:/home/roo:/bin/bash
gentoo:x:4001:4001::/home/gentoo:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
tuser:x:4003:4003::/home/tuser1/:/bin/bash
hadoop:x:1029:1029::/home/hodoop/:/bin/bash

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

[root@suywien ~]# grep "\<[0-9]\{2,3\}\>" /etc/passwd
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
......

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

[root@suywien ~]# grep "^[[:space:]]\+" /boot/grub2/grub.cfg 
 load_env
 set default="${next_entry}"
 set next_entry=
 save_env next_entry
 set boot_once=true
 ......

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

[root@suywien ~]# 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@suywien ~]# netstat -tan | grep "listen[[:space:]]*$"
tcp  0  0 192.168.122.1:53  0.0.0.0:*    listen  
tcp  0  0 0.0.0.0:22    0.0.0.0:*    listen  
tcp  0  0 127.0.0.1:631   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:631     :::*     listen  
tcp6  0  0 ::1:25     :::*     listen

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

[root@suywien ~]# useradd bash
[root@suywien ~]# useradd basher
[root@suywien ~]# useradd -s /sbin/nologin nologin

[root@suywien ~]# grep -e "^([^:]+\>).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:4004:4004::/home/bash:/bin/bash
nologin:x:4006:4006::/home/nologin:/sbin/nologin

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。