问题

最近在学习 ansible ,在使用普通用户远程执行 ip a 命令是发现提示错误:/bin/sh: ip: command not found。

原因

command not found 命令未找到,一般想到的是环境变量的问题。网上查找资料,也证实了这个猜测,根本原因是 ansible 登录方式为 non-login shell(与之对应的是 login shell )。login shell 登陆后会加载 /etc/profile、~/.bash_profile,而 non-login shell 登陆后不会加载 /etc/profile、~/.bash_profile,而是加载 /etc/bashrc、~/.bashrc,一般的环境变量都设置在 /etc/profile、~/.bash_profile,所以这时就出现了找不到命令的情况。

  • login shell:取得 shell 需要完整的登录流程,就是说通过输入账号和密码登录系统。
  • non-login shell:取得 shell 不需要重复登录的举动。比如 X Window 登录 linux 后,再以 X 的图形界面启动终端机,此时那个终端机并没有需要输入账号和密码或者在原本的 shell 环境下再次执行 sh 命令,同样也没有输入账号密码就进入新的 shell环境(前一个 shell 的子进程)。

解决方案

  1. 把 /etc/profile、~/.bash_profile中的环境变量同步一份到 /etc/bashrc、~/.bashrc,这种方案不推荐。
  2. 执行命令或脚本时命令都写全路径,我一般采用这种方案。
  3. 执行命令和脚本时前面首先加载环境变量,比如: . /etc/profile &> /dev/null; . ~/.bash_profile &> /dev/null; ip a;

ansible 远程执行时提示 command not found 问题的更多相关文章

  1. Windows 2008 远程登陆时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限"

    ECS Win2008 远程时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限"的解决方法   问题描述 ECS Windows 2008 远程登陆时提示&quo ...

  2. Ansible 远程执行命令

    写法如下: [root@localhost ~]$ ansible 192.168.119.134 -m command -a 'date' # 对指定的主机远程执行命令,-m 指定使用哪个模块,-a ...

  3. Ansible 远程执行脚本

    1. 先在服务端创建一个 shell 脚本 [root@localhost ~]$ cat /tmp/test.sh #!/bin/bash echo "hello world" ...

  4. 执行脚本 提示 command not found

    问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...

  5. 解决安装vc2005运行库时提示Command line option syntax error.Type Command/?for Help

    安装vc2005运行库时提示 这是因为它要自解压到用户的临时文件夹下,如果用户名中带中文,就会报错. 简单的解决方法是,手动解压之,再安装 当然,你也可以修改用户名或者再新建个用户.

  6. 将window的shell脚本通过ftp传输到Linux服务器后, shell脚本中执行时提示“没有那个文件或目录”的解决办法

    出现bad interpreter:No such file or directory的原因,是文件格式的问题.这个文件是在Windows下编写的.换行的方式与Unix不一样,但是在vim下面如果不S ...

  7. 使用sudo执行命令提示command not found

    笔记: 使用源码部署nginx的时候,使用sudo nginx提示command not found,但是直接使用nginx会导致权限问题: 这种情况应该是环境变量导致的,使用 env |grep P ...

  8. 安装SQL Servre2000时提示“command line option syntax error! type command /? for help”

    问题: 当程序正在安装ms数据访问组件时,弹出错误提示框:command line option syntax error,type command/? for help,点击确定继续:到了程序正在安 ...

  9. paramiko模拟ansible远程执行命令

    主模块 #!/usr/bin/env python from multiprocessing import Process import paramiko import time import sys ...

随机推荐

  1. inline-block,inline,block,table-cell,float

    float:left ---------------------------------------------------------------------------------------- ...

  2. hbase之RPC详解

    Hbase的RPC主要由HBaseRPC.RpcEngine.HBaseClient.HBaseServer.VersionedProtocol 5个概念组成. 1.HBaseRPC是hbase RP ...

  3. vue中修改了数据但视图无法更新的情况(转)

    原文地址:https://blog.csdn.net/qq_39985511/article/details/79778806

  4. centOS发布.Net Core 2.0 API

    1.dotnet  xxx.dll & & 放在启动参数后面表示设置此进程为后台进程.(目前测试无效) 2.ps -ef | grep xxx ps:将某个进程显示出来 -A 显示所有 ...

  5. Flink分布式缓存Distributed Cache

    1 分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程 ...

  6. W3C、MDN及html常用标签介绍

    W3C 万维网(World Wide Web)是作为欧洲核子研究组织的一个项目发展起来的,在那里 Tim Berners-Lee 开发出万维网的雏形.Tim Berners-Lee- 万维网的发明人和 ...

  7. C/C++语言for循环语句执行顺序

    for循环如下: ; i<; ++i) { } 执行顺序如下: 1.i=0  初始化初值 2.i<10 进行判断,如果条件为真,则继续执行 3.执行循环体代码 4.i++ 变量i自增 5. ...

  8. asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密。

    原文:asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密. GitHub demo https://github.com/zhanglilong23/Asp.NetCore. ...

  9. git update-index --assume-unchanged on directory 转摘自:http://stackoverflow.com/questions/12288212/git-update-index-assume-unchanged-on-directory

    30down votefavorite 16 git 1.7.12 I want to mark all files below a given directory as assume-unchang ...

  10. linux下有趣的工具

    1.toilet(在CentoOS7 安装) yum install -y https://raw.githubusercontent.com/sliqua-hosting/repo/master/c ...