正則實踐練習

接上一篇博文:http://blog.51cto.com/9657273/2299225

說明:本次博文是一套練習,用於強化前邊的知識點。

  • 1、複製/etc/skel 目錄爲 /home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限;
注意點:
1. 複製的骨架文件/etc/skel是一個目錄,所以cp要加遞歸複製選項-r;
2. 要求複製的權限文件屬組和其他用戶均沒有任何訪問權限,這個時候可能用臨時會話改變umask值或者
在當前bash的子shell中改變umask值並操作複製,並且複製的時候cp不能保留原有屬性;

實際執行命令爲:
(umask 0077;cp -r /etc/skel /home/tuser1)

操作過程:
[root@node2 tmp]# (umask 0077;cp -r /etc/skel /home/tuser1)
[root@node2 tmp]# ls -la /home/tuser1/
total 12
drwx------  2 root root  59 Oct 18 16:50 .
drwxr-xr-x. 3 root root  19 Oct 18 16:50 ..
-rw-------  1 root root  18 Oct 18 16:50 .bash_logout
-rw-------  1 root root 193 Oct 18 16:50 .bash_profile
-rw-------  1 root root 231 Oct 18 16:50 .bashrc
[root@node2 tmp]# ls -ld /home/tuser1/
drwx------ 2 root root 59 Oct 18 16:50 /home/tuser1/
  • 2、編輯/etc/group文件,添加組hadoop;
這題沒有什麼好說的,注意/etc/group配置文件語法格式和規範;同時添加的組id要符合普通非系統用戶範圍
可以使用vi編輯器或者sed直接修改文件或者echo,cat等命令加重定向來完成都行;
加入的一條記錄內容爲:
hadoop:x:1001:

操作命令:
echo 'hadoop:x:1001:' >>/etc/group

操作過程:
[root@node2 tmp]# echo 'hadoop:x:1001:' >>/etc/group
[root@node2 tmp]# tail -1 /etc/group
hadoop:x:1001:
  • 3、手動編輯/etc/passwd文件新增一行,添加用戶hadoop,其基本組ID爲hadoop組的id號,其家目錄爲/home/hadoop;
注意/etc/passwd配置文件語法格式,以冒號分隔的第一個字段表示用戶名,第四個字段表示用戶屬組基本
組id號,倒數第二個字段表示用戶的家目錄
新增內容爲:
hadoop:x:1002:1001::/home/hadoop:/bin/bash

我是直接vim編輯操作新增的,這裏就不給出命令了;配置文件/etc/passwd的最後一行爲(不一定要寫入
到最後一行):
[root@node2 tmp]# tail -1 /etc/passwd
hadoop:x:1002:1001::/home/hadoop:/bin/bash
  • 4、複製/etc/skel 目錄爲 /home/hadoop,要求修改hadoop目錄的屬組和其他用戶沒有任何訪問權限;
這一題和第一題很像,這題就是應用了。我們不做說明,給出命令和過程即可。
命令爲:
(umask 0077;cp -r /etc/skel /home/hadoop)

執行過程和結果:
[root@node2 tmp]# (umask 0077;cp -r /etc/skel /home/hadoop)
[root@node2 tmp]# ls -la /home/hadoop/
total 12
drwx------  2 root root  59 Oct 18 17:05 .
drwxr-xr-x. 5 root root  45 Oct 18 17:05 ..
-rw-------  1 root root  18 Oct 18 17:05 .bash_logout
-rw-------  1 root root 193 Oct 18 17:05 .bash_profile
-rw-------  1 root root 231 Oct 18 17:05 .bashrc
[root@node2 tmp]# ls -ld /home/hadoop/
drwx------ 2 root root 59 Oct 18 17:05 /home/hadoop/
  • 5、修改/home/hadoop 目錄及其內部所有文件的屬主爲hadoop,屬組爲hadoop;
執行命令:
chown -R hadoop:hadoop /home/hadoop
執行過程和結果:
[root@node2 tmp]# chown -R hadoop:hadoop /home/hadoop
[root@node2 tmp]# ls -la /home/hadoop/
total 12
drwx------  2 hadoop hadoop  59 Oct 18 17:05 .
drwxr-xr-x. 5 root   root    45 Oct 18 17:05 ..
-rw-------  1 hadoop hadoop  18 Oct 18 17:05 .bash_logout
-rw-------  1 hadoop hadoop 193 Oct 18 17:05 .bash_profile
-rw-------  1 hadoop hadoop 231 Oct 18 17:05 .bashrc
[root@node2 tmp]# ls -ld /home/hadoop/
drwx------ 2 hadoop hadoop 59 Oct 18 17:05 /home/hadoop/
  • 6、顯示/proc/meminfo 文件中以大寫或小寫s開頭的行,用兩種方式;
執行命令:
方法1:grep '^[sS]' /proc/meminfo
方法2:grep -i '^s' /proc/meminfo
方法3:grep -E '^(s|S)' /proc/meminfo

執行過程和結果:
爲了演示效果,我認爲的複製了一份/proc/meminfo,並且對立面的內容有意的加入了以小寫字母s開頭的行
以及小寫字母在中間的文本。
[root@node2 tmp]# grep '^[sS]' /proc/meminfo 
SwapCached:          208 kB
SwapTotal:       2097148 kB
SwapFree:        2095648 kB
Shmem:              7264 kB
Slab:              61208 kB
SReclaimable:      35548 kB
SUnreclaim:        25660 kB
[root@node2 tmp]# cp /proc/meminfo /tmp/meminfo.bak
[root@node2 tmp]# echo "swapTotals:       2097148 kB" >> /tmp/meminfo.bak 
[root@node2 tmp]# grep '^[sS]' /tmp/meminfo.bak
SwapCached:          208 kB
SwapTotal:       2097148 kB
SwapFree:        2095648 kB
Shmem:              7264 kB
Slab:              61208 kB
SReclaimable:      35548 kB
SUnreclaim:        25660 kB
swapTotals:       2097148 kB
[root@node2 tmp]# grep -i '^s' /tmp/meminfo.bak 
SwapCached:          208 kB
SwapTotal:       2097148 kB
SwapFree:        2095648 kB
Shmem:              7264 kB
Slab:              61208 kB
SReclaimable:      35548 kB
SUnreclaim:        25660 kB
swapTotals:       2097148 kB
[root@node2 tmp]# grep -E '^(s|S)' /tmp/meminfo.bak 
SwapCached:          208 kB
SwapTotal:       2097148 kB
SwapFree:        2095648 kB
Shmem:              7264 kB
Slab:              61208 kB
SReclaimable:      35548 kB
SUnreclaim:        25660 kB
swapTotals:       2097148 kB
  • 7、顯示/etc/passwd文件中其默認shell爲非/sbin/nologin的用戶;
/etc/passwd文件以符號:分隔的最後一個字段表示用戶的登錄shell;
執行命令:
grep -v '/sbin/nologin$' /etc/passwd | cut -d':' -f1

執行過程:
[root@node2 tmp]# grep -v '/sbin/nologin$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
yanhui:x:1000:1000::/home/yanhui:/bin/bash
hadoop:x:1002:1001::/home/hadoop:/bin/bash
[root@node2 tmp]# grep -v '/sbin/nologin$' /etc/passwd | cut -d':' -f1
root
sync
shutdown
halt
yanhui
hadoop
  • 8、顯示/etc/passwd文件中其默認shell爲/bin/bash的用戶;
執行命令:
grep '/bin/bash$' /etc/passwd|cut -d':' -f1

執行過程和結果:
[root@node2 tmp]# grep '/bin/bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
yanhui:x:1000:1000::/home/yanhui:/bin/bash
hadoop:x:1002:1001::/home/hadoop:/bin/bash
[root@node2 tmp]# grep '/bin/bash$' /etc/passwd|cut -d':' -f1
root
yanhui
hadoop
  • 9、找出/etc/passwd文件中的一位數或兩位數;
注意一點,做正則匹配的時候,默認是貪婪匹配的,如果要限定值匹配固定數量的,要用邊界錨定;
還有一點注意擴展正則與標準正則中,特定元字符使用的形式略有不同,標準正則中要做轉移的;
執行命令:
grep -E '\b[0-9]{1,2}\b' /etc/passwd

執行過程和結果:
[root@node2 tmp]# grep -E '\b[0-9]{1,2}\b' /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
tcpdump:x:72:72::/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
  • 10、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行;
注意:/boot/grub/grub.conf這個配置文件在CentOS 7.x系列上沒有,要在CentOS 6.x上測試;
執行命令:grep -E '^[[:space:]]{1,}' /boot/grub/grub.conf

執行過程和結果:
[root@vir-rs2 ~]# ls -l /boot/grub/grub.conf 
-rw-------. 1 root root 771 Aug 16 00:26 /boot/grub/grub.conf
[root@vir-rs2 ~]#  grep -E '^[[:space:]]{1,}' /boot/grub/grub.conf
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=610d4939-d78f-462b-92d6-8ad25ac6ad8e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img
[root@vir-rs2 ~]#  grep -E '^[[:space:]]{1,}' /boot/grub/grub.conf|cat -A
^Iroot (hd0,0)$
^Ikernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=610d4939-d78f-462b-92d6-8ad25ac6ad8e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet$
^Iinitrd /initramfs-2.6.32-696.el6.x86_64.img$
  • 11、顯示/etc/rc.d/rc.sysinit文件中以#開頭,後面跟至少一個空白字符,而後又至少一個非空白字符的行;
注意:/etc/rc.d/rc.sysinit這個配置文件CentOS 7.x上沒有,要在CentOS 6.x上測試;
執行命令:
grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit
或者
grep -E '^#[[:space:]]{1,}[^[:space:]]{1,}' /etc/rc.d/rc.sysinit

執行結果和過程:
[root@vir-rs2 ~]# grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.
# Initialize hardware
# Set default affinity
# Load other user-defined modules
# Load modules (for backward compatibility with VARs)
# Configure kernel parameters
# Set the hostname.
# Sync waiting for storage.
# Device mapper & related initialization
# Start any MD RAID arrays that haven't been started yet
# Remount the root filesystem read-write.
# Clean up SELinux labels
# If relabeling, relabel mount points.
# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
# The 'no' applies to all listed filesystem types. See mount(8).
# Check to see if a full relabel is needed
# Update quotas if necessary
# Initialize pseudo-random number generator
# Configure machine if necessary.
# Clean out /.
# Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
# Clean up /var.
# Clean up utmp/wtmp
# Clean up various /tmp bits
# Make ICE directory
# Start up swapping.
# Set up binfmt_misc
# Boot time profiles. Yes, this should be somewhere else.
# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
# create the crash indicator flag to warn on crashes, offer fsck with timeout
# Let rhgb know that we're leaving rc.sysinit
[root@vir-rs2 ~]# grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit|wc -l
38
  • 12、打出netstat -tan命令執行結果中以'LISTEN',後或跟空白字符結尾的行;
執行命令:
netstat -tan|grep -E 'LISTEN[[:space:]]*$'

執行過程:
[root@node2 tmp]# netstat -tan|grep -E 'LISTEN[[:spache:]]*$'
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相同的用戶的信息;
執行命令:
grep -E '(^[^:]+\b).*\1$' /etc/passwd

執行過程和結果:
[root@node2 ~]# grep -E '(^[^:]+\b).*\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:1003:1003::/home/bash:/bin/bash
nologin:x:1006:1006::/home/nologin:/sbin/nologin
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章