Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Linux

為 ufw 建立應用程式的 rule set

Posted on 2017 年 5 月 5 日2021 年 3 月 12 日 By 日落 在〈為 ufw 建立應用程式的 rule set〉中尚無留言

以往在 ufw 加入防火牆規則時,都是一條一條新增上去:

ufw allow 80
ufw allow 443
ufw allow 8080
.....

這種方式雖然簡單,但是之後要維護會有點困難:到底哪個 port 是為了哪一個應用程式開的?

ufw 其實還有其他特別功能,叫做 app list:

zero@zero-x230:~$ sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  CUPS
  Samba

會自動增測,並有預設的設定檔來幫你新增 rule set。來看一下「Apache Full」設定檔到底寫了什麼,打開「/etc/ufw/applications.d/」:

[Apache]
title=Web Server
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=80/tcp

[Apache Secure]
title=Web Server (HTTPS)
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=443/tcp

[Apache Full]
title=Web Server (HTTP,HTTPS)
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=80,443/tcp

預設有三個 rule sets,一個是 port 80,另一個是 HTTPS 的 port 443,亦或二者都開啟。

我們可以依樣畫葫蘆,建立自己的 app rule sets,像是我幫 Resilio Sync 湊出來的 rule set。

你也可以參考 Ubuntu forum 上面的教學,建立自己需要的 rule set。

Tags:Linux, Ubuntu, 資訊安全

修改 grub 開機選單等待時間 (timeout)

Posted on 2017 年 1 月 18 日2021 年 3 月 12 日 By 日落 在〈修改 grub 開機選單等待時間 (timeout)〉中尚無留言

查了很多資料,大多都說是去修改 /boot/grub/grub.cnf 的設定,但新版的 grub.cnf 事實上是電腦自動產生的,不管怎麼改參數,數值都會被覆蓋掉。

正確解法應該是直接修改 /etc/default/grub:

GRUB_TIMEOUT=10  # wait for 10 secs

修改完畢後記得更新設定:

sudo update-grub2

詳細的解說,可以參考 Grub2 – Ubuntu community wiki 上的說明。

Tags:Linux, Ubuntu

grep 時保留前後 N 行內文

Posted on 2016 年 9 月 27 日2021 年 3 月 12 日 By 日落 在〈grep 時保留前後 N 行內文〉中尚無留言

一般 grep 只會將出現關鍵字的那一行文字顯示出來,例如:

johnroyer@box:~/logs$ zgrep 'parse' *gz
2016-02-27.log.gz:[2016-02-27 12:00:35] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
2016-02-27.log.gz:[2016-02-27 12:00:36] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
2016-02-27.log.gz:[2016-02-27 12:01:08] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
....

但有時顯示出來的訊息只是 function call stack trace 的其中一行,單看這一行無法理解到底發生了什麼事情。

遇到這種情況時,可以透過參數「-A」和「-B」來設定保留前後文:

johnroyer@box:~/logs$ zgrep 'Exception' *.gz -A 5 -B 2
[2016-05-29 23:41:22] production.INFO: RuntimeException: https://theinitium.com/newsfeed/
[2016-05-29 23:41:22] production.INFO: DOMDocument cannot parse XML: PCDATA invalid Char value 8
[2016-05-29 23:41:23] production.ERROR: exception 'RuntimeException' with message 'Invalid host label, check its content' in /home/segm/prod/www-crawler/vendor/league/url/src/Components/Host.php:164
Stack trace:
#0 /home/segm/prod/www-crawler/vendor/league/url/src/Components/AbstractSegment.php(47): LeagueUrlComponentsHost->validate('rss_Content.jsp')
#1 /home/segm/prod/www-crawler/vendor/league/url/src/Components/AbstractSegment.php(39): LeagueUrlComponentsAbstractSegment->set('rss_Content.jsp')
#2 /home/segm/prod/www-crawler/vendor/league/url/src/Components/Host.php(72): LeagueUrlComponentsAbstractSegment->__construct('rss_Content.jsp')
#3 /home/segm/prod/www-crawler/vendor/league/url/src/AbstractUrl.php(226): LeagueUrlComponentsHost->__construct('rss_Content.jsp')
.....

上面的範例是關鍵字前保留 2 行,往後保留 5 行。

Tags:Bash, Linux, 資訊學習

在 gnome-teminal 使用 Ctrl + arrow 切換 GNU screen 視窗

Posted on 2016 年 6 月 26 日2021 年 3 月 12 日 By 日落 在〈在 gnome-teminal 使用 Ctrl + arrow 切換 GNU screen 視窗〉中有 2 則留言

這邊標題應該下的不是很好,其實問題和 gnome-teminal 應該是沒什麼關係的。

在 windows 透過 pietty (快換 putty 吧) 連上 server 時,都是直接用 Ctrl + 左/右 來切換 windows, .screenrc 設定方式如下:

bindkey 33[C next
bindkey 33[D prev

不過當 client 是 Ubuntu 時,在 gnome-terminal 操作時,Ctrl + arraow 卻完全沒有效果。後來友人提示在 screen 底下可以先 ctrl + V,再按下 key binding,screen 會把收到的 key code 顯示出來,方便 debug。嘗試了不少種組合都沒有成功。

當 Google 第一頁搜尋結果無法找到方法時,只好往第二頁找屍體。慢慢看到有人提到需要修改 /etc/inputrc 的設定:

# allow the use of the Home/End keys
"e[1~": beginning-of-line
"e[4~": end-of-line

# allow the use of the Delete/Insert keys
"e[3~": delete-char
"e[2~": quoted-insert

....

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"e[1;5C": forward-word
"e[1;5D": backward-word
"e[5C": forward-word
"e[5D": backward-word
"ee[C": forward-word
"ee[D": backward-word

設定檔中間可以看到 Ctrl + arrow 已經被轉成 forward-word 等操作,把那幾行註解掉即可。

ps. 除了 ssh client 這邊的 inputrc 需要修改外,server side 若有 inputrc 也需要一起修改,不然 client 送過去的 key binding 還是會被 server 改掉。

Tags:Linux, Ubuntu, 資訊學習

xargs -P 在 stdout 可能會遇到 race condition

Posted on 2016 年 2 月 4 日2021 年 3 月 12 日 By 日落 在〈xargs -P 在 stdout 可能會遇到 race condition〉中尚無留言

爬 log 發現 log 格式不正確,而且還是經常發生,而手動追蹤時又找不到錯誤在哪裡:

find . -name '*2016-01*.log.gz' | xargs -I'{}' -P 4 zgrep keyword {} | awk ...

做了測試以後才發現 xargs -P 時,各個 process 只要有 stdout 就會和其他 process 打架,造成資料還沒寫完就被其他 process 插單,導致最後出來的資料不正確。


先建立二個檔案,儲存不同的二個資料。

0.test.log (每行 50 字):

.................................................
.................................................
.................................................
....

1.test.log (每行 50 字):

1111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111
...

接下來使用 xargs 來 echo 這二個檔案內容:

find . -name '*test.log' | xargs -I'{}' -P 2 cat {} > output.xargs.log

接下來寫個 script 來檢查 output.xargs.log 的內容是否都正確:

for LINE in `cat output.xargs.log `; do
    if [ 50 -lt ${#LINE} ]; then
        echo $LINE
    fi
done

結果會發現 output 有一行超過 50 個自得情況發生:

111111111111111111111111111111111111.................................................

而相同的情況下,parallel 就不會有相同的情況發生:

find . -name '*test.log' | parallel -j 2 cat {} > output.xargs.log

原因是 parallel 會將 jobs (process) 的 output 先 buffer 起來,等到整個 job 都結束以後在一起送到 stdout。若使用上述的範例改用 parallel 的操作來測試的話,可以發現不同 job 的 output 有被完全區隔開來,沒有混在一起:

...
.................................................
.................................................
.................................................
1111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111
...

總之,以後用到 xargs -P 時,要小心 race condition … (暈)

Tags:Bash, Linux

文章分頁

上一頁 1 ... 20 21 22 ... 32 下一頁

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.