Homework week03
This week's assignment:
1. List the user names of all logged-in users on the current system. Note: if the same user has logged in multiple times, it can only be displayed once.
[root@captain ~] # who | cut-D''- F1 | sort-uicheckoracleroot
2. Take out the relevant information of the user who last logged in to the current system.
[root@captain shell] # cat getuserinfo.sh #! / bin/bash# author: Felix Ruan# getuser basic information# date:20160820# v1.0if [`id-u`-ne 0] Then echo "only root can execute this script" exit 1fireadonly User=$ {1head-`last | head-1 | cut-d'-f1`} readonly PASSWD=/etc/passwdfunction getuginfo () {echo "basic information for last login user: $1" echo "user name: `id-nu $1`, uid: `id-u $1`" echo "group name: `id-ng $1` Gid: `local ushell=-g $1` "} function gethome () {echo" user home: `grep-w ^ $1$ PASSWD | cut-dudes PASSWD'- f6` "} function getshell () {local ushell= `grep-w" ^ $1 "$PASSWD | cut-dazzles'- f7` case $ushell in * sh) echo" user shell: $ushell " ; ") echo" user shell: / bin/sh ";; *) Esac} function pwdinfo () {echo "password expiry information:" chage-l $1 | egrep-e'(Password | Account) expires'-e 'Maximum number'} function main () {getuginfo $1 echo'-- 'gethome $1 echo'-- -'getshell $1 echo'-'pwdinfo $1 echo} main $User [root@captain shell] # sh getuserinfo.sh basic information for last login user: oracleuser name: oracle Uid: 500group name: dba Gid: 500--user home: / root/oracle--user shell: / bin/bash--password expiry information:Password expires : neverAccount expires: neverMaximum number of days between password change: 99999
3. Take out the most shell on the current system that users regard as their default shell.
[root@captain ~] # awk-F ":" 'BEGIN {printf ("Shell\ t\ tCount\ n")}\ > {a [$NF] +} END {\ > for (i in a)\ > if (i~/sh$/)\ > {if (a [I] > cnt) cnt=a [I] Sh=i}\ > {printf ("% s\ t% d\ n", sh, cnt)}}'/ etc/passwdShell Count/bin/bash 7 [root@captain ~] #
4. Change the information of the last 10 users with the largest value in the third field in / etc/passwd to uppercase and save it to the / tmp/maxusers.txt file.
[root@captain ~] # cat / etc/passwd | sort-tweezer'- k3-n | tail-10 > / tmp/maxusers.txt [root@captain ~] # cat / tmp/maxusers.txtpostfix:x:89:89::/var/spool/postfix:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologindhcpd:x:177:177:DHCP server:/:/sbin/nologinnginx:x:497 : 497:nginx user:/var/cache/nginx:/sbin/nologinmongod:x:498:498:mongod:/var/lib/mongo:/bin/falsesaslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinoracle:x:500:500::/home/oracle:/bin/bashicheck:x:501:500::/home/icheck:/bin/bashnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
5. Take out the IP address of the current host, and prompt: split the result of the ifconfig command.
[root@captain ~] # for nic in `ifconfig-a | cut-d''- F1 | grep-v 'lo' `> do > ifconfig $nic | grep' inet addr' |\ > awk-v var=$nic-F' [:] +'{printf ("% s:% s\ n", var, $4)}'> doneeth0: 192.168.162.134eth2: 192.168.1.10eth2:0: 192.168.10.10 [root@captain ~] #
6. List the file names of all files ending in .conf in the / etc directory, and convert their names to uppercase and save them to the / tmp/etc.conf file.
[root@captain etc] # ls * .confasound.conf gai.conf libuser.conf numad.conf sos.confautofs.conf grub.conf logrotate.conf oddjobd.conf sudo.confautofs_ldap_auth.conf gssapi_mech.conf ltrace.conf pm-utils-hd-apm-restore.conf sudo-ldap.confcas.conf Host.conf mke2fs.conf prelink.conf sysctl.confcgconfig.conf idmapd.conf mongod.conf readahead.conf Trolltech.confcgrules.conf kdump.conf named.conf request-key.conf updatedb.confcgsnapshot_blacklist.conf krb5.conf nfsmount.conf resolv.conf Warnquota.confdracut.conf latrace.conf nsswitch.conf rsyslog.conf xinetd.confelinks.conf ld.so.conf ntp1.conf sestatus.conf yp.conffprintd.conf libaudit.conf ntp.conf smartd.conf yum.conf [root@captain etc] # ls *. Conf | tr 'amurz` 'Amurz' > / tmp/etc.conf [root@captain etc] # cat / tmp/etc.confASOUND.CONFAUTOFS.CONFAUTOFS_LDAP_AUTH.CONFCAS.CONFCGCONFIG.CONFCGRULES.CONFCGSNAPSHOT_BLACKLIST.CONFDRACUT.CONFELINKS.CONFFPRINTD.CONFGAI.CONFGRUB.CONFGSSAPI_MECH.CONFHOST.CONFIDMAPD.CONFKDUMP.CONFKRB5.CONFLATRACE.CONFLD.SO.CONFLIBAUDIT.CONFLIBUSER.CONFLOGROTATE.CONFLTRACE.CONFMKE2FS.CONFMONGOD.CONFNAMED.CONFNFSMOUNT.CONFNSSWITCH.CONFNTP1.CONFNTP.CONFNUMAD.CONFODDJOBD.CONFPM-UTILS-HD-APM-RESTORE.CONFPRELINK. CONFREADAHEAD.CONFREQUEST-KEY.CONFRESOLV.CONFRSYSLOG.CONFSESTATUS.CONFSMARTD.CONFSOS.CONFSUDO.CONFSUDO-LDAP.CONFSYSCTL.CONFTROLLTECH.CONFUPDATEDB.CONFWARNQUOTA.CONFXINETD.CONFYP.CONFYUM.CONF
7. Display the total number of subdirectories or files at one level under the / var directory.
[root@captain ~] # ls / var/account crash db ftp lib lock mail nis preserve spool wwwcache cvs empty games local log named opt run tmp yp [root@captain ~] # ls / var/ | wc-L22
8. Take out the names of the 10 groups with the lowest value in the third field in the / etc/group file.
[root@captain etc] # sort-tweezer'- k3-n / etc/group | head-10 | cut-tweezer'- f1rootbindaemonsysadmttydisklpmemkmem
9. Merge the contents of / etc/fstab and / etc/issue files into the same content and save them to the / tmp/etc.test file.
[root@captain ~] # cat / etc/fstab / etc/issue > / tmp/etc.test [root@captain ~] # cat / tmp/etc.test## / etc/fstab# Created by anaconda on Sun Feb 28 19:13:12 2016 June # 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/VolGroup-lv_root / ext4 defaults 1 1UUID=269efa02-c94a-42b2-8088-e223080e9f87 / boot ext4 defaults 1 2/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0tmpfs / dev/shm Tmpfs defaults 0 0devpts / dev/pts devpts gid=5 Mode=620 0 0sysfs / sys sysfs defaults 0 0proc / proc proc defaults 0 0CentOS release 6.6 (Final) Kernel\ r on an\ m
10. Summarize and describe how to use user and group management commands and complete the following exercises:
(1) create a group distro with a GID of 2016
[root@captain ~] # groupadd-g 2016 distro
(2) create user mandriva, whose ID number is 1005; the basic group is distro
[root@captain] # useradd-u 1005-g distro mandriva
(3) create user mageia, whose ID number is 1100 and home directory is / home/linux
[root@captain] # useradd-u 1100-d / home/linux mageia
(4) add a password to the user mageia, and the password is mageedu
[root@captain ~] # echo 'mageedu' | passwd-- stdin mageiaChanging password for user mageia.passwd: all authentication tokens updated successfully.
(5) delete mandriva, but keep its home directory
[root@captain ~] # userdel mandriva [root@captain ~] # ls-d / home/mandriva//home/mandriva/
(6) create user slackware, whose ID number is 2002, basic group is distro, and additional group peguin
[root@captain] # groupadd peguin [root@captain] # useradd-u 2002-g distro-G peguin slackware
(7) modify the default shell of slackware to / bin/tcsh
[root@captain ~] # grep'^ slackware' / etc/passwdslackware:x:2002:2016::/home/slackware:/bin/bash [root@captain ~] # chsh-s / bin/tcsh slackwareChanging shell for slackware.Shell changed. [root@captain ~] # grep'^ slackware' / etc/passwdslackware:x:2002:2016::/home/slackware:/bin/tcsh
(8) add additional group admins for user slackware
[root@captain home] # id slackwareuid=2002 (slackware) gid=2016 (distro) groups=2016 (distro), 2017 (peguin) [root@captain ~] # groupadd admins [root@captain ~] # usermod-aG admins slackware [root@captain home] # id slackwareuid=2002 (slackware) gid=2016 (distro) groups=2016 (distro), 2017 (peguin), 2018 (admins)
(9) add a password for slackware, and the minimum duration of the password is 3 days, the longest is 180 days, and the warning is 3 days
[root@captain ~] # echo "magedu123" | passwd-- stdin slackwareChanging password for user slackware.passwd: all authentication tokens updated successfully. [root@captain ~] # chage-l slackwareLast password change: Aug 20 2016Password expires: neverPassword inactive: neverAccount expires: neverMinimum number of days between password change: 0Maximum number of days between password change: 99999Number of days of warning before password expires: 7 [root@captain ~] # chage-m 3-M 180-W 3 slackware [root@captain ~] # chage-l slackwareLast password change: Aug 20 2016Password expires: Feb 16, 2017Password inactive: neverAccount expires: neverMinimum number of days between password change: 3Maximum number of days between password change: 180Number of days of warning before password expires: 3
(10) add user openstack, whose ID number is 3003, basic group is clouds, and additional groups are peguin and nova
[root@captain ~] # groupadd clouds [root@captain ~] # groupadd nova [root@captain ~] # useradd-u 3003-g clouds-G peguin,nova openstack [root@captain ~] # id openstackuid=3003 (openstack) gid=2019 (clouds) groups=2019 (clouds), 2017 (peguin), 2020 (nova)
(11) add the system user mysql and require its shell to be / sbin/nologin
# there are already mysql users on the system. Use MySQL instead of [root@captain] # useradd-r-s / sbin/nologin MySQL [root@captain ~] # grep'^ MySQL' / etc/passwdMySQL:x:496:496::/home/MySQL:/sbin/nologin
(12) use the echo command to add a password for openstack non-interactively.
[root@captain ~] # echo 'magedu123' | passwd-- stdin openstackChanging password for user openstack.passwd: all authentication tokens updated successfully.