linux 以某个用户执行,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 bug-coreutils@gnu.org

GNU coreutils home page:

General help using GNU software:

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 都启动后,才会执行的.

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值