Debian11使用su命令后command not found问题解决方法[转]
首页 > IT > Debian   作者:eehello  2023年1月1日 16:10 星期日  浏览:374  字号:   评论:0 条
时间:2023-1-1 16:10   浏览:374  评论:0 条 

转自:https://yangyq.net/2022/06/debian-su-command-not-found.html

https://cloud.tencent.com/developer/article/1985786



在很多情况下,openssh是不允许root进行连接的,那么就需要用一个普通用户先登录,然后在用su命令来模拟root进行操作。

但是使用这个命令后,成功转为root用户后,很多命令无法找到,提示command not found,例如:

bash: dpkg-reconfigure: command not found

这是因为su命令用法不对,应该是:

su -


注意后面有一个连接线。加上后面这个符号相当于使用root账户登录,会初始化对应的环境变量,也就是加载上/usr/sbin/目录,但是如果不加这个符号,就相当于临时转为root用户,无法加载root的环境变量。


第二种解决方法:

造成这个问题的原因是没有把 /sbin 的目录路径(系统用户目录)添加到$PATH变量中

#查看当前$PATH root@debian:/home/user# echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games#查看当前$PATH root@debian:/home/user# echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

#查看当前$PATH
root@debian:/home/user# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

如果没有 /sbin 可以使用以下命令

root@debian:/home/user# export PATH=$PATH:/sbin/
root@debian:/home/user#

临时把对应路劲加到$PATH变量中,测试一下是否可用。 但是这只是临时添加,断开ssh重连就失效了,需要永久添加需要编辑 .bashrc 文件(每个用户都有.bashrc 文件,想要每个用户都能正常使用,需要去编辑每个用户的 .bashrc 文件)

#切换到root目录
root@debian:/home/user# cd ~
root@debian:~# 
#编辑文件
root@debian:~# vim .bashrc

在最后一行添加

export PATH=$PATH:/usr/sbin
# ~/.bashrc: executed by bash(1) for non-login shells. 
 
# Note: PS1 and umask are already set in /etc/profile. You should not 
# need this unless you want different defaults for root. 
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ ' 
# umask 022 
 
# You may uncomment the following lines if you want `ls' to be colorized: 
# export LS_OPTIONS='--color=auto' 
# eval "`dircolors`" 
# alias ls='ls $LS_OPTIONS' 
# alias ll='ls $LS_OPTIONS -l' 
# alias l='ls $LS_OPTIONS -lA' 
# 
# Some more alias to avoid making mistakes: 
# alias rm='rm -i' 
# alias cp='cp -i' 
# alias mv='mv -i' 
export PATH=$PATH:/usr/sbin


保存后,执行一下

root@debian:~# source ~/.bashrc
root@debian:~#

然后在重新查看一下$PATH变量

root@debian:~# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/sbin

可以看到,多了个/usr/sbin

现在就可以正常的使用reboot、service等命令了。


 您阅读这篇文章共花了: 
 本文没有标签!
二维码加载中...
本文作者:eehello      文章标题: Debian11使用su命令后command not found问题解决方法[转]
本文地址:https://www.xxnet.eu.org/?post=313
版权声明:若无注明,本文皆为“点滴记忆---观雨亭”原创,转载请保留文章出处。

返回顶部| 首页| 手气不错| 留言板|后花园

Copyright © 2014-2023 点滴记忆---观雨亭