Linux用户管理与grep正则表达式实战
前言部分简要说明文章目的。
本教程旨在分享Linux系统中关于用户管理的实际操作步骤以及grep正则表达式的应用案例,供读者参考学习。以下内容将直接切入主题,详细展开各项技术细节。
核心内容聚焦于Linux系统的用户账户管理流程以及grep工具中正则表达式的语法应用。
任务一:将系统默认模板目录 /etc/skel 复制至 /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/
任务二:修改配置文件 /etc/group,在其中新增一个名为 hadoop 的用户组。
[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029
任务三:手动在 /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
任务四:将 /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/
任务五:递归修改 /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 2026 .bash_logout
-rw-r--r-- 1 hadoop hadoop 193 Nov 20 2026 .bash_profile
-rw-r--r-- 1 hadoop hadoop 231 Nov 20 2026 .bashrc
drwxr-xr-x 4 hadoop hadoop 37 Feb 12 03:03 .mozilla
任务六:从 /proc/meminfo 文件中筛选出以大写 S 或小写 s 开头的行,要求提供两种不同的实现方式。
第一种实现方式。
[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
第二种实现方式。
[root@suywien ~]# grep -i "^s" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 9100 kB
任务七:在 /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
......
任务八:在 /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
任务九:从 /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
......
任务十:显示 /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
......
任务十一:在 /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.
任务十二:执行 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
任务十三:依次添加用户 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
总结部分回顾全文。
以上内容涵盖了Linux用户管理及grep正则表达式的核心知识点,希望对您的学习与工作有所助益。若有任何疑问,欢迎留言交流,感谢支持。