Day13
歸檔:課堂筆記
日期:2019/3/18
[root@oldboyedu ~]# ls -lhi
total 32K
33631870 -rw-r--r--. 1 root root? ? ? 4 Mar 13? 2019 a.txt
第二列結(jié)尾的. 是標(biāo)識(shí)selinux的符號(hào)
1汛闸、selinux是什么盈厘?
安全規(guī)則,讓Linux系統(tǒng)更安全的一套規(guī)則丑蛤。
這個(gè)規(guī)則太嚴(yán)格了唯竹,一般的情況下都會(huì)關(guān)閉selinux米奸。
自己開(kāi)啟防火墻啊颖侄,用其他手段來(lái)實(shí)現(xiàn)同樣的安全目的萄凤。
2丹皱、怎么關(guān)掉Selinux妒穴?
查看方法:
[root@oldboyedu ~]# getenforce
Enforcing
臨時(shí)關(guān)掉:
[root@oldboyedu ~]# setenforce
usage:? setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@oldboyedu ~]# setenforce 0
[root@oldboyedu ~]# getenforce
Permissive
永久關(guān)閉:
vim /etc/selinux/config
[root@oldboyedu ~]# grep dis /etc/selinux/config
#? ? disabled - No SELinux policy is loaded.
SELINUX=disabled
Linux里防火墻C6 iptables? C7 firewalld
作用:防護(hù)計(jì)算機(jī),防止被入侵摊崭。
systemctl status firewalld.service
開(kāi)啟firewalld(C6 service iptables start 或者/etc/init.d/iptables start)
開(kāi)啟防火墻:systemctl start firewalld.service
[root@oldboyedu ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
? Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
? Active: active (running) since Sun 2020-10-04 20:48:49 CST; 1s ago
? ? Docs: man:firewalld(1)
Main PID: 7003 (firewalld)
? CGroup: /system.slice/firewalld.service
? ? ? ? ? └─7003 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Oct 04 20:48:48 oldboyedu systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 04 20:48:49 oldboyedu systemd[1]: Started firewalld - dynamic firewall daemon.
讓firewalld開(kāi)機(jī)自啟動(dòng)
systemctl enable firewalld.service
C6用法:chkconfig iptables off
C7就是一個(gè)命令systemctl
自學(xué)能力:總結(jié)兩個(gè)命令systemctl和chkconfig讼油。
[root@oldboyedu ~]# ls -lhi
total 32K
33631870 -rw-r--r--. 1 root root? ? ? 4 Mar 13? 2019 a.txt
? ? ? ? ? ? ? ? ? 硬鏈接數(shù)
1、什么是硬鏈接呢簸?
具有相同inode節(jié)點(diǎn)號(hào)的文件互為硬鏈接矮台。
一個(gè)文件的兩個(gè)入口乏屯。
[root@oldboyedu ~]# ls -ldi test test/.
16964029 drwxr-xr-x. 2 root root 4096 Oct? 1 14:07 test
16964029 drwxr-xr-x. 2 root root 4096 Oct? 1 14:07 test/.
[root@oldboyedu /data]# ls -ldi oldboy
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy
[root@oldboyedu /data]# ls -ldi oldboy oldboy/. oldboy/test{1..3}/..
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy/.
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy/test1/..
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy/test2/..
33631853 drwxr-xr-x. 5 root root 45 Oct? 4 21:03 oldboy/test3/..
2、硬鏈接原理瘦赫。
3辰晕、測(cè)試
創(chuàng)建硬鏈接:
ln 源文件 硬鏈接文件
a.文件硬鏈接
[root@oldboyedu /data]# echo "I am oldboy." >oldboy.txt
[root@oldboyedu /data]# cat oldboy.txt
I am oldboy.
[root@oldboyedu /data]# ln oldboy.t
oldboy.tar.gz? oldboy.txt? ?
[root@oldboyedu /data]# ln oldboy.txt oldboy_hard_link
[root@oldboyedu /data]# ls -lirt
total 20
33631860 -rw-r--r--. 1 root root? 0 Oct? 1 00:19 5.txt
33631855 -rw-r--r--. 1 root root? 0 Oct? 1 00:19 4.txt
33631847 -rw-r--r--. 1 root root? 0 Oct? 1 00:19 3.txt
33631845 -rw-r--r--. 1 root root? 0 Oct? 1 00:19 2.txt
33631841 -rw-r--r--. 1 root root? 0 Oct? 1 00:19 1.txt
33656236 -rw-r--r--. 1 root root? 18 Oct? 1 09:01 paichu.txt
33631869 -rw-r--r--. 1 root root 187 Oct? 1 09:01 oldboy.tar.gz
33656257 -rwxr-xr-x. 1 root root? 14 Oct? 1 11:07 test.txt
33631853 drwxr-xr-x. 5 root root? 45 Oct? 4 21:03 oldboy
33656238 -rw-r--r--. 2 root root? 13 Oct? 4 21:10 oldboy.txt
33656238 -rw-r--r--. 2 root root? 13 Oct? 4 21:10 oldboy_hard_link
作用:
1、備份确虱,防止誤刪含友。
[root@oldboyedu /data]# ln /etc/hostname /opt/hostname
[root@oldboyedu /data]# cat /opt/hostname
oldboyedu
[root@oldboyedu /data]# rm -f /etc/hostname
[root@oldboyedu /data]# cat /etc/hostname
cat: /etc/hostname: No such file or directory
[root@oldboyedu /data]# cat /opt/hostname
oldboyedu
[root@oldboyedu /data]# ln /opt/hostname /etc/hostname
[root@oldboyedu /data]# cat /etc/hostname
oldboyedu
b.目錄硬鏈接
不支持人工創(chuàng)建目錄硬鏈接。
[root@oldboyedu /data]# ln oldboy oldgirl
ln: ‘oldboy’: hard link not allowed for directory
老男孩的思考:
/etc/目錄校辩,對(duì)應(yīng)一個(gè)分區(qū)
/home目錄窘问,還可能對(duì)應(yīng)一個(gè)分區(qū)。
ln /etc/ /home/oldboy
軟鏈接:
本質(zhì)是快捷方式宜咒,指向源文件實(shí)體惠赫,本身和源文件是不同的文件。
文件
[root@oldboyedu /data]# !echo
echo "I am oldboy." >oldboy.txt
[root@oldboyedu /data]# cat oldboy.txt
I am oldboy.
[root@oldboyedu /data]# ln -s oldboy.txt? oldboy_soft_link
目錄:是工作中的重點(diǎn)
[root@oldboyedu /data]# mkdir oldboy
[root@oldboyedu /data]# touch oldboy/test.txt
[root@oldboyedu /data]# ln -s oldboy? oldboy_soft_link_dir
[root@oldboyedu /data]# ls oldboy_soft_link_dir/
test.txt
[root@oldboyedu /data]# ls oldboy
test.txt
工作中為什么會(huì)使用軟鏈接故黑。
第一個(gè)用途:
安裝軟件:/application/nginx-1.10
過(guò)半年:/application/nginx-1.20
導(dǎo)致一個(gè)問(wèn)題儿咱,工作中,開(kāi)發(fā)等引用/application/nginx-1.10路徑场晶。
安裝軟件:/application/nginx-1.10===>/application/nginx(讓開(kāi)發(fā)用)
過(guò)半年:? /application/nginx-1.20===>/application/nginx(讓開(kāi)發(fā)用)
第二個(gè)用途:
/etc/對(duì)應(yīng)的分區(qū)要滿了混埠,沒(méi)法放很多文件,但是程序還想通過(guò)/etc/目錄訪問(wèn)文件峰搪。
此時(shí)岔冀,我們可以把文件放入/opt/oldboy下,然后做一個(gè)到/etc/oldboy的軟鏈接概耻。
[root@oldboyedu /data]# mkdir /opt/oldboy
[root@oldboyedu /data]# touch /opt/oldboy/{1..3}.txt
[root@oldboyedu /data]# ln -s /opt/oldboy/ /etc/oldboy
[root@oldboyedu /data]# ls /etc/oldboy
1.txt? 2.txt? 3.txt
[root@oldboyedu /data]# ll /opt/oldboy
total 0
-rw-r--r--. 1 root root 0 Oct? 4 22:02 1.txt
-rw-r--r--. 1 root root 0 Oct? 4 22:02 2.txt
-rw-r--r--. 1 root root 0 Oct? 4 22:02 3.txt
必會(huì)面試題:軟鏈接和硬鏈接的區(qū)別使套?
Linux文件刪除原理:
1、靜態(tài)文件:沒(méi)有進(jìn)程或程序正在訪問(wèn)的文件鞠柄。
所有的硬鏈接數(shù)為0(i_link)侦高,即所有硬鏈接都被干掉了,包括自身厌杜。
硬鏈接的數(shù)量的代表變量符號(hào)i_link
rm -f oldboy.txt oldboy_hard_link
執(zhí)行完 其實(shí)文件也沒(méi)刪奉呛,關(guān)機(jī)停止運(yùn)行。
a.系統(tǒng)定時(shí)清理沒(méi)有文件名的inode夯尽。
b.磁盤(pán)檢查會(huì)清理瞧壮。
c.增加新文件時(shí)優(yōu)先占用沒(méi)有文件名的inode。
恢復(fù)的工具:debugfs,ext3grep等等匙握。
亡羊補(bǔ)牢其實(shí)不可取的咆槽,違反運(yùn)維的三大核心原則。
多備份圈纺,操作前備份秦忿,異服務(wù)器和異地備份麦射。
2、動(dòng)態(tài)文件:有程序或進(jìn)程訪問(wèn)的文件
刪除原理:
a.所有硬鏈接刪除灯谣。i_link為0.
b..i_count是進(jìn)程調(diào)用文件的數(shù)量(引用計(jì)數(shù))潜秋。所有進(jìn)程調(diào)用都要停止取消。i_count為0
3胎许、實(shí)踐文件刪除原理
環(huán)境準(zhǔn)備峻呛,命令集合如下:
mkdir -p /app/logs? ? ? ? ? ? ? ? ? ? ? ? ? #<==創(chuàng)建用于掛載的目錄。
dd if=/dev/zero of=/dev/sdc bs=8K? count=10? #<==創(chuàng)建指定大小的文件呐萨。
mkfs.ext4 /dev/sdc? ? ? ? ? ? ? ? ? ? ? ? ? #<==格式化杀饵。
mount -o loop /dev/sdc /app/logs? ? ? ? ? ? #<==掛載。
df -h? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #<==檢查掛載結(jié)果谬擦。
實(shí)踐:
[root@oldboyedu /data]# cd /app/logs/
[root@oldboyedu /app/logs]# touch nginx.log
[root@oldboyedu /app/logs]# tail -f nginx.log
[root@oldboyedu /app/logs]# cat /etc/services >>nginx.log
cat: write error: No space left on device
模擬進(jìn)程讀文件:
[root@oldboyedu /app/logs]# touch nginx.log
[root@oldboyedu /app/logs]# tail -f nginx.log
ln nginx.log nginx_hard.log
當(dāng)前:
i_link=2
i_count=1
模擬把文件變大,讓分區(qū)滿
[root@oldboyedu /app/logs]# cat /etc/services >>nginx.log
cat: write error: No space left on device
[root@oldboyedu /app/logs]# df -h
Filesystem? ? ? Size? Used Avail Use% Mounted on
/dev/sda3? ? ? ? 19G? 1.9G? 18G? 10% /
devtmpfs? ? ? ? 980M? 80K? 980M? 1% /dev
tmpfs? ? ? ? ? 991M? ? 0? 991M? 0% /dev/shm
tmpfs? ? ? ? ? 991M? 9.5M? 981M? 1% /run
tmpfs? ? ? ? ? 991M? ? 0? 991M? 0% /sys/fs/cgroup
/dev/sda1? ? ? 253M? 136M? 118M? 54% /boot
tmpfs? ? ? ? ? 199M? ? 0? 199M? 0% /run/user/0
/dev/loop0? ? ? 73K? 71K? ? 0 100% /app/logs
分區(qū)滿了:清理朽缎,刪除惨远。
刪除源文件
[root@oldboyedu /app/logs]# rm -f nginx.log
結(jié)果:
i_link=1
i_count=1
ctrl+c 中斷 進(jìn)程調(diào)用文件
結(jié)果:
i_link=1
i_count=0
刪除硬鏈接文件
[root@oldboyedu /app/logs]# rm -f nginx_hard.log
結(jié)果:
i_link=0
i_count=0
[root@oldboyedu /app/logs]# df -h
Filesystem? ? ? Size? Used Avail Use% Mounted on
/dev/sda3? ? ? ? 19G? 1.9G? 18G? 10% /
devtmpfs? ? ? ? 980M? 80K? 980M? 1% /dev
tmpfs? ? ? ? ? 991M? ? 0? 991M? 0% /dev/shm
tmpfs? ? ? ? ? 991M? 9.5M? 981M? 1% /run
tmpfs? ? ? ? ? 991M? ? 0? 991M? 0% /sys/fs/cgroup
/dev/sda1? ? ? 253M? 136M? 118M? 54% /boot
tmpfs? ? ? ? ? 199M? ? 0? 199M? 0% /run/user/0
/dev/loop0? ? ? 73K? 14K? 54K? 21% /app/logs