Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 第四周作業:權限、用戶與正則表達式 13 題

1. 複製 /etc/skel 為 /home/tuser1,且屬組與其他用戶皆無任何權限

用歸檔複製保留所有屬性,再移除組與其他人的權限。-a 等同 -dr --preserve=all:`-d` 不追蹤符號鏈接(只複製鏈接本身),`-r` 遞歸,--preserve=all 保留 mode、ownership、timestamps、context、links、xattr 等全部屬性。

cp -a /etc/skel/ /home/tuser1/
chmod -R go= /home/tuser1/

Linux 第四周作業:權限、用戶與正則表達式 13 題

2. 編輯 /etc/group 加入 hadoop 組

echo "hadoop:x:666: " >> /etc/group
# 或用 vim 編輯 /etc/group,在末尾新增一行 hadoop:x:666: 後存檔

Linux 第四周作業:權限、用戶與正則表達式 13 題

3. 手動編輯 /etc/passwd 新增 hadoop 用戶

基本組 ID 取 hadoop 組的 GID,家目錄為 /home/hadoop。用 vim 開啟後在命令模式按 G 移到末行,按 o 新增一行:

hadoop:x:500:666:description:/home/hadoop:/bin/bash

Linux 第四周作業:權限、用戶與正則表達式 13 題

4. 複製 /etc/skel 為 /home/hadoop 並收緊權限

cp -a /etc/skel/ /home/hadoop
chmod -R go= /home/hadoop

Linux 第四周作業:權限、用戶與正則表達式 13 題

5. 把 /home/hadoop 及其內部檔案的屬主與屬組改為 hadoop

# 方式一
chown -R hadoop /home/hadoop && chgrp -R hadoop /home/hadoop
# 方式二
chown -R hadoop:hadoop /home/hadoop

Linux 第四周作業:權限、用戶與正則表達式 13 題

6. 顯示 /proc/meminfo 中以 S 或 s 開頭的行

grep "^[sS]" /proc/meminfo
grep -i "^s" /proc/meminfo
egrep '^s|^S' /proc/meminfo

Linux 第四周作業:權限、用戶與正則表達式 13 題

7. 顯示 /etc/passwd 中預設 shell 不是 /sbin/nologin 的用戶

grep -v "nologin>$" /etc/passwd | cut -d: -f1

Linux 第四周作業:權限、用戶與正則表達式 13 題

8. 顯示預設 shell 為 /bin/bash 的用戶

grep "bash>$" /etc/passwd | cut -d: -f1

Linux 第四周作業:權限、用戶與正則表達式 13 題

9. 找出 /etc/passwd 中的一位數或兩位數

egrep -o "<[0-9]>|<[0-9][0-9]>" /etc/passwd | sort -n | uniq

Linux 第四周作業:權限、用戶與正則表達式 13 題

10. 顯示 /boot/grub/grub.conf 中以至少一個空白字元開頭的行

egrep "^[[:space:]]+.*" /boot/grub/grub.conf

Linux 第四周作業:權限、用戶與正則表達式 13 題

11. 顯示 /etc/rc.d/rc.sysinit 中以 # 開頭、其後至少一個空白、再至少一個非空白字元的行

egrep "^#.*([[:space:]]+).*([^[:space:]]+)" /etc/rc.d/rc.sysinit

Linux 第四周作業:權限、用戶與正則表達式 13 題

(內容太多,僅擷取部分。)

12. 取出 netstat -tan 結果中以 LISTEN 結尾或 LISTEN 後接空白的行

netstat -tan | egrep "LISTEN([[:space:]]){0,}$"

Linux 第四周作業:權限、用戶與正則表達式 13 題

13. 建立 bash、testbash、basher、nologin 四個用戶,找出用戶名與預設 shell 相同的用戶

useradd bash && useradd testbash && useradd basher \
  && useradd -s /sbin/nologin nologin \
  && egrep '^([[:alpha:]]+>).*\1$' /etc/passwd

Linux 第四周作業:權限、用戶與正則表達式 13 題

本文出自「Record Mark」博客,謝絕轉載!

Copyright © Linux教程網 All Rights Reserved