问题

最近在学习 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. C# Self Injector into non managed process

    Hey all, I'm gonna explain you how make a self injecting program in C#.I hope you guys thinks its us ...

  2. 【HTTP】http请求url参数包含+号,被解析为空格

    项目技术:Angular 6 问题现象:接口传参的时候,使用 httpClient.post 方法提交数据,字段中包含+号被解析成空格,提交数据错误 解决过程: 1.http请求中包含+号,会被自动解 ...

  3. mysql使用crontab定时备份

    1, 安装crontab yum install vixie-cron yum install crontabs 说明:vixie-cron软件包是cron的主程序:crontabs软件包是用来安装. ...

  4. HTML- 锚点实例

    <!DOCTYPE HTML> <html> <head> <meta charset='utf-8'> <title>锚点实例</t ...

  5. HTML图片

    HTML图片 html图片 <img>标签可以在网页上插入一张图片,它是独立使用的标签,通过“src”属性定义图片的地址,通过“alt”属性定义图片加载失败时显示的文字,以及对搜索引擎和盲 ...

  6. [LeetCode] 196.删除重复的电子邮箱

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...

  7. [fw]How to use DISM to install a hotfix from within Windows

    How to use DISM to install a hotfix from within Windows Jeff Hughes (CORE) 15 Feb 2011 10:10 AM 22 H ...

  8. getopts的错误报告模式

  9. shell选项和参数

  10. linux-java

    查看Java进程耗内存线程 top -Hp pid printf '%x\n' pid ->jid(java thread) 查看time值最大 jstack pid | grep jid 查看 ...