linux 的用户,用户组手动添加及管理命令和正则表达式用法示例

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

cp -rv /etc/skel/ /home/tuser1
----------------------------------------
[root@localhost ~]# cp -rv /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’
----------------------------------------

chmod -R go= /home/tuser1
----------------------------------------
[root@localhost ~]# chmod -R go= /home/tuser1
[root@localhost ~]# ls -ld /home/tuser1/
drwx------. 3 root root 74 Mar  2 23:39 /home/tuser1/
----------------------------------------

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

vim  /etc/group 按o键进入编辑模式,输入:hadoop:x:2005
按esc键进入命令模式,按shift+:进入末行模式输入
:wq 回车键保存退出
----------------------------------------
[root@localhost ~]# grep hadoop /etc/group
hadoop:x:2005:
----------------------------------------

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

mkdir -v /home/hadoop
----------------------------------------
[root@localhost ~]# mkdir -v /home/hadoop
mkdir: created directory ‘/home/hadoop’
----------------------------------------
vim /etc/passwd 按o键进入编辑模式输入:
hadoop:x:2005:2005:hadoop:/home/hadoop:/bin/bash
按esc键进入命令模式,按shift+:进入末行模式输入
:wq 回车键保存退出
----------------------------------------
[root@localhost ~]# grep hadoop /etc/passwd
hadoop:x:2005:2005:hadoop:/home/hadoop:/bin/bash
----------------------------------------

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

cp -rv /etc/skel/ /home/hadoop 
----------------------------------------
[root@localhost ~]# cp -rv /etc/skel/ /home/hadoop 
‘/etc/skel/’ -> ‘/home/hadoop/skel’
‘/etc/skel/.mozilla’ -> ‘/home/hadoop/skel/.mozilla’
‘/etc/skel/.mozilla/extensions’ -> ‘/home/hadoop/skel/.mozilla/extensions’
‘/etc/skel/.mozilla/plugins’ -> ‘/home/hadoop/skel/.mozilla/plugins’
‘/etc/skel/.bash_logout’ -> ‘/home/hadoop/skel/.bash_logout’
‘/etc/skel/.bash_profile’ -> ‘/home/hadoop/skel/.bash_profile’
‘/etc/skel/.bashrc’ -> ‘/home/hadoop/skel/.bashrc’
----------------------------------------
chmod go= /home/hadoop
----------------------------------------
[root@localhost ~]# chmod go= /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop/
drwx------. 3 root root 17 Mar  2 23:57 /home/hadoop/
----------------------------------------

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

chown -Rv hadoop:hadoop /home/hadoop/
----------------------------------------
[root@localhost ~]# chown -Rv hadoop:hadoop /home/hadoop/
changed ownership of ‘/home/hadoop/skel/.mozilla/extensions’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel/.mozilla/plugins’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel/.mozilla’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel/.bash_logout’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel/.bash_profile’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel/.bashrc’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/skel’ from root:root to hadoop:hadoop
changed ownership of ‘/home/hadoop/’ from root:root to hadoop:hadoop
----------------------------------------

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

  方式1 grep -i ^s /proc/meminfo
----------------------------------------
[root@localhost ~]# grep -i ^s /proc/meminfo
SwapCached:            0 kB
SwapTotal:       4079612 kB
SwapFree:        4079612 kB
Shmem:             10036 kB
Slab:             153420 kB
SReclaimable:      90028 kB
SUnreclaim:        63392 kB
----------------------------------------
  方式2 egrep "^(s|S)" /proc/meminfo
----------------------------------------
[root@localhost ~]# egrep "^(s|S)" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       4079612 kB
SwapFree:        4079612 kB
Shmem:             10036 kB
Slab:             153436 kB
SReclaimable:      90028 kB
SUnreclaim:        63408 kB
----------------------------------------

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

cat /etc/passwd | grep -v "/sbin/nologin" | cut -d: -f1
----------------------------------------
[root@localhost ~]# cat /etc/passwd | grep -v "/sbin/nologin" | cut -d: -f1
root
sync
shutdown
halt
linuxprobe
hadoop
----------------------------------------

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

cat /etc/passwd | grep "/bin/bash" | cut -d: -f1
----------------------------------------
[root@localhost ~]# cat /etc/passwd | grep "/bin/bash" | cut -d: -f1
root
linuxprobe
hadoop
----------------------------------------

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

  方法一:grep "\<[0-9]\{1,2\}\>" /etc/passwd
----------------------------------------
[root@localhost ~]# grep "\<[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
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
----------------------------------------
  方法二:grep "\<[0-9][0-9]\?\>" /etc/passwd
----------------------------------------
[root@localhost ~]# grep "\<[0-9][0-9]\?\>" /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
----------------------------------------

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

redhat7版本没有/boot/grub/grub.conf文件,替换成/boot/grub2/grub.cfg

 方式1:grep "^[[:space:]]\{1,\}"/boot/grub2/grub.cfg
----------------------------------------
[root@localhost ~]# grep "^[[:space:]]\{1,\}" /boot/grub2/grub.cfg
 load_env
  set default="${next_entry}"
  set next_entry=
  save_env next_entry
  set boot_once=true
  set default="${saved_entry}"
 menuentry_id_option="--id"
----------------------------------------
 方式2:grep "^[[:space:]]\+" /boot/grub2/grub.cfg
----------------------------------------
[root@localhost ~]# grep "^[[:space:]]\+" /boot/grub2/grub.cfg
 load_env
  set default="${next_entry}"
  set next_entry=
  save_env next_entry
  set boot_once=true
  set default="${saved_entry}"
 menuentry_id_option="--id"
----------------------------------------

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

redhat7版本没有/etc/rc.d/sysinit文件,替换成 /etc/rc.d/rc.local

grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"  /etc/rc.d/sysinit
----------------------------------------
[root@localhost ~]# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"  /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 constrast 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’,后或跟空白字符结尾的行;

netstat -tan | grep  "LISTEN*[[:space:]]"
----------------------------------------
[root@localhost ~]# netstat -tan | grep  "LISTEN*[[:space:]]"
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:38810           0.0.0.0:*               LISTEN     
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     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::36628                :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
----------------------------------------

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

useradd   bash   
----------------------------------------
[root@localhost ~]# id bash
uid=2006(bash) gid=2006(bash) groups=2006(bash)
----------------------------------------
useradd  testbash  
----------------------------------------
[root@localhost ~]# id testbash
uid=2007(testbash) gid=2007(testbash) groups=2007(testbash)
----------------------------------------
useradd  basher 
----------------------------------------
[root@localhost ~]# id basher
uid=2008(basher) gid=2008(basher) groups=2008(basher)
----------------------------------------
useradd -s /sbin/nologin nologin
----------------------------------------
[root@localhost ~]# grep "nologin" /etc/passwd | tail -1
nologin:x:2009:2009::/home/nologin:/sbin/nologin
----------------------------------------
grep  "^\([[:alnum:]]\)\{1,\}:.*\1$"  /etc/passwd
----------------------------------------
[root@localhost ~]# grep  "^\([[:alnum:]]\)\{1,\}:.*\1$"  /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/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
bash:x:2006:2006::/home/bash:/bin/bash
testbash:x:2007:2007::/home/testbash:/bin/bash
nologin:x:2009:2009::/home/nologin:/sbin/nologin
----------------------------------------

你可能感兴趣的:(linux 的用户,用户组手动添加及管理命令和正则表达式用法示例)