監視/etc/passwd檔案是否正常,監視etcpasswd檔案

來源:互聯網
上載者:User

監視/etc/passwd檔案是否正常,監視etcpasswd檔案

協助監視/etc/passwd檔案是否正常(P90 練習6.7)

1)找出有UID0的所有項

2)找出有重複UID的所有項

3)找出有重複登入名稱的所有項

4)找出沒有口令的所有項

5)找出沒有作廢日期的所有項

 

以下是實現的shell指令碼:

#!/bin/bash
#監視/etc/passwd正常

#找出有UID 0的使用者
echo "----------------------------------"
user1=`less /etc/passwd | awk -F: '$3==0 {print $1}'`
if [ -z $user1 ];then
echo "1.there is no user's UID equals 0"
else
echo -n "1.the user of UID equals 0 have: "$user1
echo ""
fi

#找出有重複UID的使用者
echo "----------------------------------"
user2=`awk -F: 'BEGIN{ORS=","}NR==FNR {a[$3]++} NR>FNR&&a[$3]>1 {print $1,$3}' /etc/passwd /etc/passwd`
if [ -z $user2 ];then
echo "2.there is no user's UID repeat"
else
echo -n "2.the user of repeat UID have: "$user2
echo ""
fi

#找出有重複登入名稱的使用者
echo "---------------------------------"
user3=`awk -F: 'BEGIN{ORS=","}NR==FNR {a[$1]++} NR>FNR&&a[$1]>1 {print $1,$3}' /etc/passwd /etc/passwd`
if [ -z $user3 ];then
echo "3.there is no user's login name repeat"
else
echo -n "3.the user of repeat login name have: "$user3
echo ""
fi

#找出沒有口令的所有使用者
echo "---------------------------------"
user4=`sudo awk -F: 'BEGIN{ORS=","} length($2)<30 {print $1}' /etc/shadow`
if [ -z $user4 ];then
echo "4.there is no user have no password"
else
echo -n "4.the user of no password have: "$user4
echo ""
fi

#找出沒有作廢日期的所有使用者
echo "---------------------------------"
user5=`sudo awk -F: 'BEGIN{ORS=","} length($7)<1 {print $1}' /etc/shadow`
if [ -z $user5 ];then
echo "5.there is no user have no outdate time"
else
echo -n "5.the user of no outdate time have: "$user5
echo ""
fi
echo "---------------------------------"


/etc/passwd檔案有什用處?它有什欄位

/etc/passwd
該目錄存放的是作業系統使用者資訊,該檔案為所有使用者可見。
給linux系統添加一個帳號:
useradd -g mysql -d /home/test -m test(:建立一個使用者test, 屬於mysql組,開始目錄是/home/test)
然後進入 /etc/passwd,可以看到如下資訊,在最後一行可以看到剛加的使用者的資訊。如下
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
hzmc:x:500:500:hzmc:/home/hzmc:/bin/bash
mysql:x:501:501::/home/mysql:/bin/bash
chenhua:x:503:501::/home/chenhua:/bin/bash
test:x:504:501::/home/test:/bin/bash
可以看出/etc/passwd檔案存放的是使用者的資訊,由6個分號組成的7個資訊,解釋如下
(1):使用者名稱。
(2):密碼(已經加密)
(3):UID(使用者標識),作業系統自己用的
(4):GID組標識。
(5):使用者全名或本地帳號
(6):開始目錄
(7):登入使用的Shell,就是對登入命令進行解析的工具。
下面為test使用者佈建密碼,執行如下命令
passwd test
[root@localhost etc]# passwd test
Changing password for user test.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated succ......餘下全文>>
 
linux下/etc/passwd檔案中組ID的問題

可以屬於不同組。
passwd裡gid是主組,其他組是向外延展群組,向外延展群組在/etc/group裡描述。
useradd username如果不指定,預設建立一個與uid相同的gid。
其他組可以在建立使用者的時候通過-G添加到其他組,也可以用usermod -G groupname username對已存在使用者修改。

例如user1的主組是500,向外延展群組是501和502
user2的主組是501,向外延展群組是502
user3的主組是502

在passwd格式如下:
user1:x:500:500::/home/user1:/bin/bash
user2:x:501:501::/home/user2:/bin/bash
user3:x:502:502::/home/user3:/bin/bash

group格式如下:
user1:x:500:
user2:x:501:user1
user3:x:502:user1,user2
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.