您的位置:首页 > 运维架构 > Linux

linux第二次作业

2016-12-20 20:32 232 查看
1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost ~]# useradd user1
[root@localhost ~]# passwd user1
Changing password for user user1.
[root@localhost ~]# who
root pts/0 2016-12-18 23:22 (172.16.214.1)
root pts/1 2016-12-18 23:27 (172.16.214.1)
root pts/2 2016-12-18 23:27 (172.16.214.1)
user1 pts/3 2016-12-18 23:36 (172.16.214.1)
[root@localhost ~]# who | cut -d ' ' -f 1 | uniq
root
user1

2、取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@localhost ~]# cut -d ':' -f 7 /etc/passwd | uniq -c | sort -nr | head -1
25 /sbin/nologin

3、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# sort -nr -t ':' -k 3 /etc/passwd | head -10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt

4、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig | egrep 'inet\>' | awk '{print $2}'
addr:172.16.214.186
addr:127.0.0.1

5、显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls -R /var | wc -l

6、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# sort -nr -t ':' -k 3 /etc/group | tail -10 | cut -d ':' -f 1

7、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test

8、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro

(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd -u 1005 -g distro mandriva

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia

(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo 'mageia' | passwd --stdin mageia

(5)、删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware

(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tsch slackware

(8)、为用户slackware新增附加组admins;
[root@localhost ~]# usermod -a -G admins slackware

(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware

(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@localhost ~]# useradd -u 3003 -g cloud -G peguin,nova openstack

(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd -r -s /bin/nologin mysql

(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo 'openstack' | passwd --stdin openstack

9、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
[root@localhost ~]# cp -r /etc/skel/ /home/tuser1
[root@localhost ~]# chmod -R go-rwx /home/tuser1/
[root@localhost ~]# ll -a /home/tuser1/
total 28
drwx------. 4 root root 4096 Dec 19 01:37 .
drwxr-xr-x. 10 root root 4096 Dec 19 01:37 ..
-rw-------. 1 root root 18 Dec 19 01:37 .bash_logout
-rw-------. 1 root root 176 Dec 19 01:37 .bash_profile
-rw-------. 1 root root 124 Dec 19 01:37 .bashrc
drwx------. 2 root root 4096 Dec 19 01:37 .gnome2
drwx------. 4 root root 4096 Dec 19 01:37 .mozilla
[root@localhost ~]# ll -d /home/tuser1/
drwx------. 4 root root 4096 Dec 19 01:37 /home/tuser1/

10、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
[root@localhost ~]# cat /proc/meminfo | grep -e '^[S|s]'
SwapCached: 0 kB
SwapTotal: 2031612 kB
SwapFree: 2031612 kB
Shmem: 1180 kB
Slab: 85792 kB
SReclaimable: 22424 kB
SUnreclaim: 63368 kB

[root@localhost ~]# cat /proc/meminfo | grep ^[Ss]
SwapCached: 0 kB
SwapTotal: 2031612 kB
SwapFree: 2031612 kB
Shmem: 1180 kB
Slab: 85816 kB
SReclaimable: 22424 kB
SUnreclaim: 63392 kB

11、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
[root@localhost ~]# cat /etc/passwd | grep -v ‘/sbin/nologin$' | cut -d ':' -f 1

12、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
[root@localhost ~]# cat /etc/passwd | grep '/bin/bash$' | cut -d ':' -f 1
root
admin
test
user1
mageia

13、找出/etc/passwd文件中的一位数或两位数;
[root@localhost ~]# egrep '\<[0-9]{1,2}\>' /etc/passwd

14、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[root@localhost ~]# cat /etc/rc.d/rc.local | egrep '^#[[:space:]]+[^[:space:]]+'
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

15、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@localhost ~]# netstat -tan | egrep -i 'listen[[:space:]]+$'

16、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@localhost ~]# cat /etc/passwd | egrep '^([a-z]+\>).*\1$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:2003:2003::/home/bash:/bin/bash
nologin:x:2006:2006::/home/nologin:/sbin/nologin
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息