您好,登錄后才能下訂單哦!
本周作業內容:
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。