`
Poechant
  • 浏览: 212251 次
博客专栏
Bebe66e7-3a30-3fc9-aeea-cfa3b474b591
Nginx高性能Web服务...
浏览量:23502
5738817b-23a1-3a32-86de-632d7da73b1e
Cumulus实时媒体服务...
浏览量:21411
社区版块
存档分类
最新评论

Linux下执行一些命令前加sudo时出现command not found的原因

 
阅读更多

转载请注明来自“柳大的CSDN博客”:http://blog.csdn.net/poechant


当我们用sudo来执行cd、ls等命令时,会出现command not found的提示:

sudo cd /home/michael
sudo: command not found

我们知道在执行Linux命令时,如果在其前面加上sudo,就表示以root权限执行。但是这其实是有一个前提的,就是只有那些Linux内置系统命令才可以用如此的形式来执行,而对于Shell内置命令或其他用户自定义命令、别名等,是不能用sudo来使用root权限的。为什么呢?详细说一下sudo幕后隐藏的过程才能明白。


因为当在Linux下用sudo执行某一命令时,是在原进程(parent process)的基础上fork出来一个子进程(child process),这个子进程是以root权限执行的。然后在子进程中,执行你在sudo后面跟的命令。


在子进程中是无法调用涉及到父进程的状态的一些命令的,所以非系统内置命令会被拒绝。这就是为什么会出现command not found的提示。具体来说,当我们执行:

sudo cd /home/michael

所在这个shell进程中(称其为PP,表示parent process)fork出一个子进程(称其为CP,表示child process),那么在CP中是无法改变PP的所在目录的。

sudo ls /home/michael

sudo后由PP产生了CP,CP是无法获取PP所在的目录的内容的(具体来说,是读取该目录的block data,可详见《柳大的Linux游记·基础篇(2)Linux文件系统的inode》一文)。


转载请注明来自“柳大的CSDN博客”:http://blog.csdn.net/poechant

-

分享到:
评论

相关推荐

    squashfs1.3r3.tar.gz

    SQUASHFS 1.3r3 - A squashed read-only filesystem for Linux Copyright 2004 Phillip Lougher (phillip@lougher.demon.co.uk) Released under the GPL licence (version 2 or later). Squashfs is currently ...

    linux运维常用命令

    linux运维常用命令,个人整理的常用运维命令,适用于linux新手

    Linux系统使用Fuser命令的方法

    -bash: fuser: command not found 请执行如下命令安装: [winbert@winbert-server ~]$ sudo yum -y install psmisc 如何使用fuser命令? man命令可用于查看任何命令的帮助手册,但是学习新知识(尤其是linu

    ubuntu linux设置静态ip

    第一步、执行su,输入root密码进入特权模式。 第二步、添加DNS服务器IP。 第三步、重启网络。 第四步、验证。

    linux下安装easy_install的方法

    如果想使用easy_install工具,可能需要先安装setuptools,不过更酷的方法是使用ez_setup.py脚本:复制代码 代码...比如说要安装Python的MySQL支持,可以执行如下命令,系统会自动在pypi网站列表里查找相关软件包: 复

    安装docker和docker-compose实例详解

    2.执行以下命令安装依赖包 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 3.鉴于国内网络问题,强烈建议使用国内源执行下面的命令添加 yum 软件源 sudo yum-config-manager --add-repo ...

    libforensic1394-0.2.tar.gz

    Instructions for loading the new stack can be found on the Linux IEEE 1394 FireWire wiki: https://ieee1394.wiki.kernel.org/index.php/Juju_Migration In addition the page also contains ...

    t:Twitter的命令行强大工具

    ruby: command not found 在Linux上,对于基于Debian的系统,打开一个终端并输入: sudo apt-get install ruby-dev 或对于基于Red Hat的发行版(如Fedora和CentOS),键入: sudo yum install ruby-devel (如有...

    vm安装macos补丁_unlock-all-v111

    error is shown during patching as the files are not found. This can be safely ignored. In all cases make sure VMware is not running, and any background guests have been shutdown. 2. Windows ------...

    VMware Unlocker for OS X 1.1.0

    error is shown during patching as the files are not found. This can be safely ignored. In all cases make sure VMware is not running, and any background guests have been shutdown. 2. Windows --------...

    vmware8 mac补丁

    error is shown during patching as the files are not found. This can be safely ignored. In all cases make sure VMware is not running, and any background guests have been shutdown. 2. Windows ------...

    tensorflow1.12支持cuda10

    自编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3;...hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl

    tensorflow-1.12支持cuda10.0

    自编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3;...hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl

    cpp checker

    CFGDIR=cfg : Specify folder where .cfg files are found HAVE_RULES=yes : Enable rules (pcre is required if this is used) g++ (for experts) ================= If you just want to build Cppcheck ...

Global site tag (gtag.js) - Google Analytics