homework week03
本周作业内容:
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@captain ~]# who | cut -d' ' -f1 | sort -uicheckoracleroot
2、取出最后登录到当前系统的用户的相关信息。
[root@captain shell]# cat getuserinfo.sh #!/bin/bash# author: Felix Ruan# get user basic information# date:20160820# v1.0if [ `id -u` -ne 0 ]; then echo "only root can execute this script" exit 1fireadonly User=${1:-`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: `id -g $1`"}function gethome(){ echo "user home: `grep -w ^$1 $PASSWD | cut -d':' -f6`"}function getshell(){ local ushell=`grep -w "^$1" $PASSWD | cut -d':' -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、取出当前系统上被用户当作其默认shell的最多的那个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、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@captain ~]# cat /etc/passwd | sort -t':' -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、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[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、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[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 'a-z' 'A-Z' > /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、显示/var目录下一级子目录或文件的总个数。
[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、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@captain etc]# sort -t':' -k3 -n /etc/group | head -10 | cut -d':' -f1rootbindaemonsysadmttydisklpmemkmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[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## 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、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[root@captain ~]# groupadd -g 2016 distro
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@captain ~]# useradd -u 1005 -g distro mandriva
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@captain ~]# useradd -u 1100 -d /home/linux mageia
(4)、给用户mageia添加密码,密码为mageedu;
[root@captain ~]# echo 'mageedu' | passwd --stdin mageiaChanging password for user mageia.passwd: all authentication tokens updated successfully.
(5)、删除mandriva,但保留其家目录;
[root@captain ~]# userdel mandriva[root@captain ~]# ls -d /home/mandriva//home/mandriva/
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@captain ~]# groupadd peguin[root@captain ~]# useradd -u 2002 -g distro -G peguin slackware
(7)、修改slackware的默认shell为/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)、为用户slackware新增附加组admins;
[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)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[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)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和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)、添加系统用户mysql,要求其shell为/sbin/nologin;
# 系统上已有mysql用户,使用MySQL代替[root@captain ~]# useradd -r -s /sbin/nologin MySQL[root@captain ~]# grep '^MySQL' /etc/passwdMySQL:x:496:496::/home/MySQL:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@captain ~]# echo 'magedu123' | passwd --stdin openstackChanging password for user openstack.passwd: all authentication tokens updated successfully.