歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 下使用指定的用戶來執行命令

Linux 下使用指定的用戶來執行命令

日期:2017/2/28 14:25:41   编辑:Linux教程

有時候,我們需要在Linux開機的時候執行一些特定的程序或都腳本,因為涉及到安全主面的問題,所以又不想用root來執行,那怎樣辦呢。

經過查看 su 的幫助提示,發現:

[root@RedHat6 ~]# su --help

Usage: 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 exit

A 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 執行單行命令"

哈哈,突破點。 馬上到命行裡試一下:

[root@redhat6 ~]# su - admin -c "id"

uid=500(admin) gid=500(admin) groups=500(admin)

[root@redhat6 ~]#

輸出的時 admin 在執行命令 "id" 顯示的結果 ,而且執行後並沒有切換到"admin" 的console 下 .

#################### 找到方法了 ################

所以,在開機的時候在 /etc/rc.lcal 裡面添加一句命令就可以實現:開機時,使用一個普通用戶來幫我們做某些操作了 ....

[root@redhat6 ~]$ 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 都啟動後,才會執行的.

Copyright © Linux教程網 All Rights Reserved