linux 下使用指定的使用者來執行命令

來源:互聯網
上載者:User

標籤: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 下使用指定的使用者來執行命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.