用戶及權(quán)限管理以及正則表達式的示例

1、復(fù)制/etc/skel目錄為/home/tuser1悄窃,要求/home/tuser1及其內(nèi)部文件的屬組和其它用戶均沒有任何訪問權(quán)限讥电。

[root@localhost ~]# cp -rv /etc/skel /home/tuser1‘/etc/skel’ -> ‘/home/tuser1’‘/etc/skel/.bash_logout’ -> ‘/home/tuser1/.bash_logout’

‘/etc/skel/.bash_profile’ -> ‘/home/tuser1/.bash_profile’

‘/etc/skel/.bashrc’ -> ‘/home/tuser1/.bashrc’

更改權(quán)限:

[root@localhost ~]# chmod -R go= /home/tuser1

驗證內(nèi)部文件:

[root@localhost tuser1]# ls -la /home/tuser1

total 12

d-wx------. 2 root root 62 Aug 21 01:19 .

drwxr-xr-x. 3 root root 20 Aug 21 01:19 ..

-rw-------. 1 root root 18 Aug 21 01:19 .bash_logout

-rw-------. 1 root root 193 Aug 21 01:19 .bash_profile

-rw-------. 1 root root 231 Aug 21 01:19 .bashrc

驗證目錄:

[root@localhost tuser1]# ls -ld /home/tuser1

d-wx------. 2 root root 62 Aug 21 01:19 /home/tuser1

2、編輯/etc/group文件轧抗,添加組hadoop恩敌。

使用nano或vim或vi編輯,以下是使用的nano:

[root@localhost ~]# nano /etc/group

添加hadoop:x:2018

ctrl+o保存横媚,ctrl+x退出

[root@localhost ~]# grep "hadoop" /etc/group

hadoop:x:2018

此處hadoop的引號不加也可以

3纠炮、手動編輯/etc/passwd文件新增一行月趟,添加用戶hadoop,其基本組ID為hadoop組的id號恢口;其家目錄為/home/hadoop孝宗。

[root@localhost ~]# mkdir /home/hadoop

使用nano或vim或vi編輯,以下是使用的nano:

[root@localhost ~]# nano /etc/passwd

添加hadoop:x:2000:2000::/home/hadoop:/sbin/nologin

ctrl+o保存耕肩,ctrl+x退出

驗證:

[root@localhost ~]# grep "hadoop" /etc/passwdhadoop:x:hadoop:hadoop::/home/hadoop:/sbin/nologin

4因妇、復(fù)制/etc/skel目錄為/home/hadoop,要求修改hadoop目錄的屬組和其它用戶沒有任何訪問權(quán)限猿诸。

[root@localhost ~]# cp -rv /etc/skel /home/hadoop

‘/etc/skel’ -> ‘/home/hadoop/skel’‘/etc/skel/.bash_logout’ -> ‘/home/hadoop/skel/.bash_logout’‘/etc/skel/.bash_profile’ -> ‘/home/hadoop/skel/.bash_profile’‘/etc/skel/.bashrc’ -> ‘/home/hadoop/skel/.bashrc’

更改權(quán)限:

[root@localhost ~]# chmod -R go= /home/hadoop

驗證目錄下的文件:

[root@localhost ~]# ls -la /home/hadoop

total 0

drwx------. 3 root root 18 Aug 21 01:48 .

drwxr-xr-x. 4 root root 34 Aug 21 01:44 ..

drwx------. 2 root root 62 Aug 21 01:48 skel

驗證目錄:

drwx------. 3 root root 18 Aug 21 01:48 /home/hadoop

5婚被、修改/home/hadoop目錄及其內(nèi)部所有文件的屬主為hadoop,屬組為hadoop梳虽。

[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop

驗證目錄下的文件:

[root@localhost ~]# ls -la /home/hadoop

total 0

drwx------. 3 hadoop hadoop 18 Aug 21 01:48 .

drwxr-xr-x. 4 root root 34 Aug 21 01:44 ..

drwx------. 2 hadoop hadoop 62 Aug 21 01:48 skel

驗證目錄:

[root@localhost ~]# ls -ld /home/hadoop

drwx------. 3 hadoop hadoop 18 Aug 21 01:48 /home/hadoop

6址芯、顯示/proc/meminfo文件中以大寫或小寫S開頭的行;用兩種方式窜觉;

第一種方式:

[root@localhost ~]# grep "^[sS]" /proc/meminfo

SwapCached: 0 kB

SwapTotal: 2097148 kB

SwapFree: 2097148 kB

Shmem: 7900 kB

Slab: 63796 kB

SReclaimable: 26668 kB

SUnreclaim: 37128 kB

第二種方式:

[root@localhost ~]# grep -i "^s" /proc/meminfo

SwapCached: 0 kB

SwapTotal: 2097148 kB

SwapFree: 2097148 kB

Shmem: 7900 kB

Slab: 63796 kB

SReclaimable: 26668 kB

SUnreclaim: 37128 kB

第三種方式:

[root@localhost ~]# grep "^[s|S]" /proc/meminfo

取得的結(jié)果是一樣的

7谷炸、顯示/etc/passwd文件中其默認shell為非/sbin/nologin的用戶;

[root@localhost ~]# grep -v "/sbin/nologin" /etc/passwd

root:x:0:0:root:/root:/bin/bash

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

8竖螃、顯示/etc/passwd文件中其默認shell為/bin/bash的用戶淑廊;

[root@localhost ~]# grep "/bin/bash" /etc/passwd

root:x:0:0:root:/root:/bin/bash

9、找出/etc/passwd文件中的一位數(shù)或兩位數(shù)特咆;

方法一:

[root@localhost ~]# egrep "\<[0-9]{1,2}\>" /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin

方法二:

[root@localhost ~]# grep "\<[0-9]\{1,2\}\>" /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin

10、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行录粱;

centos沒有/boot/grub/grub.conf文件,我添加了一個文件及一些內(nèi)容在其中

第一種方法:

[root@localhost ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf

?2312313

?safdfadf

第二種方法:

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

?2312313

?safdfadf

第三種方法:

[root@localhost ~]# egrep "[[:space:]]+" /boot/grub/grub.conf

?2312313

?safdfadf

第四種方法:

[root@localhost ~]# egrep "[[:space:]]{1,}" /boot/grub/grub.conf

?2312313

?safdfadf

11腻格、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符啥繁,而后又有至少一個非空白字符的行菜职;

centos7沒有/etc/rc.d/sysinit文件,替換成 /etc/rc.d/rc.local

第一種方法:

[root@localhost ~]# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/rc.d/rc.local

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

# In contrast to previous versions due to parallel execution during boot

# this script will NOT be run after all other services.

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

第二種方法:

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

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

# In contrast to previous versions due to parallel execution during boot

#this script will NOT be run after all other services.

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

12、打出netstat -tan命令執(zhí)行結(jié)果中以‘LISTEN’旗闽,后或跟空白字符結(jié)尾的行酬核;

[root@localhost ~]# 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

tcp6 0 0 :::22 :::* LISTEN

tcp6 0 0 ::1:25 :::* LISTEN

13、添加用戶bash, testbash, basher, nologin (此一個用戶的shell為/sbin/nologin)适室,而后找出當前系統(tǒng)上其用戶名和默認shell相同的用戶的信息嫡意;

[root@localhost ~]# useradd bash

[root@localhost ~]# useradd testbash

[root@localhost ~]# useradd basher

[root@localhost ~]# useradd -s /sbin/nologin nologin

驗證:

[root@localhost ~]# id bash

uid=2001(bash) gid=2001(bash) groups=2001(bash)

[root@localhost ~]# id testbash

uid=2002(testbash) gid=2002(testbash) groups=2002(testbash)

[root@localhost ~]# id basher

uid=2003(basher) gid=2003(basher) groups=2003(basher)

[root@localhost ~]# tail -1 /etc/passwdnologin:x:2004:2004::/home/nologin:/sbin/nologin

找出用戶名與默認shell相同的用戶:

[root@localhost ~]# grep -E "^([^:]+\>).*\1$" /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltbash:x:2001:2001::/home/bash:/bin/bashnologin:x:2004:2004::/home/nologin:/sbin/nologin

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市捣辆,隨后出現(xiàn)的幾起案子蔬螟,更是在濱河造成了極大的恐慌,老刑警劉巖汽畴,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旧巾,死亡現(xiàn)場離奇詭異耸序,居然都是意外死亡,警方通過查閱死者的電腦和手機鲁猩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門坎怪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人廓握,你說我怎么就攤上這事搅窿。” “怎么了疾棵?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵戈钢,是天一觀的道長。 經(jīng)常有香客問我是尔,道長殉了,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任拟枚,我火速辦了婚禮薪铜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘恩溅。我一直安慰自己隔箍,他們只是感情好,可當我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布脚乡。 她就那樣靜靜地躺著蜒滩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪奶稠。 梳的紋絲不亂的頭發(fā)上俯艰,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機與錄音锌订,去河邊找鬼竹握。 笑死,一個胖子當著我的面吹牛辆飘,可吹牛的內(nèi)容都是我干的啦辐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蜈项,長吁一口氣:“原來是場噩夢啊……” “哼芹关!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起战得,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤充边,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體浇冰,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡贬媒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了肘习。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片际乘。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漂佩,靈堂內(nèi)的尸體忽然破棺而出脖含,到底是詐尸還是另有隱情,我是刑警寧澤投蝉,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布养葵,位于F島的核電站,受9級特大地震影響瘩缆,放射性物質(zhì)發(fā)生泄漏关拒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一庸娱、第九天 我趴在偏房一處隱蔽的房頂上張望着绊。 院中可真熱鬧,春花似錦熟尉、人聲如沸归露。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剧包。三九已至,卻和暖如春往果,著一層夾襖步出監(jiān)牢的瞬間玄捕,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工棚放, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人馅闽。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓飘蚯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親福也。 傳聞我的和親對象是個殘疾皇子局骤,可洞房花燭夜當晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容