JeffHung.Blog

(My smile insists of having nose. :-)

Sudo_root - prompt when needed in script

(這是一篇積存的舊文。)
Sudo 是個很好用的工具,讓我們可以盡可能地減少擁有 root 權限的時間,並隱藏 root 密碼,以提升系統安全性[1]。不過,當我們在使用 shell script,而 script 裡只有一小部分需要用到 root 權限時,卻通常是直接用 sudo 或 su 成 root 執行整個 script,讓 script 裡面大部分沒有需要 root 權限的地方,也獲得了 root 權限。如果,我們能夠善用 sudo,只在 script 裡真正需要 root 權限的地方,才切換成 root 身份執行,這樣應該可以降低 script 其他部分,對系統可能的危害。
最簡單的想法,就是在 script 裡,有需要 root 權限的指令前面,加上 sudo 就好了。由於 sudo 會記錄上一次執行的 time-stamp,因此就算我們加上 sudo 的指令,在一個跑上千次的迴圈裡面,我們也只需要輸入密碼一次。好比說:

#!/bin/sh
# backup all configuration files in /etc.
for i in /etc/*; do
[...]

在 screen 裡顯示 vi 正在編輯的檔案

Rafan 在問,怎樣在 screen 裡顯示 ssh 出去的 hostname。這個以前我有搞過類似的:在 screen 裡顯示 vi 正在編輯的檔案。
怎樣使用 screen 的 shelltitle 功能
Screen 可以利用 shelltitle 功能,只要畫面上出現規定的 pattern,就可以抓取其中指定的區域,當作 shell window 的 title。Screen 的 manpage 是這麼說的:
TITLES (naming windows)
You can customize each window's name in the window display (viewed with
the "windows" command [...]