Linux 中的 chattr 及 lsattr 指令

有時身為 root 使用者時,還是會有不小心誤刪檔案,以防被自己雷才有這篇 … 在 Linux 中有一個指令 chattr 可以改變檔案屬性;還有另外一個指令 lsattr 可以列出檔案屬性(檔案屬性並不是檔案權限,在 ls 中看不出來) attributes 屬性 總共有這些屬性,詳細請看手冊 a: append only A: no atime updates c: compressed C: no copy on write d: no dump D: synchronous directory updates e: extent format i: immutable j: data journalling P: project hierarchy s: secure deletion S: synchronous updates t: no tail-merging T: top of directory hierarchy u: undeletable 我自己常用的屬性是 a, i a 屬性 只能以追加模式寫入檔案 i 屬性 不能被刪除、重新命名 不能建立 link 不能修改 metadata 不能寫入檔案 chattr 語法如下...

2021-08-01 · Jett

更新 Linux Sudo 漏洞

最近看到 這篇 提到 sudo 有安全性漏洞 也看到 這篇 有提供指令可以檢查,看看是否受到此漏洞的影響 sudoedit -s / sudoedit: /: not a regular file 自己檢測一下,發現居然是漏洞版本,看來需要更新套件了 在 apt update 後可以看到有哪些套件是可更新的,剛好有看到 sudo 這個套件,那就來更新吧 sudo apt update sudo apt list --upgradable | grep sudo sudo apt --only-upgrade install sudo 更新後再用指令檢查一下,結果符合預期 usage: sudoedit [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ... 參考資料: https://www.sudo.ws/alerts/unescape_overflow.html

2021-01-30 · Jett

Linux 中的 Log Rotation

最近在 Linux 主機中 /var/log 資料夾底下發現有許多的 xxx.log.1, xxx.log.2 的檔案,好奇去查並記錄下來 這些檔案是 log rotate 產生的檔案,主要功能是做日誌檔案的輪替 logrotate 設定檔 主要設定檔的路徑是在 /etc/logrotate.conf,會載入 /etc/logrotate.d/ 底下的檔案,根據設定檔進行 rotate 執行 logrotate logrotate 預設會在每日的 crontab 中執行 (/etc/cron.daily/logrotate),如果想要手動執行 logrotate 的話,可以直接呼叫 logrotate --force 指令,--force 是強制執行 rotate 檔案,可搭配 --debug 一起使用並觀察 logrotate 範例 在 /etc/logrotate.conf 檔案中定義 logrotate 的預設值,範例如下 # 設定頻率對日誌檔做 rotate (hourly, daily, weekly, monthly, yearly) (hourly 需要改變 logrotate 的頻率) # weekly [weekday] <- default 0 (0 means Sunday, 1 means Monday, ... , 6 means Saturday) weekly # 日誌被 rotate 了 4 次後刪除舊日誌 rotate 4 # rotate 舊日誌文件後創建新日誌文件權限 # create [mode(octal)] [owner] [group]....

2020-11-05 · Jett

Linux 中的 tar 指令

在 Linux 中常常看到副檔名 .tar.gz 的檔案,紀錄一下過程 打包檔案 副檔名包含 .tar 的檔案,都是使用 tar 指令進行打包,也代表未使用壓縮的檔案 看一下怎麼打包檔案 tar -c -v -f a.tar /etc -c, --create: create a new ARCHIVE -f, --file=ARCHIVE: use ARCHIVE file or device ARCHIVE -v, --verbose: verbosely list files processed 壓縮檔案 副檔名 .tar.gz 的檔案,是經過 gzip 壓縮後的 tar 檔案,可以縮寫成 .tgz 看一下怎麼壓縮檔案,多一個參數 -z tar -c -z -v -f a.tar.gz /etc -z, --gzip Filter the archive through gzip(1) 解壓縮檔案 看一下怎麼解壓縮檔案,使用 -x 參數 tar -x -v -f a....

2020-10-04 · Jett

Linux 中的 sed 指令

在 Linux 的 sed 版本是 GNU,在 macOS 的 sed 版本是 BSD,兩者在使用上略有不同,踩到這個雷,紀錄一下 -i 參數的差異 最常見的就是 -i,如果沒有提供後綴,則原始文件將被覆蓋而不進行備份(in place) 下面的指令在 macOS 中是可以執行的 cat << EOF > test.txt foo bar baz EOF sed -e 's/foo/bar/g' -i '' test.txt 但是在 Linux 中卻會噴錯 sed: can't read : No such file or directory 在 Linux 中只需要使用 -i 即可 sed -e 's/foo/bar/g' -i test.txt 更改 separator 另外一個技巧就是更換 separator s/regular expression/replacement/flags 如果使用 / 當作 separator,在正規表示式中有用到 / 的地方,需要使用 \ 跳脫,這會造成很難閱讀...

2020-09-10 · Jett