1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
[root@localhost ~]# who
root???? pts/0??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/1??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/2??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/3??????? 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | cut -d” ” -f1
root
root
root
root
[root@localhost ~]# who | cut -d” ” -f1 | sort
root
root
root
root
[root@localhost ~]# who | cut -d” ” -f1 | sort | uniq
root
[root@localhost ~]# who | cut -d” ” -f1 | uniq
root
2、取出最后登錄到當前系統的用戶的相關信息。
[root@localhost ~]# who
root???? pts/0??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/1??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/2??????? 2017-12-16 22:46 (192.168.43.233)
root???? pts/3??????? 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | tail -1
root???? pts/3??????? 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | tail -1 | cut -d” ” -f1 | id
uid=0(root) gid=0(root) groups=0(root)
3、取出當前系統上被用戶當作其默認shell的最多的那個shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7
/bin/bash
……
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c
1 /bin/bash
4 /sbin/nologin
1 /bin/sync
1 /sbin/shutdown
1 /sbin/halt
10 /sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | sort -nr
10 /sbin/nologin
4 /sbin/nologin
1 /sbin/shutdown
1 /sbin/halt
1 /bin/sync
1 /bin/bash
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | sort -nr | head -1
10 /sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | tail -1
10 /sbin/nologin
4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# sort -t: -k3 -n /etc/passwd
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr ‘a-z’ ‘A-Z’
MAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGIN
……
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr ‘a-z’ ‘A-Z’ > /tmp/maxusers.txt
[root@localhost ~]# cat /tmp/maxusers.txt
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
SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN
DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN
POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN
NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:192:192:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
POLKITD:X:999:997:USER FOR POLKITD:/:/SBIN/NOLOGIN
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
[root@localhost ~]# ifconfig ens33 | egrep -o ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.?){4}’
192.168.43.191
255.255.255.0
192.168.43.255
[root@localhost ~]# ifconfig ens33 | egrep -o ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.?){4}’ | head -1
192.168.43.191
6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
[root@localhost ~]# find /etc -name “*.conf” | tr ‘a-z’ ‘A-Z’ > /tmp/etc.conf
[root@localhost ~]# cat /tmp/etc.conf
/ETC/OPENLDAP/LDAP.CONF
……
7、顯示/var目錄下一級子目錄或文件的總個數。
[root@localhost ~]# ls /var | wc -l
8、取出/etc/group文件中第三個字段數值最小的10個組的名字。
[root@localhost ~]# sort -t: -k3 -n /etc/group | head
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
[root@localhost ~]# sort -t: -k3 -n /etc/group | head | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
9、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/fstab /etc/issue
#
# /etc/fstab
# Created by anaconda on Thu Nov 30 17:46:50 2017
#
# 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
#
UUID=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 /?????????????????????? ext4??? defaults??????? 1 1
UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot?????????????????? ext4??? defaults??????? 1 2
UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap??????????????????? swap??? defaults??????? 0 0
\S
Kernel \r on an \m
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@localhost ~]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Thu Nov 30 17:46:50 2017
#
# 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
#
UUID=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 /?????????????????????? ext4??? defaults??????? 1 1
UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot?????????????????? ext4??? defaults??????? 1 2
UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap??????????????????? swap??? defaults??????? 0 0
\S
Kernel \r on an \m
10、請總結描述用戶和組管理類命令的使用方法并完成以下練習:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
練習1:創建用戶gentoo,UID為4001,基本組為gentoo,附加組為distro(GID為5000)和peguin(GID為5001);
[root@localhost ~]# groupadd -g 5000 distro
[root@localhost ~]# groupadd -g 5001 penguin
[root@localhost ~]# useradd gentoo -u 4001 -G distro,penguin
[root@localhost ~]# tail -1 /etc/passwd
gentoo:x:4001:4001::/home/gentoo:/bin/bash
[root@localhost ~]# tail -3 /etc/group
distro:x:5000:gentoo
penguin:x:5001:gentoo
gentoo:x:4001:
練習2:創建用戶fedora,其注釋信息為”Fedora Core”,默認shell為/bin/tcsh;
[root@localhost ~]# useradd fedora -s /bin/tcsh -c “Fedora Core”
[root@localhost ~]# tail -1 /etc/passwd
fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/tcsh
練習3:修改gentoo用戶的家目錄為/var/tmp/gentoo;要求其原有文件仍能被用戶訪問;
[root@localhost ~]# usermod -d /var/tmp/gentoo -m gentoo
練習4:為gentoo新增附加組netadmin;
[root@localhost ~]# groupadd netadmin
[root@localhost ~]# usermod -a -G netadmin gentoo
[root@localhost ~]# id gentoo
uid=4001(gentoo) gid=4001(gentoo) groups=4001(gentoo),5000(distro),5001(penguin),5002(netadmin)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
??? |
|
|
|
|
|
|
(1)、創建組distro,其GID為2016;
[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# tail -1 /etc/group
distro:x:2016:
(2)、創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro
[root@localhost ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux
[root@localhost ~]# tail -n 1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
(4)、給用戶mageia添加密碼,密碼為mageedu;
[root@localhost ~]# echo “mageedu” | passwd –stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# tail -1 /etc/shadow
mageia:$6$p2GTrIVS$lsnXrakfyBgdIKKCtnZH5hhDr4MNOik/pZDRiOXtP5hG3g.qR0A2dKXDTUj8vvgdKi.7ARnDWMeD99V3CrjmA1:17516:0:99999:7:::
(5)、刪除mandriva,但保留其家目錄;
[root@localhost ~]# userdel mandriva
(6)、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@localhost ~]# groupadd penguin
[root@localhost ~]# useradd slackware -u 2002 -g distro -G penguin
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(penguin)
(7)、修改slackware的默認shell為/bin/tcsh;
[root@localhost ~]# usermod slackware -s /bin/tcsh
[root@localhost ~]# tail -1 /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、為用戶slackware新增附加組admins;
[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -a -G admins slackware
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(penguin),5003(admins)
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/90189
贊~實驗步驟的每一步都描述出來,對學習理解有很大幫助~中間的腦圖總結的知識點也非常到位,非常贊的一篇~加油