ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

简记修复改etc下profile失败的补救措施

2026/8/3 11:11:33 拓冰建站 浏览量
简记修复改etc下profile失败的补救措施

现象

下午配置环境变量一个小小的失误,把etc文件夹下的profile改崩了,导致很多基本命令都用不了,服务器出现了下面这种情况。

[root@xxxx ~]# vi /etc/profile
-bash: vi: command not found
[root@xxxxx~]# vi
-bash: vi: command not found
[root@xxxx ~]# ls
-bash: ls: command not found
[root@xxxx ~]# /bin/vi /etc/profile
[root@xxxx ~]# source /etc/profile
-bash: id: command not found
-bash: [: =: unary operator expected
[root@xxxx ~]# ll
-bash: ls: command not found
[root@xxxx ~]# ls
-bash: ls: command not found

可以看到,所有的命令都报找不到非常恐怖。

在这里插入图片描述

补救方式

强制还原profile

查阅网上资料笔者找到一个非常干脆的方式,首先我们强制使用死路径的方式执行bash的vim命令,将错误的环境变量配置删除后,键入wq退出。

/bin/vim /etc/profile

强制配置path

完成上述步骤之后,bash命令还是不能使用,所以我们还需要强行配置一下PATH变量。键入下面这段命令重置一下path,这里补充说明一下我们把profile改错之后导致所有环境变量都读不到,所以我们在命令行直接强写一下PATH,确保bash命令都能用,然后所有基本指令可以用之后,再用source命令即可还原所有。

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

然后键入下面这段命令,自此问题修复,这样以来所有的bash命令都可以用了。

source /etc/profile

参考

Linux – 04 – 修改/etc/profile失败补救方法