day16課堂筆記

1、selinux是什么?

安全規(guī)則,讓Linux系統(tǒng)更安全的一套規(guī)則。

這個規(guī)則太嚴(yán)格了,一般的情況下都會關(guān)閉selinux。

自己開啟防火墻啊,用其他手段來實現(xiàn)同樣的安全目的。

2、怎么關(guān)掉Selinux?

查看方法:

[root@oldboyedu ~]# getenforce

Enforcing

臨時關(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ī),防止被入侵。

systemctl status firewalld.service

開啟firewalld(C6 service iptables start 或者/etc/init.d/iptables start)

開啟防火墻: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開機(jī)自啟動

systemctl enable firewalld.service

C6用法:chkconfig iptables off

C7就是一個命令systemctl

自學(xué)能力:總結(jié)兩個命令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)號的文件互為硬鏈接。

一個文件的兩個入口。

[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、測試

創(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/目錄,對應(yīng)一個分區(qū)

/home目錄,還可能對應(yīng)一個分區(qū)。

ln /etc/ /home/oldboy

軟鏈接:

本質(zhì)是快捷方式,指向源文件實體,本身和源文件是不同的文件。

文件

[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

工作中為什么會使用軟鏈接。

第一個用途:

安裝軟件:/application/nginx-1.10

過半年:/application/nginx-1.20

導(dǎo)致一個問題,工作中,開發(fā)等引用/application/nginx-1.10路徑。

安裝軟件:/application/nginx-1.10===>/application/nginx(讓開發(fā)用)

過半年:? /application/nginx-1.20===>/application/nginx(讓開發(fā)用)

第二個用途:

/etc/對應(yīng)的分區(qū)要滿了,沒法放很多文件,但是程序還想通過/etc/目錄訪問文件。

此時,我們可以把文件放入/opt/oldboy下,然后做一個到/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

必會面試題:軟鏈接和硬鏈接的區(qū)別?

Linux文件刪除原理:

1、靜態(tài)文件:沒有進(jìn)程或程序正在訪問的文件。

所有的硬鏈接數(shù)為0(i_link),即所有硬鏈接都被干掉了,包括自身。

硬鏈接的數(shù)量的代表變量符號i_link

rm -f oldboy.txt oldboy_hard_link

執(zhí)行完 其實文件也沒刪,關(guān)機(jī)停止運(yùn)行。

a.系統(tǒng)定時清理沒有文件名的inode。

b.磁盤檢查會清理。

c.增加新文件時優(yōu)先占用沒有文件名的inode。

恢復(fù)的工具:debugfs,ext3grep等等。

亡羊補(bǔ)牢其實不可取的,違反運(yùn)維的三大核心原則。

多備份,操作前備份,異服務(wù)器和異地備份。

2、動態(tài)文件:有程序或進(jìn)程訪問的文件

刪除原理:

a.所有硬鏈接刪除。i_link為0.

b..i_count是進(jìn)程調(diào)用文件的數(shù)量(引用計數(shù))。所有進(jìn)程調(diào)用都要停止取消。i_count為0

3、實踐文件刪除原理

環(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é)果。

實踐:

[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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容