linux用户,正则表达练习

日常工作中与用户相关的一些命令操作。

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

例如:

[root@caibird ~]# cp /etc/skel/ /home/tuser1 -r
[root@caibird ~]# ll -a /home/tuser1/
总用量 24
drwxr-xr-x. 3 root root 4096 527 05:47 .
drwxr-xr-x. 3 root root 4096 527 05:47 ..
-rw-r--r--. 1 root root   18 527 05:47 .bash_logout
-rw-r--r--. 1 root root  176 527 05:47 .bash_profile
-rw-r--r--. 1 root root  124 527 05:47 .bashrc
drwxr-xr-x. 2 root root 4096 527 05:47 .gnome2

[root@caibird ~]# chmod  -R go=  /home/tuser1/
[root@caibird ~]# ll -a /home/tuser1/
总用量 24
drwx------. 3 root root 4096 527 05:47 .
drwxr-xr-x. 3 root root 4096 527 05:47 ..
-rw-------. 1 root root   18 527 05:47 .bash_logout
-rw-------. 1 root root  176 527 05:47 .bash_profile
-rw-------. 1 root root  124 527 05:47 .bashrc
drwx------. 2 root root 4096 527 05:47 .gnome2
[root@caibird ~]# 

********************************************************

[root@caibird ~]# ll -a /home/tuser1/
总用量 28
d-wx--x--x. 4 root root 4096 528 20:59 .
drwxr-xr-x. 9 root root 4096 529 10:50 ..
-rw-r--r--. 1 root root   18 528 20:59 .bash_logout
-rw-r--r--. 1 root root  176 528 20:59 .bash_profile
-rw-r--r--. 1 root root  124 528 20:59 .bashrc
drwxr-xr-x. 2 root root 4096 528 20:59 .gnome2
drwxr-xr-x. 4 root root 4096 528 20:59 .mozilla
[root@caibird ~]# chmod -R 700 /home/tuser1/
[root@caibird ~]# !l                                # 调用history !l  快捷键       
ll -a /home/tuser1/    
总用量 28
drwx------. 4 root root 4096 528 20:59 .
drwxr-xr-x. 9 root root 4096 529 10:50 ..
-rwx------. 1 root root   18 528 20:59 .bash_logout
-rwx------. 1 root root  176 528 20:59 .bash_profile
-rwx------. 1 root root  124 528 20:59 .bashrc
drwx------. 2 root root 4096 528 20:59 .gnome2
drwx------. 4 root root 4096 528 20:59 .mozilla
[root@caibird ~]# 


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

例如:

[root@caibird ~]# tail  /etc/group
utempter:x:35:
floppy:x:19:
vcsa:x:69:
cdrom:x:11:
tape:x:33:
dialout:x:18:
saslauth:x:76:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
[root@caibird ~]# echo hadoop:x:200 >> /etc/group              #添加hadoop组到/etc/group
[root@caibird ~]# tail /etc/group
floppy:x:19:
vcsa:x:69:
cdrom:x:11:
tape:x:33:
dialout:x:18:
saslauth:x:76:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
hadoop:x:200
[root@caibird ~]# 

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

例如:

[root@caibird ~]# tail /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@caibird ~]# echo hadoop:x:200:200::/home/hadoop:/bin/bash
hadoop:x:200:200::/home/hadoop:/bin/bash
[root@caibird ~]# echo hadoop:x:200:200::/home/hadoop:/bin/bash >> /etc/passwd
[root@caibird ~]# tail -1 /etc/passwd
hadoop:x:200:200::/home/hadoop:/bin/bash

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

例如:

[root@caibird ~]# ll /home/
总用量 8
drwxr-xr-x. 4 root root 4096 527 10:12 hadoop
drwx------. 3 root root 4096 5月  27 05:47 tuser1
[root@caibird ~]# cp /etc/skel/  /home/hadoop/ -r
[root@caibird ~]# ls /home/hadoop/ -la
总用量 28
drwxr-xr-x. 4 root root 4096 527 10:12 .
drwxr-xr-x. 4 root root 4096 527 10:11 ..
-rw-r--r--. 1 root root   18 5月  27 10:11 .bash_logout
-rw-r--r--. 1 root root  176 5月  27 10:11 .bash_profile
-rw-r--r--. 1 root root  124 5月  27 10:11 .bashrc
drwxr-xr-x. 2 root root 4096 527 10:11 .gnome2
drwxr-xr-x. 3 root root 4096 527 10:12 skel
[root@caibird ~]# chmod -R go= /home/hadoop/
[root@caibird ~]# ll -d /home/hadoop/
drwx------. 4 root root 4096 5月  27 10:12 /home/hadoop/
[root@caibird ~]# ll -a /home/hadoop/
总用量 28
drwx------. 4 root root 4096 5月  27 10:12 .
drwxr-xr-x. 4 root root 4096 527 10:11 ..
-rw-------. 1 root root   18 5月  27 10:11 .bash_logout
-rw-------. 1 root root  176 5月  27 10:11 .bash_profile
-rw-------. 1 root root  124 5月  27 10:11 .bashrc
drwx------. 2 root root 4096 5月  27 10:11 .gnome2
drwx------. 3 root root 4096 5月  27 10:12 skel
[root@caibird ~]# 

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

例如:

[root@caibird ~]# cp /etc/skel/ /home/hadoop -r
[root@caibird ~]# ls /home/hadoop/ -al
总用量 28
drwxr-xr-x. 4 root root 4096 529 10:50 .
drwxr-xr-x. 9 root root 4096 529 10:50 ..
-rw-r--r--. 1 root root   18 529 10:50 .bash_logout
-rw-r--r--. 1 root root  176 529 10:50 .bash_profile
-rw-r--r--. 1 root root  124 529 10:50 .bashrc
drwxr-xr-x. 2 root root 4096 529 10:50 .gnome2
drwxr-xr-x. 4 root root 4096 529 10:50 .mozilla
[root@caibird ~]# chmod -R 700 /home/hadoop/
[root@caibird ~]# ls /home/hadoop/
[root@caibird ~]# ls /home/hadoop/ -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla
[root@caibird ~]# ls /home/hadoop/ -al
总用量 28
drwx------. 4 root root 4096 529 10:50 .
drwxr-xr-x. 9 root root 4096 529 10:50 ..
-rwx------. 1 root root   18 529 10:50 .bash_logout
-rwx------. 1 root root  176 529 10:50 .bash_profile
-rwx------. 1 root root  124 529 10:50 .bashrc
drwx------. 2 root root 4096 529 10:50 .gnome2
drwx------. 4 root root 4096 529 10:50 .mozilla
[root@caibird ~]# chown -R hadoop:hadoop /home/hadoop/
[root@caibird ~]# ls -al /home/hadoop/
总用量 28
drwx------. 4 hadoop hadoop 4096 529 10:50 .
drwxr-xr-x. 9 root   root   4096 529 10:50 ..
-rwx------. 1 hadoop hadoop   18 529 10:50 .bash_logout
-rwx------. 1 hadoop hadoop  176 529 10:50 .bash_profile
-rwx------. 1 hadoop hadoop  124 529 10:50 .bashrc
drwx------. 2 hadoop hadoop 4096 529 10:50 .gnome2
drwx------. 4 hadoop hadoop 4096 529 10:50 .mozilla
[root@caibird ~]# 

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

例如 :

[root@caibird ~]# grep "^[sS]\+" /proc/meminfo   #方法一
SwapCached:         1988 kB
SwapTotal:       2097148 kB
SwapFree:        2089648 kB
Shmem:              3872 kB
Slab:             115900 kB
SReclaimable:      48380 kB
SUnreclaim:        67520 kB

[root@caibird ~]# grep -i "^s" /proc/meminfo    #方法二
SwapCached:         1988 kB
SwapTotal:       2097148 kB
SwapFree:        2089648 kB
Shmem:              3872 kB
Slab:             115900 kB
SReclaimable:      48384 kB
SUnreclaim:        67516 kB

[root@caibird ~]# grep "^[s\|S]"  /proc/meminfo  #方法三
SwapCached:         1988 kB
SwapTotal:       2097148 kB
SwapFree:        2089648 kB
Shmem:              3872 kB
Slab:             115900 kB
SReclaimable:      48388 kB
SUnreclaim:        67512 kB
[root@caibird ~]# 

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

例如:

[root@caibird ~]# 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
tan:x:500:500::/home/tan:/bin/bash
bash:x:501:501::/home/bash:/bin/bash
testbash:x:502:502::/home/testbash:/bin/bash
basher:x:503:503::/home/basher:/bin/bash

********************************************************


[root@caibird ~]# grep  "[^/sbin/nologin]$" /etc/passwd 
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
tan:x:500:500::/home/tan:/bin/bash
bash:x:501:501::/home/bash:/bin/bash
testbash:x:502:502::/home/testbash:/bin/bash
basher:x:503:503::/home/basher:/bin/bash
[root@caibird ~]# 

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

例如:

[root@caibird ~]# grep  "/bin/bash$"  /etc/passwd
root:x:0:0:root:/root:/bin/bash
tan:x:500:500::/home/tan:/bin/bash
bash:x:501:501::/home/bash:/bin/bash
testbash:x:502:502::/home/testbash:/bin/bash
basher:x:503:503::/home/basher:/bin/bash
[root@caibird ~]# 

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

例如:

[root@caibird ~]# grep -E "\<[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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/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
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@caibird ~]# 

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

例如:

[root@caibird ~]# cat /boot/grub/grub.conf  # 源文件内容
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=86ccc263-bb4e-4c21-a2e4-4e2d4d465202 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img

*********************************************************

[root@caibird ~]# cat /boot/grub/grub.conf | grep "^[[:space:]]\+"         # 执行结果
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=86ccc263-bb4e-4c21-a2e4-4e2d4d465202 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img
[root@caibird ~]# 

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

例如:

[root@caibird ~]# grep "^#[[:space:]]\+[^[:space:]]\+" ./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@caibird ~]# grep "^#[[: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@caibird ~]# 

  • 12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@caibird ~]# netstat  -tan | grep "LISTEN[[:space:]]\+$"
tcp        0      0 0.0.0.0:111                 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      
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:42331               0.0.0.0:*                   LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::38105                    :::*                        LISTEN      
tcp        0      0 ::1:6010                    :::*                        LISTEN      
[root@caibird ~]# 

  • 13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@caibird ~]# clear
[root@caibird ~]# useradd bash
[root@caibird ~]# 
[root@caibird ~]# useradd testbash
[root@caibird ~]# useradd  basher
[root@caibird ~]# useradd nologin -s /sbin/nologin
[root@caibird ~]# tail /etc/passwd
apache:x:48:48:Apache:/var/www:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tan:x:500:500::/home/tan:/bin/bash
bash:x:501:501::/home/bash:/bin/bash
testbash:x:502:502::/home/testbash:/bin/bash
basher:x:503:503::/home/basher:/bin/bash
nologin:x:504:504::/home/nologin:/sbin/nologin

[root@caibird ~]# 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:501:501::/home/bash:/bin/bash
nologin:x:504:504::/home/nologin:/sbin/nologin
[root@caibird ~]# 

生活从不亏待每一个努力向上的人,未来的幸运都是过往努力的积攒。
今天你努力了吗?

猜你喜欢

转载自blog.csdn.net/qq_34150037/article/details/80544621
今日推荐