標籤:su進階用法
有時間,我們需要在開機的時候執行一些特定的程式或都指令碼,因為涉及到安全主面的問題,所以又不想用root來執行,那怎樣辦呢。
經過查看 su 的協助提示,發現:
[[email protected] ~]# su --helpUsage: su [OPTION]... [-] [USER [ARG]...]Change the effective user id and group id to that of USER. -, -l, --login make the shell a login shell -c, --command=COMMAND pass a single COMMAND to the shell with -c --session-command=COMMAND pass a single COMMAND to the shell with -c and do not create a new session -f, --fast pass -f to the shell (for csh or tcsh) -m, --preserve-environment do not reset environment variables -p same as -m -s, --shell=SHELL run SHELL if /etc/shells allows it --help display this help and exit --version output version information and exitA mere - implies -l. If USER not given, assume root.Report su bugs to [email protected]GNU coreutils home page: <http://www.gnu.org/software/coreutils/>General help using GNU software: <http://www.gnu.org/gethelp/>For complete documentation, run: info coreutils ‘su invocation‘
發現其中重要的一行: "-c 執行單行命令"
哈哈,突破點。 馬上到命行裡試一下:
[[email protected] ~]# su - admin -c "id"uid=500(admin) gid=500(admin) groups=500(admin)[[email protected] ~]#輸出的時 admin 在執行命令 "id" 顯示的結果 ,而且執行後並沒有切換到"admin" 的console 下 .
#################### 找到方法了 ################
所以,在開機的時候在 /etc/rc.lcal 裡面添加一句命令就可以實現:開機時,使用一個普通使用者來幫我們做某些操作了 ....
[[email protected] ~]$ cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
#input your command
su - admin -c "xxxxxx" //最好使用絕對路徑
#注: /etc/rc.local 是在所在的service 都啟動後,才會執行的.
本文出自 “海無涯” 部落格,請務必保留此出處http://plong.blog.51cto.com/3217127/1580005
linux 下使用指定的使用者來執行命令