Linux text processing commands, user and group command exercises

Source: Internet
Author: User
Tags echo command stdin

1. List the usernames of all logged-on users on the current system. Only one user logon is displayed.

[Root @ localhost ~] # Who
Root tty1
Root pts/0 2016-08-26 05:09 (172.16.16.201)
User1 pts/1 2016-08-26 05:11 (172.16.16.201)
[Root @ localhost ~] # Who | cut-d ''-f1 | uniq root
User1

2. Retrieve the information about the user logging on to the current system.

[User1 @ localhost ~] $ Last | head-1
User1 pts/1 172.16.16.201 Fri Aug 26 05:36 still logged in

3. Retrieve the shell on the current system that the user uses as the largest default shell.


# Sort and count the 7th fields in/etc/passwd
 
[Root @ localhost ~] # Cut-d:-f7/etc/passwd | sort | uniq-c
2/bin/bash
4/bin/sh
1/bin/sync
1/sbin/halt
15/sbin/nologin
1/sbin/shutdown
      
# Sort by value
 
[Root @ localhost ~] # Cut-d:-f7/etc/passwd | sort | uniq-c | sort-n
1/bin/sync
1/sbin/halt
1/sbin/shutdown
2/bin/bash
4/bin/sh
15/sbin/nologin
      
# Obtain the shell of the last row
# Final result
 
[Root @ localhost ~] # Cut-d:-f7/etc/passwd | sort | uniq-c | sort-n | tail-1 | grep-o [^ [: space:] * $
/Sbin/nologin
4. Change the information of the last 10 users with the maximum value of the Third Field in/etc/passwd to uppercase and save the information to the/tmp/maxusers.txt file.


# Sort the fields in the/etc/passwd file by colons (:) and sort the values of the 3rd Fields. The last 10 rows are obtained.
 
[Root @ localhost ~] # Sort-n-t:-k3/etc/passwd | tail
Vcsa: x: 69: 69: virtual console memory owner:/dev:/sbin/nologin
Sshd: x: 74: 74: Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Postfix: x: 89: 89:/var/spool/postfix:/sbin/nologin
Nobody: x: 99: 99: Nobody: // sbin/nologin
Saslauth: x: 499: 76: Saslauthd user:/var/empty/saslauth:/sbin/nologin
User1: x: 500: 500:/home/user1:/bin/bash
Gentoo: x: 4001: 4001:/home/gentoo:/bin/sh
Fedora: x: 4002: 4002: Fedora Core:/home/fedora:/bin/sh
User2: x: 4003: 4003:/home/user2:/bin/sh
User3: x: 4004: 4004:/home/user3:/bin/sh
 
# Send the above results to the command "tr" in an upper-case mode through the pipeline and redirect to/tmp/users.txt.
# Final result
 
[Root @ localhost ~] # Sort-n-t:-k3/etc/passwd | tail | tr 'A-z'' a-Z'>/tmp/maxusers.txt

5. Split the command ifconfig result to retrieve the IP address of the current host.

# Retrieve the addr address in the command "ifconfig" result
 
[Root @ localhost ~] # Ifconfig | grep-E-o "addr: [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }"
Addr: 172.16.16.1
Addr: 127.0.0.1
 
# Pipeline sends the command "cut" to retrieve the address
# Final result
 
[Root @ localhost ~] # Ifconfig | grep-E-o "addr: [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3} "| cut-d:-f2
172.16.16.1127.0.0.1

6. List all file names ending with. conf in the/etc directory, convert the names to uppercase, and save them to/tmp/etc. conf.
1
[Root @ localhost ~] # Ls/etc/*. conf | tr 'A-z'' a-Z'>/tmp/etc. conf

7. Display the total number of first-level subdirectories or files under the/var Directory

[Root @ localhost ~] # Ls/var | wc-l19

8. Retrieve the names of the 10 groups with the minimum value of the 3rd fields in the/etc/group file.

# 3rd the fields are sorted by numerical values, and the first 10 rows are obtained.
 
[Root @ localhost ~] # Sort-t:-k3-n/etc/group | head
Root: x: 0:
Bin: x: 1: bin, daemon
Daemon: x: 2: bin, daemon
Sys: x: 3: bin, adm
Adm: x: 4: adm, daemon
Tty: x: 5:
Disk: x: 6:
Lp: x: 7: daemon
Mem: x: 8:
Kmem: x: 9:
 
# Send it to "cut" via MPs queue and get the group name
# Final result
 
[Root @ localhost ~] # Sort-t:-k3-n/etc/group | head | cut-d:-f1
Root
Bin
Daemon
Sys
Adm
Tty
Disk
Lp
Mem
Kmem

9. Merge the/etc/fstab and/etc/issue files into the same content and save it to the/tmp/etc. test file.

[Root @ localhost ~] # Cat/etc/fstab>/tmp/etc. test
[Root @ localhost ~] # Cat/etc/issue>/tmp/etc. test
10. User and group management command exercises

10.1 create a distro group whose GID is 2016


[Root @ localhost ~] # Tail/etc/group
[Root @ localhost ~] # Tail-1/etc/group
Distro: x: 2016:

10.2 Create a user named mandriva with the ID number 1005 and the BASIC group named distro.


[Root @ localhost ~] # Useradd-u 1005-g distro mandriva
[Root @ localhost ~] # Id mandriva
Uid = 1005 (mandriva) gid = 2016 (distro) groups = 2016 (distro)
10.3 create mageia user. The ID number is 1100, and the home directory is/home/linux


[Root @ localhost ~] # Useradd-u 1100-d/home/linux mageia
[Root @ localhost ~] # Tail-1/etc/passwd
Mageia: x: 1100: 1100:/home/linux:/bin/sh

10.4 add a password for mageia to the user. The password is mageedu.


[Root @ localhost ~] # Echo "mageedu" | passwd -- stdin mageia
Changing password for user mageia.
Passwd: all authentication tokens updated successfully.

10.5 delete mandriva but keep its home directory

You can directly delete the user's mandriva, because the default home directory is retained.

[Root @ localhost ~] # Userdel mandriva
[Root @ localhost ~] # Ls/home
Fedora gentoo linux lost + found mandriva user1 user2 user3

10.6 create the slackware user. The ID number is 2002, the BASIC group is distro, and the additional group is peguin.


[Root @ localhost ~] # Useradd-u 2002-g distro-G peguin slackware
[Root @ localhost ~] # Id slackware
Uid = 2002 (slackware) gid = 2016 (distro) groups = 2016 (distro), 5001 (peguin)

10.7 modify slackware's default shell to/bin/tcsh

[Root @ localhost ~] # Usermod-s/bin/tcsh slackware
[Root @ localhost ~] # Grep ^ slackware/etc/passwd
Slackware: x: 2002: 2016:/home/slackware:/bin/tcsh

10.8 add an additional group admins for slackware

[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 (peguin), 5003 (admins)

10.9 add a password for slackware. The minimum password validity period is 3 days, the maximum password is 180 days, and the warning is 3 days.


[Root @ localhost ~] # Echo "slackware" | passwd -- stdin slackware
Changing password for user slackware.
Passwd: all authentication tokens updated successfully.
[Root @ localhost ~] # Grep ^ slackware/etc/shadow | cut-d:-f4-6
3: 180: 3

10.10 add an openstack user. The ID number is 3003, the BASIC group is clouds, and the additional groups are peguin and nova.


[Root @ localhost ~] # Useradd-u 3003-g clouds-G peguin openstack
[Root @ localhost ~] # Usermod-a-G nova openstack
[Root @ localhost ~] # Id openstack
Uid = 3003 (openstack) gid = 5004 (clouds) groups = 5004 (clouds), 5001 (peguin), 5005 (nova)
10.11 add the system user mysql and require the shell to be/sbin/nologin.

[Root @ localhost ~] # Useradd-r-s/sbin/nologin mysql
[Root @ localhost ~] # Tail-1/etc/passwd
Mysql: x: 498: 498:/home/mysql:/sbin/nologin

10.12 use the echo command to add a password for openstack in non-interactive mode

[Root @ localhost ~] # Echo "openstack" | passwd -- stdin openstack
Changing password for user openstack.
Passwd: all authentication tokens updated successfully.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.