您的位置:首页 > 运维架构 > Shell

关于丢失.bansh_profile配置文件造成-bansh-4.2#问题 推荐

2017-04-13 16:43 489 查看
首先是描述一下问题的产生过程吧:不小心cp了很多的文件到/root/下面去了,去/root/下执行ll发现好多好多文件,这样对我要查找需要的文件来说实在是太麻烦了,没有一目了然的感觉了,于是我rm –rf /root/ 和rm –f /root/将/root/目录下的所有文件都删除了,当时以为这样式正常的删除,没有什么副作用。但是,以后问题就来了,发现只要su到root用户下面去就会出现-bash-4.2#开头的命令行,以前的是[root@localhost~]#,这肯定有问题啊,虽然后面的命令不会受影响,但是前面的路径看不到了,这是很难受的!
于是百度,发现了原来是因为/root/下面的隐藏文件“.bash_profile”文件丢掉了,到这儿才发现是删除/root/下的文件的时候,是全部删掉了的,没有注意到隐藏文件。到了这儿问题就明显了,好了,接下来就是修复这个问题了!但是在修复前有个问题就是网上一些说直接从普通用户家目录下面复制.bash_profile文件到/root/目录下面就可以了,但是测试后不可以,原先很简单,就是两个文件不一样。我们先来看看普通用户user1和user2 下的.bash_profile文件是不是一样的:
[root@localhost ~]# vimdiff/home/user1/.bash_profile /home/user2/.bash_profile





结果发现普通用户之间是相同的,那么我们再看看root用户和普通用户之间是否也一样呢?
[root@localhost ~]# vimdiff  .bash_profile /home/user1/.bash_profile




这是截图,看出不同了吧,所以要从普通用户复制.bash_profile过来,还要修改一点文件的,就是将红色区域删除,就是删除“.local/bin:$HOME/”就可以了。

好了,到这儿了,原理文件都说的差不多了,接下来就是模拟出错环境和恢复过程:
首先在没有删除/root/下隐藏文件的时候去/root/下面ls –al | grep “.bash_profile”一下,看看有没有.bash_profile
[root@localhost ~]# ls -al | grep".bash_profile"
-rw-r--r--. 1 root root   176 Apr 12 16:18.bash_profile
-rw-r--r--. 1 root root 12288 Apr 12 12:41 .bash_profile.swp
结果发现是有“.bash_profile”这个文件的!,接下来我们把它删了
[root@localhost ~]# rm -f .bash_profile
[root@localhost ~]# ls -al | grep".bash_profile"
-rw-r--r--. 1 root root 12288 Apr 12 12:41 .bash_profile.swp
看出确实是删掉了,到这儿就是模拟了丢失.bash_profile文件环境,接下来我们就看看丢了这个文件的后果:
[user1@localhost ~]$ su -
Password:
Last login: Wed Apr 12 16:18:58 CST 2017 onpts/0
-bash-4.2# ls
anaconda-ks.cfg  initial-setup-ks.cfg
-bash-4.2# pwd
/root
-bash-4.2#
结果是不是很怪,切换到root用户的时候,竟然不是[root@localhost ~]#,而是-bash_4.2#,这样我们一眼看不出当前的工作目录,很不舒服,接下来就是去恢复这个.bash_profile的文件了,如果之前有备份/root/下的.bash_profile文件,就好办了,直接cp到/root/下就可以了,但是之前是直接删掉了的,没有备份,没有原件了。那就到普通用户下面去复制修改一份.bash_profile文件到/root/文件下去
先复制文件到root家目录中去:
-bash-4.2# cp  /home/user1/.bash_profile  ./
-bash-4.2# ls -al | grep".bash_profile"
-rw-r--r--. 1 root root   193 Apr 12 19:09.bash_profile
修改.bash_profile文件:
-bash-4.2# cat ./.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
.~/.bashrc
fi

# User specific environment and startupprograms

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
-bash-4.2# vim ./.bash_profile
-bash-4.2# cat ./.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
.~/.bashrc
fi

# User specific environment and startupprograms

PATH=$PATH:$HOME/bin

export PATH
-bash-4.2#
好了,修改成功了;我们su – 刷新一下!
-bash-4.2# su -
Last login: Wed Apr 12 19:09:03 CST 2017 onpts/0
[root@localhost ~]#
O(∩_∩)O~,发现又恢复了,到这儿就彻底解决了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  问题 bash