How to Restore Default .bashrc File After Accidental Overwrite in CentOS 6.5


2 views

When working with Linux systems, especially when configuring shell environments, it's easy to make mistakes with redirection operators. The difference between > (overwrite) and >> (append) can be crucial.

In this case, the command:

echo 'export EDITOR=/usr/bin/nano' > /etc/bashrc

instead of:

echo 'export EDITOR=/usr/bin/nano' >> /etc/bashrc

has completely replaced the default .bashrc content with a single line.

On CentOS 6.5, the default .bashrc isn't stored as a separate file in the distribution. Instead, it's generated during package installation. Here's how to restore it:

The cleanest solution is to reinstall the bash package:

sudo yum reinstall bash

This will restore all bash-related configuration files to their default state.

If you just want to restore the user-specific .bashrc:

cp /etc/skel/.bashrc ~/
source ~/.bashrc

If you prefer manual restoration, here's a typical CentOS 6.5 default .bashrc content:

# .bashrc

# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

Best practices to avoid similar problems:

  • Always back up before modifying critical files: cp ~/.bashrc ~/.bashrc.bak
  • Use version control for dotfiles
  • Consider using set -o noclobber to prevent accidental overwrites

If you need immediate shell access while fixing the issue:

bash --noprofile --norc

This starts bash without loading any initialization files.


Many Linux administrators have faced this situation - accidentally overwriting critical configuration files with a single misplaced redirection operator. In CentOS 6.5, the /etc/bashrc file contains essential shell configurations that affect all users.

Using > instead of >> is particularly destructive:

# Dangerous overwrite (what you did):
echo 'export EDITOR=/usr/bin/nano' > /etc/bashrc

# Safe append (what you intended):
echo 'export EDITOR=/usr/bin/nano' >> /etc/bashrc

For CentOS 6.5 x86_64, you have several recovery paths:

Option 1: Restore from RPM Package

The cleanest method is to reinstall the bash package:

# First verify the package owning bashrc
rpm -qf /etc/bashrc

# Expected output: bash-4.1.2-48.el6.x86_64
# Then reinstall (no need for full removal)
sudo rpm -Uvh --force bash-4.1.2-48.el6.x86_64.rpm

If you don't have the original RPM, download it from CentOS vault:

wget http://vault.centos.org/6.5/os/x86_64/Packages/bash-4.1.2-48.el6.x86_64.rpm

Option 2: Manual Restoration

Here's the default CentOS 6.5 /etc/bashrc content for quick restoration:

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# Prevent doublesourcing
if [ -z "$BASHRCSOURCED" ]; then
  BASHRCSOURCED="Y"

  # By default, we want umask to get set. This sets it for non-login shell.
  # Current threshold for system reserved uid/gids is 200
  # You could check uidgid reservation validity in
  # /usr/share/doc/setup-*/uidgid file
  if [ $UID -gt 199 ] && [ "id -gn" = "id -un" ]; then
    umask 002
  else
    umask 022
  fi

  # are we an interactive shell?
  if [ "$PS1" ]; then
    if [ -z "$PROMPT_COMMAND" ]; then
      case $TERM in
      xterm*|vte*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
      screen*)
        if [ -e /etc/sysconfig/bash-prompt-screen ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
      *)
        [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
        ;;
      esac
    fi
  fi
fi

Consider these best practices to avoid similar issues:

# 1. Always make backups before editing
sudo cp /etc/bashrc /etc/bashrc.bak

# 2. Use visudo-like protection for critical files
function safe_edit() {
  file=$1
  sudo cp "$file" "$file.bak-$(date +%Y%m%d)" \
  && sudo nano "$file"
}

# 3. Implement configuration management
# For frequent changes, use Ansible/Chef/Puppet

After restoring /etc/bashrc, verify it works correctly:

# Start new shell session
bash --norc
# Then source the file manually
source /etc/bashrc
# Check environment variables
env | grep EDITOR