2011-2.26 陳老師UIE筆記

sgy618發表於2011-03-03

2011-2.26 陳老師UIE筆記

[@more@]

基本許可權位
r w x
4 2 1
------------------
chmod 777 /file
rwxrwxrwx
------------------
高階許可權位
普通使用者可以改密碼的前提是要對/etc/shadow有寫許可權、

1、set uid ,設定UID位,又稱suid位。
chmod u+s /file 。往往給可執行檔案加。
當使用者執行擁有SUID許可權位的檔案時,計算許可權時所使用的EUID,將不再是當前使用者的UID,而是檔案的所有者的UID。如果檔案的所有者是root,那麼意味著,此可執行檔案接下來的檔案操作對應是root許可權
用數值4表示
chmod u+s /file

[root@localhost ~]# ls -l /bin/cat
-rwxr-xr-x 1 root root 23132 2010-02-23 /bin/cat

[sungy@localhost ~]$ cat /etc/shadow
cat: /etc/shadow: 許可權不夠

[root@localhost ~]# chmod u+s /bin/cat
[root@localhost ~]# ls -l /bin/cat
-rwsr-xr-x 1 root root 23132 2010-02-23 /bin/cat

[sungy@localhost ~]$ cat /etc/shadow
root:$1$qTSUAati$ZZUCoHgGsdBaxkdv.EFUD.:15026:0:99999:7:::
............
sungy:!!:15032:0:99999:7:::
[sungy@localhost ~]$

[root@localhost ~]# find /bin/ -perm 4755 -ls 檢視可執行檔案哪些有SUID許可權位()
關於find命令,參考

2、set gid ,設定gid位,sgid位。
chmod g+s /file .往往給目錄加。
當使用者在此目錄下建立檔案時,檔案的所有者將是當前使用者,而所屬組將繼承此目錄的所屬組。
----------
如以一例項:
實現同組成員上傳的檔案,可以相互寫
[root@server ~]# useradd u1
[root@server ~]# useradd u2
[root@server ~]# useradd u3
-------
[root@server ~]# gpasswd -a u1 g1
Adding user u1 to group g1
[root@server ~]# gpasswd -a u2 g1
Adding user u2 to group g1
[root@server ~]# gpasswd -a u3 g1
Adding user u3 to group g1
-------
[root@server ~]# ls -ld /test
drwxrwxrwx 3 root root 4096 Feb 26 11:29 /test
[root@server ~]# chgrp g1 /test
[root@server ~]# ls -ld /test
drwxrwxrwx 3 root g1 4096 Feb 26 11:29 /test
[root@server ~]# chmod g+s /test
-----
[u3@server test]$ whoami
u3
[u3@server test]$ ls -ld u2_dir/
drwxrwsr-x 3 u2 g1 4096 Feb 26 11:31 u2_dir/
[u3@server test]$ ls -ld u2_file
-rw-rw-r-- 1 u2 g1 6 Feb 26 11:31 u2_file
[u3@server test]$ mkdir u2_dir/aaa
[u3@server test]$ vim u2_file

3、sticky位,一般給所有使用者都能寫的目錄加。
chmod o+t /file
如果一個目錄使用者都可以w此目錄,建立和刪除。
如果希望使用者只能刪除自己所有的檔案,而不能刪除其它人所有的檔案,則需要使用sticky位

suid 4
sgid 2
sticky 1
chmod 7777 /file rwsrwsrwt
chmod 4755 /file rwsr-xr-x

set sticky位,一般給所有使用者都能些的目錄加。防刪除位,
[root@localhost ~]# chmod o+t test/
drwxrwsrwt 3 root g1 4096 02-27 19:44 test
在該目錄下的檔案中,只有檔案屬於自己的才能刪除,其他人不能刪,儘管同一組也不行
[ub@localhost test]$ rm -rf ua_file
rm: 無法刪除 “ua_file”: 不允許的操作

chmod 7777 /file rwsrwsrwt
chmod 4755 /file rwsr-xr-x


[root@localhost test]# md5sum /etc/passwd
0752fe07d363a2ca5cc50370fc013f0b /etc/passwd 只要檔案沒有被改動,結果是一樣的

[root@localhost sungy]# find ./test -type f -exec md5sum {} ; > ./111
[root@localhost sungy]# ls
111 2011-2.26.txt install.log install.log.bak red5_5.iso test
[root@localhost sungy]# cat 111
9ed914791ba218af0fb64f1758e01fa9 ./test/c.patch
d41d8cd98f00b204e9800998ecf8427e ./test/ccc
dd8c6a395b5dd36c56d23275028f526c ./test/a
dd8c6a395b5dd36c56d23275028f526c ./test/b
08ab09a3f8942b1f0c9aa55084686207 ./test/find.txt
d41d8cd98f00b204e9800998ecf8427e ./test/ddd

高階檔案操作命令和正規表示式
head
tail
cat
less
more
grep

cut -d: -f1,3 /etc/passwd
-d 指定分隔符
-f 指定分割以後的第N段
cut -d: -f1-3,7 /etc/passwd

sort /etc/passwd 按首字元排序 正序
sort -r /etc/passwd 反序
sort -n /file1 按數值大小排序
sort -t: -k3 -n /etc/passwd
-t 指定分隔符
-k 指定按第N段排序
-n 表示按數值大小排序,而/etc/passwd的以:分隔的第3段就是UID
uniq /file1 把檔案中連續的重複行只顯示一次。
uniq -c /file1 把檔案中連續的重複行只顯示一次,-c 表示並顯示此行重複的次數。
如:/file1內容如下,進行操作
[root@server ~]# cat /file1
1
1
1
1
11
22
22
22
22
1
1
1
11
11
33
44421
123
15

sort /file1 | uniq -c | sort -r

[root@server ~]# wc /etc/passwd
37 56 1647 /etc/passwd
[root@server ~]# wc -l /etc/passwd
37 /etc/passwd
[root@server ~]# wc -w /etc/passwd
56 /etc/passwd
[root@server ~]# wc -c /etc/passwd
1647 /etc/passwd

ftp://192.168.1.254/

[root@server ~]# find /home -type f -exec md5sum {} ; > /tmp/file2
[root@server ~]# find /home -type f -exec md5sum {} ; > /tmp/file
[root@server ~]# diff /tmp/file /tmp/file2

[root@server ~]# diff -e a b > c.diff
c.diff 是a的補丁檔案,所以只能給a打補丁,使其等同於b
[root@server ~]# cat a
123123123
[root@server ~]# cat b
123123
1211111

[root@server ~]# patch a < c.diff
[root@server ~]# cat a
123123
1211111

[root@server ~]# cat b
123123
1211111

[root@server ~]#


[root@server ~]# tr 'o' 'O' < /etc/passwd
[root@server ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@server ~]# echo $PATH | tr ":" " "
/usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /root/bin

tr -d f < /tmp/passwd
表示把檔案中的f字串刪除,但是注意並不實際改變檔案內容,而是檔案的輸出內容做改變。

tr命令:只能重定向,不能讀取檔案
[root@localhost sungy]# tr 'o' 'O' < /etc/passwd 替換,但不改變本檔案本身
rOOt:x:0:0:rOOt:/rOOt:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nOlOgin

[root@localhost sungy]# echo $PATH | tr ":" " "
/usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /usr/X11R6/bin /root/bin
[root@localhost sungy]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

[root@localhost sungy]# tr -d o < /etc/passwd 刪除字元操作
rt:x:0:0:rt:/rt:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nlgin


合併 檔案內容
[root@server ~]# paste -d "t" /tmp/file /tmp/file2
[root@server ~]# paste -d "-" /tmp/file /tmp/file2
分割檔案
[root@server a]# split -b 100 file 把檔案分為100位元組為一個單位的檔案
[root@server a]# split -l 10 file 把檔案每10行分成一個檔案
分完以後的檔名如下: xaa xab xac

[root@localhost sungy]# cat xa* <=> [root@localhost sungy]# cat xaa xab xac xad 合併檔案

[root@server a]# cut -d : -f 3 /etc/passwd | sort -n | tr 'n' ' '
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 28 29 32 38 42 43 47 51 68 69 70 74 77 81 86 99 100 500 501 502 503 65534
----------------

[root@server ~]# grep -v root /etc/passwd
-v 表示取反,不包含root字串的行
[root@server ~]# grep -c root /etc/passwd
-c 表示統計,統計出包含root字串的行的行數
[root@server ~]# grep -n root /etc/passwd
-n 顯示行號,表示把包含root字串的行的行號顯示出來
[root@server ~]# grep -i Root /etc/passwd
-i 不區分大小寫

正規表示式 regular expression
^
^string 表示行以string開頭
[root@server ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

$
string$ 表示 行以string字串結尾
[root@server ~]# grep 'sh$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
pg:x:500:500::/home/pg:/bin/bash
u1:x:501:502::/home/u1:/bin/bash
u2:x:502:503::/home/u2:/bin/bash
u3:x:503:504::/home/u3:/bin/bash

[root@server ~]# grep '^123$' /file 僅僅有123字串的行

123
[root@server ~]# grep -n '^$' /file 空行

. 表示任意的一個字元

[abcde] 表示一個字元。這個字元可能是a,b,c,d,e
[a-e]
[a-zA-Z0-9] 表示一個字元,其可以是小寫字母或者大寫字母或者是數字
[^A-Za-z0-9] 表示不是小寫字母大寫字母數字的字元

[root@server a]# grep '^[^#]' /etc/vsftpd/vsftpd.conf
[root@server a]# grep -v '^#' /etc/vsftpd/vsftpd.conf


[a-z][a-z] 表示兩個小寫字母

* 此符號總是和其前一個字元一起看。
a* 表示重複a任意次.也就是說 0個a,1個a,2個a,......
aa* 表示至少一個a
[a-z][a-z]* 這個則表示:小寫字母組成的字串,可以認為是單詞(小寫字母的)


a{5} 表示a重複5次
a{3,} 表示a重複3次及以上
a{1,3} 表示a重複3次及以下


[root@server ~]# grep 'ro{2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rooo:x:504:505::/home/rooo:/bin/bash
[root@server ~]# grep 'ro{2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rooo:x:504:505::/home/rooo:/bin/bash
[root@server ~]# grep 'ro{2}[^o]' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

ot> 表示單詞以ot結尾
以空格或者特殊符號分隔的連續的字串(大小寫字母和數字)認為是單詞


[root@server a]# grep --color 'ot>' /etc/passwd
[root@server a]# grep --color '

[root@server ~]# grep '[^[:alnum:]]' /etc/passwd
[^a-zA-Z0-9]
[root@server ~]# grep '[[:alnum:]]' /etc/passwd
[^a-zA-Z0-9]
[root@server ~]# grep '[[:digit:]]' /etc/passwd
[0-9]
[root@server ~]# grep '[[:lower:][:upper:]]' /etc/passwd
[a-zA-Z]
[root@server ~]# grep '[[:upper:]]' /etc/passwd
[A-Z]

以上都是基本正則 grep 'basic regexp' file
還有擴充套件正則.使用時:grep -E 'extend regexp' file
egrep 'extend regexp' file
1、 基本正則中的aa* 在擴充套件正則中可以用 a+ 來方便的表示。表示1個及N個重複的a字元
2、 a? 表示有a或者沒a
3、 (aa)+ 用括號表示一個字元模式,需要把這個模式看成一個整體
4、 a|b 表示a或者b
[root@server ~]# grep -E '^(a|b)' /etc/passwd
[root@server ~]# grep -E '^a|^b' /etc/passwd
[root@server ~]# grep -E 'bash$|^adm' /etc/passwd
5、a{5}
a{1,5}
a{5,}
(abc){1,3}
(abc){4,}
(abc){5}

[root@server ~]# egrep --color '(ro){1,3}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rooo:x:504:505::/home/rooo:/bin/bash
rororo:x:505:506::/home/rororo:/bin/bash
[root@server ~]# egrep 'ro{3}' /etc/passwd
UID為3位數的使用者資訊
[root@server ~]# grep '^[^:]*:[^:]*:[0-9]{3}:' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
avahi-autoipd:x:100:101:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
pg:x:500:500::/home/pg:/bin/bash
u1:x:501:502::/home/u1:/bin/bash
u2:x:502:503::/home/u2:/bin/bash
u3:x:503:504::/home/u3:/bin/bash
rooo:x:504:505::/home/rooo:/bin/bash
rororo:x:505:506::/home/rororo:/bin/bash

/dev/null 黑洞裝置檔案 不佔空間 ls 1> /dev/null <=> ls > /dev/null 1可以省略
ls -l / fff 1> /dev/null 將正確的資訊扔掉
ls -l / fff 2> /dev/null 將錯誤的資訊扔掉
ls -l / fff &> /dev/null 將正確的和錯誤的都扔掉

> /tmp/file 將file檔案清空 等價於 1> /tmp/file
類似於tr命令很少見,只能透過輸入重定向來讀內容

430 ls 1> /tmp/file
432 ls 1> /dev/null
433 ls -l / fff
434 ls -l / fff 1> /dev/null
435 ls -l / fff 2> /dev/null
436 ls -l / fff
437 ls -l / fff 2> /dev/null
438 ls -l / fff 1> /dev/null
439 ls -l / fff &> /dev/null
440 echo fdjldfsl
441 echo fdjldfsl >> /tmp/file
442 > /tmp/file
443 1> /tmp/file
444 cat /tmp/file
445 echo fdjldfsl 1>> /tmp/file
446 echo fdjldfsl >> /tmp/file
447 cat /tmp/file
448 echo fdjldfsl 2>> /tmp/file
449 cat /tmp/file
450 echo fdjldfsl &>> /tmp/file

453 tr 'n' ' ' < /tmp/file
454 echo fdjsklfds
455 echo fdjsklfds 2> /tmp/a
456 cat /tmp/a
457 lsj fldsjlkfdsjkl
458 echo fdjsklfds 2> /tmp/a
459 lsj fldsjlkfdsjkl 2> /tmp/a
460 cat /tmp/a
461 cat -n /etc/passwd | tail -n 1
462 cat -n /etc/passwd | tail -n 1 > /tmp/a


[root@server ~]# > /tmp/a
[root@server ~]# cat -n /etc/passwd |tail -n 1 | tee /tmp/a
39 rororo:x:505:506::/home/rororo:/bin/bash
[root@server ~]# cat /tmp/a
39 rororo:x:505:506::/home/rororo:/bin/bash

[root@localhost /]# cat -n /etc/passwd | tail -n 1 1> /tmp/a
[root@localhost /]# cat /tmp/a
39 uc:x:504:505::/home/uc:/bin/bash

[root@localhost /]# cat -n /etc/passwd | tail -n 1 |tee /tmp/a tee是將管道傳過來的資訊在螢幕上顯示一份並且存檔案一份
39 uc:x:504:505::/home/uc:/bin/bash
[root@localhost /]# cat /tmp/a
39 uc:x:504:505::/home/uc:/bin/bash

[root@server ~]# cat > /tmp/a <> jlfjklsdf
> fsdjklsdfjklsdf
> sdfsjklsdfjklsdf
> EOF


<< 定義臨時緩衝區的結束符
[root@localhost dev]# cat > /tmp/a <> fdfsf
> fdsfsdfadsf
> fdsafasf
> eee
> EEE

groups + 使用者名稱 查是哪個組

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23168012/viewspace-1046761/,如需轉載,請註明出處,否則將追究法律責任。

相關文章