The Linux command uses

Source: Internet
Author: User
Tags echo command stdin

1. List the user names of all logged-in users on the current system, note: The same user logged in multiple times, only one time is displayed

[email protected] ~]# who | Cut-d '-F 1 | Sort-u

Admin

Root


2. Remove the shell that is the user's default shell on the current system.

[Email protected] ~]# CAT/ETC/PASSWD | Cut-d:-F 7 | uniq-c | Sort-n | Tail-1

25/sbin/nologino


3. Change the information in the third field in the/etc/passwd to the/tmp/maxusers.txt file with the largest number of the first 10 users.

[Email protected] ~]# CAT/ETC/PASSWD | Sort-t:-K 3-n | tail-10 | TR "A-Z" "A-Z" >/tmp/maxusers.txt

[Email protected] ~]# Cat/tmp/maxusers.txt

Abrt:x:173:173::/etc/abrt:/sbin/nologin

Pulse:x:497:495:pulseaudio SYSTEM Daemon:/var/run/pulse:/sbin/nologin

SASLAUTH:X:498:76:SASLAUTHD User:/var/empty/saslauth:/sbin/nologin

Rtkit:x:499:497:realtimekit:/proc:/sbin/nologin

Admin:x:500:500::/home/admin:/bin/bash

User1:x:501:502::/home/user1:/bin/bash

User2:x:502:503::/home/user2:/bin/bash

Centos:x:503:504::/home/centos:/bin/bash

Root123:x:504:505::/home/root123:/bin/bash

Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin

4, take out the IP address of the current host, hint: The result of ifconfig command is sliced.

[Email protected] ~]# Ifconfig | grep "\ (inet add\) | head-1 | Cut-d:-F 2 | Tr-d Bcast

192.168.10.103


5. Displays the total number of sub-directories or files in the/var directory.

[Email protected] ~]# Tree-l 1/var | Wc-l

24


6. Remove the names of the 10 groups with the smallest number of the third field in the/etc/group file.

[Email protected] ~]# Cat/etc/group | Sort-t:-K 3-n | head-10 | Cut-d:-F 1

Root

Bin

Daemon

Sys

cd/

Tty

Disk

Lp

Mem

Kmem


7, the contents of the/etc/fstab and/etc/issue files are merged into the same content and saved to the/tmp/etc.test file.

[Email protected] ~]# cat/etc/fstab/etc/issue >/tmp/etc.text

[Email protected] ~]# Cat/tmp/etc.text


#

#/etc/fstab

# Created by Anaconda on Fri Nov 25 05:32:50 2016

#

# Accessible filesystems, by reference, is maintained under '/dev/disk '

# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

/DEV/MAPPER/VOLGROUP-LV_ROOT/EXT4 Defaults 1 1

Uuid=40cea874-31c8-404a-9463-32c09bad241c/boot EXT4 Defaults 1 2

/dev/mapper/volgroup-lv_swap swap swap defaults 0 0

TMPFS/DEV/SHM TMPFS Defaults 0 0

Devpts/dev/pts devpts gid=5,mode=620 0 0

Sysfs/sys Sysfs Defaults 0 0

PROC/PROC proc Defaults 0 0

CentOS Release 6.8 (Final)

Kernel \ r on an \m


8. Summarize the methods used to describe the user and group management commands and complete the following exercises:

User created: Useradd

-u:uid, defined in/etc/login.defs

-G:GID Specifies the base group to which the user belongs, either as a group name or as a GID

-C: User's comment information

-D: The specified path is the home directory.

-S: Specifies the user's default shell program, the available list is in/etc/shells

-G: Indicates additional groups for the user.

-r: Create a System user.


Group creation: Groupadd

-G: Specify GID number


User Property Modification: Usermod

-U: New UID

-G: New Basic Group

-G: The new additional group, the original additional group will be overwritten, to retain the original, you also want to use the-a option.

-S: New shell

-C: New annotation information

-D: New home directory, files in the original home directory will not be moved to the new home directory at the same time, to move, then use the-M option

-l:login Name: The new name

-l:lock the specified user

-u:unlock the specified user

-E:YYYY-MM-DD indicates the expiration date of the user account

-F: Set the duration of inactivity


Group Property Modification: Groupmod

-N: New name

-G: New GID


User Add Password: passwd (Modify the password of the specified user, only root user rights)

passwd: Change your password

-L: Lock the specified user

-U: Unlock the specified user

-n:mindays: Specify the shortest period of use

-x:maxdays: Maximum use Period

-I: Inactivity period

--stdin: Receives the user password from the standard input.

echo "passwd" | passwd--stdin USERNAME


Group Password: gpasswd

-A: Add user to the specified group

-D: Delete the user's additional group with the current group name

-A: Set the list of users who manage permissions



Delete User: Userdel

-r: Delete User's home directory


Delete Group: Groupdel

Groupdel GROUP


Temporary Switch Base Group: NEWGRP

If the user does not belong to this group, a password is required


To modify a user's properties: chage

-D Last_day

-E,--expiredate expire_date

-I.,--inactive inactive

-M,--mindays min_days

-M,--maxdays max_days

-W,--warndays Warn_days


To view the user's related ID information: ID

-u:uid

-g:gid

-g:group

-n:name


To switch users or perform as a different user: Su

Su UserName: non-login switch

Su-username: Login switch. The target user's profile is read and completely switched.

Note:root to other users without a password, a password is required for non-root user Switching.

A different Identity execution command: su-username-c ' command '


To modify some specific information: CHFN

Modify the new Shell:chsh

Show user details: Finger


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

[Email protected] ~]# Groupadd distro-g 2016

[Email protected] ~]# Cat/etc/group | Tail-1

DISTRO:X:2016:


(2), create user Mandriva, whose ID number is 1005; basic group is distro;

[Email protected] ~]# useradd mandriva-u 1005-g distro


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

[Email protected] ~]# useradd mageia-u 1100-d/home/linux

[[Email Protected]gon ~]# cat/etc/passwd | Tail-1

Mageia:x:1100:1100::/home/linux:/bin/bash


(4), to the user mageia add password, password for mageedu;

[Email protected] ~]# passwd Mageia

Changing password for user Mageia.

New Password:

Bad Password:it are based on a dictionary word

Bad Password:is too simple

Retype new Password:

Passwd:all authentication tokens updated successfully.


(5), delete Mandriva, but retain their home directory;

[Email protected] ~]# Userdel Mandriva

[Email protected] ~]# ls/home/

Admin CentOS Linux Mandriva root123 user1 user2


(6), create user Slackware, its ID number is 2002, the basic group is distro, additional group Peguin;


[Email protected] ~]# Groupadd Peguin

[Email protected] ~]# useradd slackware-u 2002-g distro-g Peguin

(7), modify the Slackware default shell for/BIN/TCSH;

[Email protected] ~]# usermod slackware-s/bin/tcsh

[Email protected] ~]# CAT/ETC/PASSWD | Tail-1

Slackware:x:2002:2016::/home/slackware:/bin/tcsh



(8), add additional Group admins for user Slackware;

Usermod slackware-ag Admins

[[email protected] ~]# ID Slackware

uid=2002 (Slackware) gid=2016 (distro) groups=2016 (distro), 2018 (admins)


(9), add password for Slackware, and require the minimum password age of 3 days, the maximum is 180 days, warning for 3 days

[Email protected] ~]# passwd-n 3-x 180-w 3 Slackware

[Email protected] ~]# Cat/etc/shadow | Tail-1

slackware:!! : 17155:3:180:3:::


(10), add user OpenStack, whose ID number is 3003, the basic group is clouds, the additional group is Peguin and Nova;

[Email protected] ~]# useradd openstack-u 3003-g clouds-g Peguin,nova

[[email protected] ~]# ID OpenStack

uid=3003 (OpenStack) gid=2020 (Clouds) groups=2020 (Clouds), Peguin (Nova)


(11), add the system user MySQL, request its shell for/sbin/nologin;

[Email protected] ~]# useradd mysql-s/sbin/nologin

[Email protected] ~]# CAT/ETC/PASSWD | Tail-1

Mysql:x:3004:3004::/home/mysql:/sbin/nologin



(12), use the echo command, non-interactive add a password for OpenStack.

[Email protected] ~]# echo "11qqwww" | passwd--stdin OpenStack

Changing password for user OpenStack.

Passwd:all authentication tokens updated successfully.


9, the replication/etc/skel directory is/home/tuser1, requires/home/tuser1 and its internal files belong to the group and other users do not have any access rights.

[Email protected] ~]# cp-r/etc/skel/home/tuser1 && chmod-r go=/home/tuser1


10. Display lines in the/proc/meminfo file that begin with uppercase or lowercase s; in two ways

[Email protected] ~]# grep-i "^s"/proc/meminfo

[[email protected] ~]# grep "^[ss]"/proc/meminfo

swapcached:0 KB

swaptotal:2064380 KB

swapfree:2064380 KB

shmem:3844 KB

slab:139288 KB

sreclaimable:94680 KB

sunreclaim:44608 KB


11. Display the default shell of the/etc/passwd file as a non-/sbin/nologin user;

[Email protected] ~]# grep-v ". *\ (/sbin/nologin\) $"/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Sync:x:5:0:sync:/sbin:/bin/sync

Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Halt:x:7:0:halt:/sbin:/sbin/halt

Admin:x:500:500::/home/admin:/bin/bash

User1:x:501:502::/home/user1:/bin/bash

User2:x:502:503::/home/user2:/bin/bash

Centos:x:503:504::/home/centos:/bin/bash

Root123:x:504:505::/home/root123:/bin/bash

Mageia:x:1100:1100::/home/linux:/bin/bash

Slackware:x:2002:2016::/home/slackware:/bin/tcsh

Openstack:x:3003:2020::/home/openstack:/bin/bash


12. Display the user whose default shell is/bin/bash in/etc/passwd file;

[[email protected] ~]# grep ". *\ (/bin/bash\) $"/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Admin:x:500:500::/home/admin:/bin/bash

User1:x:501:502::/home/user1:/bin/bash

User2:x:502:503::/home/user2:/bin/bash

Centos:x:503:504::/home/centos:/bin/bash

Root123:x:504:505::/home/root123:/bin/bash

Mageia:x:1100:1100::/home/linux:/bin/bash

Openstack:x:3003:2020::/home/openstack:/bin/bash



13. Find out one or two digits in the/etc/passwd file;

[Email protected] ~]# grep-o "\<[0-9]\{2,3\}\>"/etc/passwd

12

10

14

11

12

100

13

30

14

50

99

99



14, the display/etc/rc.d/rc.sysinit file with the beginning of #, followed by at least one white space character, and then have at least one non-whitespace character line;

[[email protected] ~]# grep "^#[[:space:]]\+[^[:space:]]"/etc/rc.d/rc.sysinit

#/etc/rc.d/rc.sysinit-run once at boot time

# taken in part from Miquel van Smoorenburg ' s BCHECKRC.

# Check SELinux Status

# Print a text banner.

# only read this once.

# Initialize Hardware

# Set Default Affinity



15, the Netstat-tan command execution results in the "LISTEN", followed by or with a blank character end of the line;

[Email protected] ~]# Netstat-tan | grep ". *listen[[:space:]]\+$"

TCP 0 0 0.0.0.0:111 0.0.0.0:* LISTEN

TCP 0 0 0.0.0.0:39476 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

TCP 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

TCP 0 0::: 111:::* LISTEN

TCP 0 0::: 53232:::* LISTEN


16, add user bash, Testbash, basher, Nologin (this one user's shell is/sbin/nologin), and then find the current system on its user name and the default shell of the same user information;

[[email protected] ~]# grep "^\ ([[: alnum:]]\+\). *\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

Nobody:x:99:99:nobody:/:/sbin/nologin

Ntp:x:38:38::/etc/ntp:/sbin/nologin

Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin

Bash:x:3005:3005::/home/bash:/bin/bash

Basher:x:3007:3007::/home/basher:/bin/bash

Nologin:x:3008:3008::/home/nologin:/sbin/nologin



The Linux command uses

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.