linux user and group commands collated and detailed

  • 2020-05-10 23:24:58
  • OfStack

1. List all the user names that have logged in on the current system. Note: if the same user logs in multiple times, it will only be displayed once.


[root@node2 ~]# who 
root   pts/0    2016-10-08 13:28 (10.106.64.41)
root   pts/1    2016-10-08 13:44 (10.106.64.41)
root   pts/2    2016-10-08 14:22 (10.106.65.82)
root   pts/3    2016-10-08 14:22 (10.106.65.82)
root   pts/4    2016-10-08 15:26 (10.106.64.41)
root   pts/5    2016-10-08 15:26 (10.106.64.41)
root   pts/6    2016-10-08 15:26 (10.106.64.41)
[root@node2 ~]# who -m
root   pts/4    2016-10-08 15:26 (10.106.64.41)

2. Take out the relevant information of the user who logged into the current system last.


[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

3. Take out the shell with the most default shell on the current system


[root@node2 ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail   
1 /bin/sync   
1 /sbin/halt   
1 /sbin/shutdown   
2 /bin/bash   
20 /sbin/nologin
[root@node2 ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -1
20 /sbin/nologin

4. Change all the information of the last 10 users with the largest value in the third field of /etc/passwd to uppercase and save it to /tmp/ maxusers.txt file.


[root@node2 ~]# cat /etc/passwd | sort -nk 3 -t : | tail |tr '[a-z]' '[A-Z]' > /tmp/maxusers.txt
[root@node2 ~]# [root@node2 ~]# cat /tmp/maxusers.txt 
POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGINNOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGINAVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGINABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGINRABBITMQ:X:995:993:RABBITMQ MESSAGING SERVER:/VAR/LIB/RABBITMQ:/SBIN/NOLOGINEPMD:X:996:994:ERLANG PORT MAPPER DAEMON:/TMP:/SBIN/NOLOGINPOLKITD:X:997:995:USER FOR POLKITD:/:/SBIN/NOLOGINSYSTEMD-NETWORK:X:998:996:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGINSYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGINUSER1:X:1000:1000::/HOME/USER1:/BIN/BASH

5. Take out the IP address of the current host, and prompt: shard the results of the ifconfig command.


[root@node2 ~]# ifconfig ens160 | grep netmask | cut -b 14-27
10.100.146.111

6. List the file names of all files ending in.conf in /etc and save them to /tmp/ etc.conf after changing them to uppercase.


[root@node2 ~]# basename -a /etc/*.conf | tr '[a-z]' '[A-Z]' > /tmp/etc.conf
[root@node2 ~]# cat /tmp/etc.conf 
ASOUND.CONF
DNSMASQ.CONF
DRACUT.CONF
E2FSCK.CONF
HOST.CONF
KDUMP.CONF
KRB5.CONF
LD.SO.CONF
LFTP.CONF
LIBAUDIT.CONF
LIBUSER.CONF
LOCALE.CONF
LOGROTATE.CONF
MAN_DB.CONF
MKE2FS.CONF
NSSWITCH.CONF
RESOLV.CONF
RSYNCD.CONF
RSYSLOG.CONF
SESTATUS.CONF
SOS.CONF
SUDO.CONF
SUDO-LDAP.CONF
SYSCTL.CONF
TCSD.CONF
UPDATEDB.CONF
VCONSOLE.CONF
YUM.CONF

7. Lists the total number of level 1 subdirectories or files under /var.


[root@node2 ~]# ls -l /var/ | wc -l
21

8. Fetch the names of the 10 groups with the smallest value of the third field in the /etc/group file.


[root@node2 ~]# cat /etc/group | sort -nk 3 -t : | head | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem

9. Merge the contents of /etc/fstab and /etc/issue files into the same file and save them to /tmp/ etc.test.


[root@node2 ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@node2 ~]# cat /tmp/etc.test 
## /etc/fstab# Created by anaconda on Fri Aug 26 14:12:50 2016## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#/dev/mapper/centos-root /            xfs   defaults    0 0/dev/mapper/centos-app /app          xfs   defaults    0 0UUID=b0abaaff-b81c-4a29-99ff-04d4df4d5c1b /boot          xfs   defaults    0 0/dev/mapper/centos-swap swap          swap  defaults    0 0\SKernel \r on an \m

10. Summarize and describe the use of user and group management commands and complete the following exercise:


1 ) useradd : create a user 
 useradd [ options ]  Login name 
 -u, --uid UID : specify UID ;  
 -g, --gid GROUP : specifies the base group ID , the group must exist in advance;  
 -G, --groups GROUP1[,GROUP2,...[,GROUPN]]] : indicates the additional group to which the user belongs, separated by commas;  
 -c, --comment COMMENT : specify annotation information;  
 -d, --home HOME_DIR : take the specified path as the user's home directory; By copying /etc/skel Rename the directory implementation; The specified home directory path does not copy the environment profile for the user if it already exists;  
 -s, --shell SHELL : specifies the user's default shell , all available shell The list is stored in /etc/shells File;  
 -r, --system : create system users; 

[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

0

3 ) userdel : Delete user 
  -r : when a user is deleted 1 And delete its home directory; 

4 ) passwd: Set user password 
passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

(1) passwd : modify the user's own password; 
(2) passwd USERNAME : changes the password of the specified user, but only root Has this permission; 

  -l, -u : locks and unlocks users;   
  -d : clear user password string;   
  -e DATE:  Expiration date;   
  -i DAYS : inactive period;   
  -n DAYS : minimum lifetime of password;   
  -x DAYS : the maximum usage period of the password;   
  -w DAYS : warning period; 
  --stdin :   
   echo "PASSWORD" | passwd --stdin USERNAME


[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

3

[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

4

(1) create group distro, whose GID is 2016;


[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

5

(2) create user mandriva, whose ID is 1005; The base group was distro;


[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

6

(3) create user mageia, whose ID number is 1100, and whose home directory is /home/linux;


[root@node2 ~]# useradd -u 1100 -d /home/linux mageia
[root@node2 ~]# id mageia
uid=1100(mageia) gid=1100(mageia)  group =1100(mageia)

(4) add a password to user mageia, and the password is mageedu;


[root@node2 ~]# passwd mageia
 Change user  mageia  The password   . 
 The new   Password: 
 Invalid password:   The password is less than  8  A character 
 Reenter new   Password: 
passwd : all authentication tokens have been successfully updated. 

(5) delete mandriva, but keep its home directory;


[root@node2 ~]# last -1
root   pts/6    10.106.64.41   Sat Oct 8 15:26  still logged in  

 
wtmp begins Fri Aug 26 14:31:15 2016

9

(6) create user slackware, whose ID number is 2002, the basic group is distro, and the additional group is peguin;


[root@node2 ~]# groupadd peguin
[root@node2 ~]# useradd -u 2002 -g distro -G peguin slackwar
e[root@node2 ~]# id slackware
uid=2002(slackware) gid=2016(distro)  group =2016(distro),2017(peguin)

(7) modify the default shell of slackware to be /bin/tcsh;


[root@node2 ~]# usermod -s /bin/tcsh slackware
[root@node2 ~]# grep slackware /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh

(8) add additional group admins for user slackware;


[root@node2 ~]# groupadd admins
[root@node2 ~]# usermod -G admins slackware
[root@node2 ~]# id slackware
uid=2002(slackware) gid=2016(distro)  group =2016(distro),2018(admins)

(9) add a password with the bit slackware, and the minimum usage period of the password is 3 days, the maximum is 180 days, and the warning is 3 days;


[root@node2 ~]# passwd -n 3 -x 180 -w 3 slackware
 Adjust user password aging data slackware . 
passwd:  Operation is successful 

(10) add user openstack, whose ID is 2003, the basic group is clouds, and the additional group is peguin and nova;


[root@node2 ~]# useradd -u 2003 -g clouds -G peguin -G nova openstack
[root@node2 ~]# id openstack
uid=2003(openstack) gid=2020(clouds)  group =2020(clouds),2019(nova)

(11) add system user mysql, requiring its shell to be /sbin/nologin;


[root@node2 ~]# useradd -s /sbin/nologin mysql

(12) use the echo command to add passwords to openstack non-interactively.


[root@node2 ~]# echo '123.abc' | passwd --stdin openstack Change user  openstack  The password   . 
passwd : all authentication tokens have been successfully updated. 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: