linux system User management with grep Regular Expressions sample tutorial
- 2020-11-03 22:39:55
- OfStack
preface
This article mainly introduces linux system user management and grep regular expressions related content, share for your reference and learning, the following words do not say much, let's start with a detailed introduction.
linux system user management with grep regular expressions
1. Copy /etc/skel directory as /home/tuser1, and require that /home/tuser1 and its internal files belong to groups and other users without any access rights.
[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. Edit /etc/group file and add group hadoop.
[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029
3. Manually edit /etc/passwd file by adding a new line, and add user hadoop, whose basic group ID is the NUMBER id of the hadoop group; The home directory is /home/hadoop.
[root@suywien ~]# vim /etc/passwd
[root@suywien ~]# tail -1 /etc/passwd
hadoop:x:1029:1029::/home/hodoop/:/bin/bash
4. Copy /etc/skel directory as /home/hadoop, requiring modification of hadoop directory's subgroups and other users without any access rights.
[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. Modify the /home/hadoop directory and all files in it. The owner is hadoop, and the owner group is 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. Display the lines beginning with uppercase or lowercase S in /proc/meminfo file; In two ways;
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. Display the default shell in /etc/passwd file as a non-/ sbin/nologin user;
[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. Display the user of /etc/passwd whose default shell is /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. Find out the one-digit or two-digit number in /etc/passwd file;
[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, display/boot grub/grub conf begin with at least one blank characters in the line;
[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029
0
11, show/etc/rc d/rc sysinit file begins with #, followed by at least one blank character, then at least 1 non-white-space characters;
[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029
1
12. Type the line ending with 'LISTEN' in the result of the netstat-ES100en command, followed by a blank character;
[root@suywien ~]# vim /etc/group
[root@suywien ~]# tail -1 /etc/group
hadoop:x:1029
2
13. Add user bash, testbash, basher, nologin (shell for this user is /sbin/nologin), and then find out the user information with the same user name and default shell on the current system;
[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
conclusion