本周作业内容:
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@captain ~]# who | cut -d' ' -f1 | sort -u icheck oracle root
2、取出最后登录到当前系统的用户的相关信息。
[root@captain shell]# cat getuserinfo.sh #!/bin/bash # author: Felix Ruan # get user basic information # date:20160820 # v1.0 if [ `id -u` -ne 0 ]; then echo "only root can execute this script" exit 1 fi readonly User=${1:-`last | head -1 | cut -d' ' -f1`} readonly PASSWD=/etc/passwd function 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: oracle user name: oracle, uid: 500 group name: dba, gid: 500 ------------------------------- user home: /root/oracle ------------------------------- user shell: /bin/bash ------------------------------- password expiry information: Password expires : never Account expires : never Maximum 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/passwd Shell 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.txt postfix:x:89:89::/var/spool/postfix:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin dhcpd:x:177:177:DHCP server:/:/sbin/nologin nginx:x:497:497:nginx user:/var/cache/nginx:/sbin/nologin mongod:x:498:498:mongod:/var/lib/mongo:/bin/false saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin oracle:x:500:500::/home/oracle:/bin/bash icheck:x:501:500::/home/icheck:/bin/bash nfsnobody: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)}' > done eth0: 192.168.162.134 eth2: 192.168.1.10 eth2:0: 192.168.10.10 [root@captain ~]#
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@captain etc]# ls *.conf asound.conf gai.conf libuser.conf numad.conf sos.conf autofs.conf grub.conf logrotate.conf oddjobd.conf sudo.conf autofs_ldap_auth.conf gssapi_mech.conf ltrace.conf pm-utils-hd-apm-restore.conf sudo-ldap.conf cas.conf host.conf mke2fs.conf prelink.conf sysctl.conf cgconfig.conf idmapd.conf mongod.conf readahead.conf Trolltech.conf cgrules.conf kdump.conf named.conf request-key.conf updatedb.conf cgsnapshot_blacklist.conf krb5.conf nfsmount.conf resolv.conf warnquota.conf dracut.conf latrace.conf nsswitch.conf rsyslog.conf xinetd.conf elinks.conf ld.so.conf ntp1.conf sestatus.conf yp.conf fprintd.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.conf ASOUND.CONF AUTOFS.CONF AUTOFS_LDAP_AUTH.CONF CAS.CONF CGCONFIG.CONF CGRULES.CONF CGSNAPSHOT_BLACKLIST.CONF DRACUT.CONF ELINKS.CONF FPRINTD.CONF GAI.CONF GRUB.CONF GSSAPI_MECH.CONF HOST.CONF IDMAPD.CONF KDUMP.CONF KRB5.CONF LATRACE.CONF LD.SO.CONF LIBAUDIT.CONF LIBUSER.CONF LOGROTATE.CONF LTRACE.CONF MKE2FS.CONF MONGOD.CONF NAMED.CONF NFSMOUNT.CONF NSSWITCH.CONF NTP1.CONF NTP.CONF NUMAD.CONF ODDJOBD.CONF PM-UTILS-HD-APM-RESTORE.CONF PRELINK.CONF READAHEAD.CONF REQUEST-KEY.CONF RESOLV.CONF RSYSLOG.CONF SESTATUS.CONF SMARTD.CONF SOS.CONF SUDO.CONF SUDO-LDAP.CONF SYSCTL.CONF TROLLTECH.CONF UPDATEDB.CONF WARNQUOTA.CONF XINETD.CONF YP.CONF YUM.CONF
7、显示/var目录下一级子目录或文件的总个数。
[root@captain ~]# ls /var/ account crash db ftp lib lock mail nis preserve spool www cache cvs empty games local log named opt run tmp yp [root@captain ~]# ls /var/ | wc -l 22
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@captain etc]# sort -t':' -k3 -n /etc/group | head -10 | cut -d':' -f1 root bin daemon sys adm tty disk lp mem kmem
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 1 UUID=269efa02-c94a-42b2-8088-e223080e9f87 /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.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 mageia Changing 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/passwd slackware:x:2002:2016::/home/slackware:/bin/bash [root@captain ~]# chsh -s /bin/tcsh slackware Changing shell for slackware. Shell changed. [root@captain ~]# grep '^slackware' /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[root@captain home]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin) [root@captain ~]# groupadd admins [root@captain ~]# usermod -aG admins slackware [root@captain home]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@captain ~]# echo "magedu123" | passwd --stdin slackware Changing password for user slackware. passwd: all authentication tokens updated successfully. [root@captain ~]# chage -l slackware Last password change : Aug 20, 2016 Password expires : never Password inactive : never Account expires : never Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [root@captain ~]# chage -m 3 -M 180 -W 3 slackware [root@captain ~]# chage -l slackware Last password change : Aug 20, 2016 Password expires : Feb 16, 2017 Password inactive : never Account expires : never Minimum number of days between password change : 3 Maximum number of days between password change : 180 Number 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 openstack uid=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/passwd MySQL:x:496:496::/home/MySQL:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@captain ~]# echo 'magedu123' | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。