Linux基础练习题(二)

系统版本:

[root@centos67d1 ~]# cat /etc/redhat-release 
CentOS release 6.7 (Final)
[root@centos67d1 ~]# uname -r
2.6.32-573.el6.x86_64
[root@centos71d1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
[root@centos71d1 ~]# uname -r
3.10.0-229.el7.x86_64

 

1. 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限

[root@centos67n1 ~]# cp -rv /etc/skel/ /home/tuser1
#注意:事先无需创建/home/tuser1目录,否则这条命令会变成复制/etc/skel目录到/home/tuser1目录下
`/etc/skel/' -> `/home/tuser1'
`/etc/skel/.bash_logout' -> `/home/tuser1/.bash_logout'
`/etc/skel/.bashrc' -> `/home/tuser1/.bashrc'
`/etc/skel/.bash_profile' -> `/home/tuser1/.bash_profile'

[root@centos67n1 ~]# chmod -R go-rwx /home/tuser1
[root@centos67n1 ~]# ll -a /home/tuser1/
total 20
drwx------. 2 root root 4096 Jul  3 17:03 .
drwxr-xr-x. 3 root root 4096 Jul  3 17:03 ..
-rw-------. 1 root root   18 Jul  3 17:03 .bash_logout
-rw-------. 1 root root  176 Jul  3 17:03 .bash_profile
-rw-------. 1 root root  124 Jul  3 17:03 .bashrc

2. 编辑/etc/group文件,添加组hadoop

[root@centos67n1 ~]# grep '777' /etc/group
#这里没有grep到任何内容,表示“777”这个组ID没有被占用;\
#注意:在centos6中系统组ID的范围是1-499,登录组是500-60000

[root@centos67n1 ~]# echo 'hadoop:x:777:' >> /etc/group
[root@centos67n1 ~]# tail -1 /etc/group
hadoop:x:777:

3. 手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop

[root@centos67n1 ~]# grep '777' /etc/passwd
#首先还是要确认“777”这个UID有没有被占用,没有grep到,表示没有被占用;\
#在centos6中,登录用户的UID范围和登录组GID范围一样是500-60000

[root@centos67n1 ~]# echo 'hadoop:x:777:777::/home/hadoop:/bin/bash' >> /etc/passwd
[root@centos67n1 ~]# tail -2 /etc/passwd
fedora:x:500:500::/home/fedora:/bin/bash
hadoop:x:777:777::/home/hadoop:/bin/bash

4. 复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限

[root@centos67d1 ~]# cp -rv /etc/skel/ /home/hadoop
`/etc/skel/' -> `/home/hadoop'
`/etc/skel/.bash_profile' -> `/home/hadoop/.bash_profile'
`/etc/skel/.bash_logout' -> `/home/hadoop/.bash_logout'
`/etc/skel/.bashrc' -> `/home/hadoop/.bashrc'

[root@centos67d1 ~]# chmod go= /home/hadoop/
[root@centos67d1 ~]# ll -d /home/hadoop/
drwx------. 2 root root 4096 Jul  4 13:55 /home/hadoop/

5. 修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop

[root@centos67d1 ~]# chown -R hadoop:hadoop /home/hadoop/
[root@centos67d1 ~]# ll -a /home/hadoop/
total 20
drwx------. 2 hadoop hadoop 4096 Jul  4 13:55 .
drwxr-xr-x. 6 root   root   4096 Jul  4 13:55 ..
-rw-r--r--. 1 hadoop hadoop   18 Jul  4 13:55 .bash_logout
-rw-r--r--. 1 hadoop hadoop  176 Jul  4 13:55 .bash_profile
-rw-r--r--. 1 hadoop hadoop  124 Jul  4 13:55 .bashrc

6. 显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式

[root@centos67d1 ~]# grep -i "^s" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2031612 kB
SwapFree:        2031612 kB
Shmem:               188 kB
Slab:              69812 kB
SReclaimable:      10648 kB
SUnreclaim:        59164 kB

或 [root@centos67d1 ~]# grep "^[Ss]" /proc/meminfo
或 [root@centos67d1 ~]# grep "^\(S\|s\)" /proc/meminfo
或 [root@centos67d1 ~]# grep -E "^(S|s)" /proc/meminfo

7. 显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户

[root@centos67d1 ~]# grep -v "/sbin/nologin" /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
centos
fedora
archlinux
hadoop

8. 显示/etc/passwd文件中其默认shell为/bin/bash的用户

[root@centos67d1 ~]# grep "/bin/bash" /etc/passwd | cut -d: -f1
root
centos
fedora
archlinux
hadoop

9. 找出/etc/passwd文件中的一位数或两位数

[root@centos71d1 ~]# grep "\<[0-9]\{1,2\}\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:\
/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

或 grep "\<[0-9][0-9]\?\>" /etc/passwd

10. 显示/boot/grub/grub.conf中以至少一个空白字符开头的行

[root@centos67d1 ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf 
    root (hd0,0)
    kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/
mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap\
SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  \
KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-573.el6.x86_64.img

或 [root@centos67d1 ~]# grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf 

11. 显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行

[root@centos67d1 ~]# grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit 

# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.
# Initialize hardware
# Set default affinity
# Load other user-defined modules
# Load modules (for backward compatibility with VARs)
....

12. netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行

[root@centos67d1 ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN

13. 用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息

[root@centos67d1 ~]# grep "^\([^:]\+\>\).*\1$" /etc/passwd
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:778:778::/home/bash:/bin/bash
nologin:x:781:781::/home/nologin:/sbin/nologin

 
 

猜你喜欢

转载自blog.51cto.com/201731/2136176