1刺桃、文件描述符
打開的文件都有一個fd: file descriptor (文件描述符),在/proc/$$/fd中吸祟,是系統(tǒng)自動分配的瑟慈。如果在一個終端用vim打開一個f1文件,在另外一個終端進行如下操作屋匕,可以看到文件描述符
- 查看文件描述符
[root@centos6 ~]#cd /proc
[root@centos6 proc]#cd $$ --- 當前進程編號
[root@centos6 2687]#ls
attr comm fd mem numa_maps root stat
autogroup coredump_filter fdinfo mountinfo oom_adj sched statm
auxv cpuset io mounts oom_score schedstat status
cgroup cwd limits mountstats oom_score_adj sessionid syscall
clear_refs environ loginuid net pagemap smaps task
cmdline exe maps ns personality stack wchan
[root@centos6 2687]#cd fd
[root@centos6 fd]#ll
total 0
lrwx------. 1 root root 64 Jul 21 07:44 0 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 1 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 19:20 10 -> /root/f1 ---文件描述符為10
lrwx------. 1 root root 64 Jul 21 18:54 2 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 255 -> /dev/pts/0
[root@centos6 fd]#cat 10 ---查看的結(jié)果和在另外一個終端封豪,用cat命令查看是一樣的
hello,I am zhangdazhi
welcome to magedu.com
CentOS release 6.9 (Final)
Kernel \r on an \m
\l
\n
\t
- 如何自己分配一個文件描述符
[root@centos6 ~]#touch f2 ---創(chuàng)建一個f2文件
[root@centos6 ~]#ls
anaconda-ks.cfg Downloads install.log passwd.txt Templates Videos
Desktop f1 install.log.syslog Pictures typescript win.txt
Documents f2 Music Public unix.txt
[root@centos6 ~]#exec 5<>/root/f2 ---分配一個文件描述符5
[root@centos6 ~]#cd /proc/$$/fd ---進入fd目錄
[root@centos6 fd]#ll
total 0
lrwx------. 1 root root 64 Jul 21 07:44 0 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 1 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 19:20 10 -> /root/f1
lrwx------. 1 root root 64 Jul 21 18:54 2 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 255 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 19:48 5 -> /root/f2 ---可以看到文件描述符
[root@centos6 fd]#exec 5>&- 關(guān)閉
[root@centos6 fd]#ll
total 0
lrwx------. 1 root root 64 Jul 21 07:44 0 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 1 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 19:20 10 -> /root/f1
lrwx------. 1 root root 64 Jul 21 18:54 2 -> /dev/pts/0
lrwx------. 1 root root 64 Jul 21 18:54 255 -> /dev/pts/0
2、重定向和管道
- set命令
[root@centos6 ~]#cat f1
anaconda-ks.cfg
Desktop
Documents
Downloads
f1
f2
install.log
install.log.syslog
Music
passwd.txt
Pictures
Public
Templates
typescript
unix.txt
Videos
win.txt
[root@centos6 ~]#set -C ---禁止覆蓋原有文件
[root@centos6 ~]#cat /etc/issue >f1
-bash: f1: cannot overwrite existing file
[root@centos6 ~]#cat /etc/issue >| f1 ---強行覆蓋原有文件
[root@centos6 ~]#cat f1
CentOS release 6.9 (Final)
Kernel \r on an \m
\l
\n
\t
[root@centos6 ~]#set +C ---允許覆蓋炒瘟,默認是這種模式
[root@centos6 ~]#hostname >f1
[root@centos6 ~]#cat f1
centos6.magedu.com
- 把對的和錯的都重定向到一個文件里的三種方法
[root@centos6 ~]#ls /app /err >f2 2>&1 ---比較老的寫法
[root@centos6 ~]#cat f2
ls: cannot access /err: No such file or directory
/app:
fa
fb
[root@centos6 ~]#ls /app /err &> f2 ---比較新和常用的寫法
[root@centos6 ~]#cat f2
ls: cannot access /err: No such file or directory
/app:
fa
fb
[root@centos6 ~]#ls /app /err >&f2
[root@centos6 ~]#cat f2
ls: cannot access /err: No such file or directory
/app:
fa
fb
- 多個命令重定向到一個文件里吹埠,用()
[root@centos6 ~]#(ls;hostname)>f1
[root@centos6 ~]#cat f1
anaconda-ks.cfg
Desktop
Documents
Downloads
f1
f2
install.log
install.log.syslog
Music
passwd.txt
Pictures
Public
Templates
typescript
unix.txt
Videos
win.txt
centos6.magedu.com
- >軟連接文件
[root@centos6 ~]#ln -s ../root/f2 /app/link1---創(chuàng)建f2文件的軟連接放到/app/link1
[root@centos6 ~]#ll /app/link1
lrwxrwxrwx. 1 root root 10 Jul 21 20:23 /app/link1 -> ../root/f2---創(chuàng)建成功
[root@centos6 ~]#> /app/link1 ---重定向
[root@centos6 ~]#ll f2 ---源文件被破壞
-rw-r--r--. 1 root root 0 Jul 21 20:23 f2
[root@centos6 ~]#ll /app/link1
lrwxrwxrwx. 1 root root 10 Jul 21 20:23 /app/link1 -> ../root/f2
結(jié)論:>軟連接文件,會將軟連接的源文件破壞。禁用
- tr 命令
選項 -c 取反 -d刪除 -s 壓縮連續(xù)缘琅、重復(fù)的字符 -t 對齊
這里解釋一下-t 粘都、-d選項
[root@centos6 ~]#tr 'abc' '1234'
abcdef
123def
[root@centos6 ~]#tr 'abcd' '123' ---d沒有替換的,就替換成之后一個3
abcdef
1233ef
[root@centos6 ~]#tr -t 'abcd' '123' ---加上t選項刷袍,d就不替換了
abcdef
123def
[root@centos6 ~]#tr -d 'abc\n'</etc/issue ---這里的\n只表示回車換行翩隧,不表示/n本身
CentOS relese 6.9 (Finl)Kernel \r on n \m\l\n\
如何將windows格式轉(zhuǎn)換為linux格式
[root@centos6 ~]#hexdump -C win.txt ---以十六進制顯示,比linux中多了一個0d呻纹,0d相當于十進制的13
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007
[root@centos6 ~]#bc ---計算處十進制的13為八進制的15
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=8
13
15
quit
[root@centos6 ~]#tr -d '\015' <win.txt >linux.t ---將八進制的\015刪除
[root@centos6 ~]#hexdump -C linux.t
00000000 61 0a 62 0a 63 |a.b.c|
00000005
- 多行重定向
[root@centos6 ~]#cat >f1<<end
> a
> b
> c
> end
[root@centos6 ~]#cat f1
a
b
c
發(fā)郵件
[root@centos6 ~]#mail -s "hello" root<<end ---hello是標題堆生,root代表發(fā)給root這個用戶,-s subject
> how are you
> and you
> end
總結(jié):看郵件用mail雷酪,點1封郵件淑仆,quit是退出郵件。也可以將郵件內(nèi)容寫在一個文件里哥力,然后用mail -s 標準輸入的重定向這個文件蔗怠,就可以獲取文件的內(nèi)容并發(fā)送出去。
- 管道
[root@centos7 ~]#ls /err 2>&1 |tr 'a-z' 'A-Z'
LS: CANNOT ACCESS /ERR: NO SUCH FILE OR DIRECTORY
[root@centos7 ~]#ls /err |& tr 'a-z' 'A-Z'
LS: CANNOT ACCESS /ERR: NO SUCH FILE OR DIRECTORY
總結(jié):管道只能把正確的標準輸出傳給后面的命令作為標準輸入吩跋,不能把錯誤的標準輸出傳給后面的命令寞射,因此要把錯誤的轉(zhuǎn)化為爭取的,上面兩種方法都可以锌钮。
- tee
[root@centos6 ~]#hostname |tee f2
centos6.magedu.com
[root@centos6 ~]#cat f2
centos6.magedu.com
總結(jié):把命令的執(zhí)行結(jié)果在屏幕上顯示桥温,同時重定向到文件中。如果f2文件存在梁丘,則覆蓋侵浸,要想不覆蓋使用-a選項進行追加。
- less和more
ll /etc|less ---可以分頁查看兰吟,用pgup和pgdn進行上下翻頁通惫,q退出
ll /etc/more --- 也可以分頁查看茂翔,但不能翻頁混蔼,到底會退出
- 管道中的 -
[root@centos6 ~]#tar -cvf - /home|tar -xvf -
表示把home目錄打包成一個文件后,通過管道傳給后面珊燎,再把這個文件解包惭嚣,用-代替這個文件。
3悔政、用戶和組權(quán)限管理
- 用戶和組的配置文件
/etc/passwd:用戶及其屬性信息(名稱晚吞、UID、主組ID等)
/etc/group:組及其屬性信息
/etc/shadow:用戶密碼及其相關(guān)屬性
/etc/gshadow:組密碼及其相關(guān)屬性
[root@centos6 ~]#getent group admins
admins:x:502:natasha,harry
[root@centos6 ~]#getent gshadow admins
admins:!::natasha,harry
總結(jié):這個/etc/group和/etc/gshadow這兩個文件中的組成員要保持一致谋国,admins是他們的附加組槽地。
- newgrp 臨時切換主組
[root@centos6 ~]#su - zhang -zhang
[zhang@centos6 ~]$id ---用戶zhang不屬于admins組
uid=500(zhang) gid=500(zhang) groups=500(zhang)
[zhang@centos6 ~]$newgrp admins
Password: ---需要密碼
[zhang@centos6 ~]$id
uid=500(zhang) gid=502(admins) groups=502(admins),500(zhang)
[root@centos6 ~]#su - harry
[harry@centos6 ~]$ id--- 用戶Harry屬于admins組
uid=503(harry) gid=504(harry) groups=504(harry),502(admins)
[harry@centos6 ~]$ newgrp admins
[harry@centos6 ~]$ id
uid=503(harry) gid=502(admins) groups=502(admins),504(harry)
總結(jié):一個用戶要想把一個組臨時切換成主組,要看這個用戶是否屬于這個組,如果屬于這個組捌蚊,則不需要密碼集畅,否則需要密碼。
- 新建用戶的相關(guān)文件
etc/default/useradd -新建用戶的默認文件夾
/etc/skel/* -家目錄的模板文件夾
/etc/login.defs -郵箱路徑缅糟、密碼策略挺智、加密算法、umask等
[root@centos6 ~]#cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos6 skel]#ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla
# *REQUIRED*
# Directory where mailboxes reside, _or_ name of file, relative to the
# home directory. If you _do_ define both, MAIL_DIR takes precedence.
# QMAIL_DIR is for Qmail
#
#QMAIL_DIR Maildir
MAIL_DIR /var/spool/mail ---郵箱
#MAIL_FILE .mail
# Password aging controls: ----密碼策略
#
# PASS_MAX_DAYS Maximum number of days a password may be used.
# PASS_MIN_DAYS Minimum number of days allowed between password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a password expires.
#
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512 ---加密算法
- useradd 創(chuàng)建用戶
[root@centos6 mail]#useradd -N tom
[root@centos6 mail]#getent passwd tom
tom:x:504:100::/home/tom:/bin/bash
[root@centos6 mail]#id tom
uid=504(tom) gid=100(users) groups=100(users)
總結(jié):加上N選項窗宦,作用是不使用tom作為主組(私用組)赦颇,而使用users做為主組。
[root@centos6 ~]#useradd -r -s /sbin/nologin nginx
[root@centos6 ~]#id nginx
uid=495(nginx) gid=491(nginx) groups=491(nginx)
[root@centos6 ~]#cd /home
[root@centos6 home]#ls
gentoo harry natasha tom zhang
[root@centos6 home]#cd /var/spool/mail
[root@centos6 mail]#ls
Distribution gentoo harry natasha root rpc tom zhang
總結(jié):創(chuàng)建系統(tǒng)用戶用-r選項赴涵,不創(chuàng)建郵箱和家目錄媒怯。
- useradd -D 顯示或更改默認設(shè)置
[root@centos7 ~]#useradd -D ---顯示默認設(shè)置
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos7 ~]#useradd -D -s /bin/csh---更改shell類型
[root@centos7 ~]#useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/csh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos7 ~]#useradd -D -b /app ---更改家目錄路徑
[root@centos7 ~]#useradd -D
GROUP=100
HOME=/app
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos7 ~]#useradd -D -g zhang ---更改默認主組。
[root@centos7 ~]#useradd -D
GROUP=1000
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
- usermod 搬家
[root@centos6 ~]#usermod -d /app/natashaxinjia -m natasha
[root@centos6 ~]#ls /app ---執(zhí)行成功
f1 f11 f2 fa fb natashaxinjia passwd.txt unix.txt win.txt
[root@centos6 ~]#
[root@centos6 app]#usermod -d /app/home tom---只更改家目錄的路徑而不搬家
[root@centos6 app]#getent passwd tom
tom:x:504:100::/app/home:/bin/bash
[root@centos6 app]#usermod -d /app/home -m tom ---執(zhí)行不成功
usermod: no changes
總結(jié):搬家用-d和-m選項句占,如果先-d沪摄,更改家目錄的路徑,發(fā)現(xiàn)不會搬家纱烘,只是把路徑改變了杨拐,但如果接著執(zhí)行再加上-m選項,會發(fā)現(xiàn)執(zhí)行不成功擂啥,所以要執(zhí)行搬家命令哄陶,就不能先執(zhí)行只更改家目錄路徑的命令。
- su 切換身份執(zhí)行命令在切換回原身份
[zhang@centos6 ~]$su - root -c 'cat /etc/shadow'
Password:
需要輸入密碼哺壶。
- 鎖定用的兩種方法
[root@centos6 ~]#usermod -L harry
[root@centos6 ~]#getent shadow harry
harry:!$6$KieKpTJ/$S/6MseMjC.vC66Uo4Tmz7gyaq..cUcXklgo/Mo9Mg4/0EjPg5ghSbMJg6un5s5Dmj12yTHqKYK2skTf1PgBA/0:17368:0:99999:7:::
[root@centos6 ~]#passwd -l sarah
Locking password for user sarah.
passwd: Success
[root@centos6 ~]#getent shadow sarah
sarah:!!$6$d4W3guwdC6/$zdizqvqB3tYAlr.XCbR0H/qu/E9L8nTWWRfld2iFNtO8Zw0g9R5u57nYmq4IXU5NWKpOTyU6ukObNad2QiU2y1:17368::::::
總結(jié):兩種方法都尅鎖定賬號屋吨,但passwd鎖定后會出現(xiàn)兩個!山宾,比較安全至扰。
- 設(shè)置密碼策略
chage[OPTION]... LOGIN
-d LAST_DAY
-E --expiredateEXPIRE_DATE
-I --inactive INACTIVE
-m --mindaysMIN_DAYS
-M --maxdaysMAX_DAYS
-W --warndaysWARN_DAYS
–l 顯示密碼策略
舉例:兩種方法強制用戶下次登錄重新設(shè)置密碼
root@centos6 ~]#passwd -e harry
Expiring password for user harry.
passwd: Success
[root@centos6 ~]#getent shadow harry
harry:$6$IVts56qg$0Jzt3Nl6wM6d5TNpmqf2uhKCqit.bBy5zEsrtHeVbOvnEJ1HqjbDi3ELS8cwpJAYBqrgIW803D/6EVS4wdqXW0:0:0:99999:7:::
[root@centos6 ~]#chage -d 0 sarah
[root@centos6 ~]#getent shadow sarah
sarah:$6$d4W3guwdC6/$zdizqvqB3tYAlr.XCbR0H/qu/E9L8nTWWRfld2iFNtO8Zw0g9R5u57nYmq4IXU5NWKpOTyU6ukObNad2QiU2y1:0::::::
更改密碼策略
[root@centos6 ~]#chage -m 0 -M 42 -W 7 -I 5 -E 2018-12-31 tom
[root@centos6 ~]#getent shadow tom
tom:!!:17368:0:42:7:5:17896:
- 給用戶添加附加組的三種方法
[root@centos6 ~]#id tom
uid=504(tom) gid=505(tom) groups=505(tom)
[root@centos6 ~]#usermod -G admins,harry tom ---第一種方法 如果tom之前有附加組,會覆蓋原有的附加組资锰,如果想不覆蓋使用-a選項
[root@centos6 ~]#id tom
uid=504(tom) gid=505(tom) groups=505(tom),502(admins),504(harry)
[root@centos6 ~]#gpasswd -a tom zhang ---- 第二種方法
Adding user tom to group zhang
[root@centos6 ~]#id tom
uid=504(tom) gid=505(tom) groups=505(tom),500(zhang),502(admins),504(harry)
[root@centos6 ~]#groupmems -a tom -g gentoo ---第三種方法
[root@centos6 ~]#id tom
uid=504(tom) gid=505(tom) groups=505(tom),500(zhang),501(gentoo),502(admins),504(harry)
[root@centos6 ~]#groups tom
tom : tom zhang gentoo admins harry
- 設(shè)置組的管理員
[root@centos6 ~]#gpasswd -A zhang,harry admins
[root@centos6 ~]#getent gshadow admins
admins:$6$fWFbAvMjJ2/oXGHL$6oQAPJnosWoc0rVG4EpFBoQXeys7Ejf3jBP6ziVsqZJuPWm9cRMlsTEt0O3nx4wMf5/ATsvsGe2Qo/2PrNen41:zhang,harry:natasha,harry,tom
總結(jié):用戶是不是屬于該組都可以作為該組的管理員敢课。
- 顯示組
[root@centos6 ~]#groups tom ---顯示tom屬于哪些附加組
tom : tom zhang gentoo admins harry
[root@centos6 ~]#groupmems -l -g tom
[root@centos6 ~]#groupmems -l -g admins ---列出組的成員
natasha harry tom
4、文件權(quán)限
- 更改文件的所有者和所屬組
[root@centos6 ~]#su - zhang
[zhang@centos6 ~]$touch f4
[zhang@centos6 ~]$ll
total 0
-rw-rw-r--. 1 zhang zhang 0 Jul 22 11:44 f4
[zhang@centos6 ~]$chown tom f4 ---普通用戶沒有權(quán)限更改文件的所有者
chown: changing ownership of `f4': Operation not permitted
[zhang@centos6 ~]$id zhang
uid=500(zhang) gid=500(zhang) groups=500(zhang),0(root),502(admins) ---zhang屬于admins組
[zhang@centos6 ~]$chgrp admins f4
[zhang@centos6 ~]$ll
total 0
-rw-rw-r--. 1 zhang admins 0 Jul 22 11:44 f4 ---更改成功
[zhang@centos6 ~]$chgrp tom f4
chgrp: changing group of `f4': Operation not permitted
總結(jié):文件的所有者只有root身份才能更改绷杜;文件的所屬組(主組)直秆,除了root身份外,文件的所有者也有權(quán)限更改所屬組鞭盟,前提是這個用戶屬于要更改為的組圾结。
- 同時更改文件的所有者和所屬組
[root@centos6 app]#ll
total 36
-rw-r--r--. 1 zhang zhang 6 Jul 21 20:56 f1
drwxr-xr-x. 2 root root 4096 Jul 22 09:17 f11
-rw-r--r--. 1 root root 38 Jul 21 21:17 f2 ---更改f2文件
-rw-r--r--. 1 root root 97 Jul 21 09:38 fa
-rw-r--r--. 1 root root 6 Jul 21 10:15 fb
drwx------. 4 natasha natasha 4096 Jul 21 16:01 natashaxinjia
-rw-r--r--. 1 root root 41 Jul 21 16:06 passwd.txt
-rw-r--r--. 1 root root 6 Jul 20 15:29 unix.txt
-rw-r--r--. 1 root root 7 Jul 20 15:30 win.txt
[root@centos6 app]#chown harry:admins f2 ---用chown命令
[root@centos6 app]#ll
total 36
-rw-r--r--. 1 zhang zhang 6 Jul 21 20:56 f1
drwxr-xr-x. 2 root root 4096 Jul 22 09:17 f11
-rw-r--r--. 1 harry admins 38 Jul 21 21:17 f2 ---已更改
-rw-r--r--. 1 root root 97 Jul 21 09:38 fa
-rw-r--r--. 1 root root 6 Jul 21 10:15 fb
drwx------. 4 natasha natasha 4096 Jul 21 16:01 natashaxinjia
-rw-r--r--. 1 root root 41 Jul 21 16:06 passwd.txt
-rw-r--r--. 1 root root 6 Jul 20 15:29 unix.txt
-rw-r--r--. 1 root root 7 Jul 20 15:30 win.txt
[root@centos6 app]#chown :admins f1 ---只更改所屬組
[root@centos6 app]#ll
total 36
-rw-r--r--. 1 zhang admins 6 Jul 21 20:56 f1
drwxr-xr-x. 2 root root 4096 Jul 22 09:17 f11
-rw-r--r--. 1 harry admins 38 Jul 21 21:17 f2
-rw-r--r--. 1 root root 97 Jul 21 09:38 fa
-rw-r--r--. 1 root root 6 Jul 21 10:15 fb
drwx------. 4 natasha natasha 4096 Jul 21 16:01 natashaxinjia
-rw-r--r--. 1 root root 41 Jul 21 16:06 passwd.txt
-rw-r--r--. 1 root root 6 Jul 20 15:29 unix.txt
-rw-r--r--. 1 root root 7 Jul 20 15:30 win.txt