pssh 不能执行指定用户命令

问题:

    一个脚本a.sh(必须root用户执行),在本地可以运行,通过pssh  -h ip_file "cd /home/byte/a.sh"不能执行。

 

原因:

  分析应该是ssh没有获取到root权限,可能原因是操作系统安装时有多个用户且默认远程用户非root。通过查看可知是ssh默认登录用户不是root

  Linux系统设置涉及到两种启动shell的方式一个是本地登录另一个是远程登录,

  本地直接登录shell终端环境调用过程是 ~/.bash_profile->~/.bashrc

  ssh登录时直接调用~/.bashrc,导致只是以普通用户登录,没有获取到root权限

解决办法:

 

1.修改命令,先执行root的.bash_profile来获取root权限

   pssh  -h ip_file "source  /root/.bash_profile;cd /home/byte/a.sh"

2.修改.ssh配置文件

  vi /etc/.ssh/enviroment 添加所需环境变量,格式如下

  VAR1=VALUE1

  VAR2=VALUE2

比如添加 USER=root

 并修改/etc/ssh/sshd_config 文件中的PermitUserEnvironment=yes(会带来安全问题,具体请看备注1链接)

下次登录就直接获取root权限了

 

备注:

1. 安全问题 

PermitUserEnvironment
             指定是否允许 sshd(8) 处理 ~/.ssh/environment 以及 ~/.ssh/authorized_keys 中的 environment= 选项。
 
默认值是"no"。如果设为"yes"可能会导致用户有机会使用某些机制(比如 LD_PRELOAD)绕过访问控

参考连接 http://www.2cto.com/os/201401/272738.html
http://serverfault.com/questions/527638/security-risks-of-permituserenvironment-in-ssh
2.相关文件解释
/bin/bash The bash executable /etc/profile The systemwide initialization file, executed for login shells ~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file ~/.bash_logout The individual login shell cleanup file, executed when a login shell exits ~/.inputrc Individual readline initialization file

参考:http://serverfault.com/questions/527638/security-risks-of-permituserenvironment-in-ssh

       http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment

posted @ 2016-12-12 13:15  iByte  阅读(2008)  评论(0编辑  收藏  举报