---
title: ".bashrc and .bash_profile: what Bash loads, and when · CloudSat"
description: "What .bashrc, .bash_profile and .profile do, which one Bash reads for SSH logins, sudo, scripts and scp, safe settings for a server, and how to debug them."
url: https://cloudsat.io/learn/bashrc-and-bash-profile-explained
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Linux](https://cloudsat.io/learn/topic/linux)

# .bashrc and .bash\_profile: what Bash loads, and when

What .bashrc, .bash\_profile and .profile do, which one Bash reads for SSH logins, sudo, scripts and scp, safe settings for a server, and how to debug them.

CloudSat team November 18, 2026 7 min read

Bash reads different startup files depending on how it was started. An SSH login, a new terminal tab, `sudo -i`, a cron job and an `scp` transfer each take a different path, and most puzzles such as "my alias works here but not there" or "scp broke after I edited my config" come from that. This guide maps which file runs when, what belongs in each, a sensible set of server settings, and how to test changes without locking yourself out.

## Which file runs when

| How Bash starts | Examples | What it reads |
| --- | --- | --- |
| Interactive login shell | `ssh user@host`, `su -`, `sudo -i`, a console login | `/etc/profile` and `/etc/profile.d/*.sh`, then the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile` |
| Interactive non-login shell | A new terminal tab on a desktop, `bash` typed at a prompt, `su` without the dash | `/etc/bash.bashrc` on Debian and Ubuntu, then `~/.bashrc` |
| Non-interactive shell | `bash script.sh`, cron jobs, systemd services | Nothing, except a file named in `$BASH_ENV` |
| A command run over SSH | `ssh host uptime`, `scp`, `rsync`, `sftp` | `~/.bashrc` on many systems |

Two details make this table work in practice. First, distributions chain the files: Ubuntu's `~/.profile` loads `~/.bashrc` when the shell is Bash, and on Fedora and RHEL `~/.bash_profile` does the same, while `~/.bashrc` loads `/etc/bashrc`. So an SSH login ends up reading both kinds of file. Second, Bash's manual says that when it detects it was started by the SSH daemon to run a command, it reads `~/.bashrc` even though the shell is not interactive. That is why a line in `.bashrc` can break file transfers.

A login shell also reads `~/.bash_logout` when it exits, which is the place for clearing the screen or a temporary directory.

### Root, sudo and su

Becoming root takes several forms, and each loads something different:

- `sudo command` runs one program as root and reads no startup files at all.
- `sudo -i` and `su -` start a login shell for root, with a clean environment, so root's profile files and `/root/.bashrc` apply.
- `sudo -s` and `su` without the dash start a non-login shell. Root's `.bashrc` applies on default configurations, but the profile files do not, and `su` keeps much of your own environment.

When a setting works for you but not after you switch to root, check which of these you used before you edit anything.

### Where new accounts get their files

`useradd -m` and `adduser` copy the files in `/etc/skel` into each new home directory. Put a team-wide `.bashrc` there and every account created afterwards starts with it; existing accounts keep the copies they already have.

## What belongs where

- **`~/.bashrc`**: everything for interactive use. Aliases, functions, the prompt, history settings and completion.
- **`~/.profile` or `~/.bash_profile`**: environment variables that programs inherit, such as `PATH`, `EDITOR` and `LANG`. Setting them here once per login avoids growing `PATH` every time `.bashrc` is reloaded.
- **`/etc/profile.d/*.sh`**: settings for every user's login shell, owned by root.
- **Neither, for services.** systemd units and cron jobs do not read these files. Give a service its variables with `Environment=` or `EnvironmentFile=` in the unit, and put them at the top of a crontab or inside the script for cron.

One trap catches Ubuntu users: creating a `~/.bash_profile` means Bash stops reading `~/.profile`, and with it the lines that load `~/.bashrc`. If you create one, start it with `[ -f ~/.profile ] && . ~/.profile`.

## A sensible .bashrc for a server

Keep the distribution's default file and add your settings below it. Ubuntu and Debian's default already begins with a guard that stops non-interactive shells early. Make sure yours has it before anything that prints output:

```bash
# If not running interactively, stop here
case $- in
  *i*) ;;
  *) return ;;
esac
```

Then the additions:

```bash
# History: larger, timestamped, written as you go
HISTSIZE=50000
HISTFILESIZE=100000
HISTCONTROL=ignoreboth            # skip duplicates and lines starting with a space
HISTTIMEFORMAT='%F %T '
shopt -s histappend
PROMPT_COMMAND="history -a${PROMPT_COMMAND:+; $PROMPT_COMMAND}"

# A prompt that names the machine; red when you are root
if [ "$EUID" -eq 0 ]; then
  PS1='\[\e[1;31m\]\u@\h\[\e[0m\]:\w\$ '
else
  PS1='\[\e[1;32m\]\u@\h\[\e[0m\]:\w\$ '
fi

# Shortcuts that do not change how commands behave
alias ll='ls -alF'
alias ports='ss -tulpn'
alias dfh='df -h -x tmpfs -x devtmpfs'
alias logs='journalctl -e -u'     # usage: logs nginx

mkcd() { mkdir -p -- "$1" && cd -- "$1"; }

# Per-machine extras, kept out of version control
[ -f ~/.bashrc.local ] && . ~/.bashrc.local
```

The hostname in the prompt is the cheapest protection against running a command on the wrong server, and the timestamped history tells you what was done, and when, after an incident. Debian and Ubuntu's default file also loads `~/.bash_aliases` if it exists, and Fedora and RHEL's loads every file in `~/.bashrc.d/`, which keeps your additions separate from the distribution's.

## Mistakes that break things

- **Output in non-interactive shells.** An `echo`, a banner or a `fortune` that runs for `ssh host command` corrupts file transfers: rsync stops with "protocol version mismatch -- is your shell clean?" and sftp reports "Received message too long". Keep output below the interactive guard.
- **Aliases in scripts.** Non-interactive shells do not expand aliases, so a script that relies on `ll` fails. Write full commands or functions in scripts.
- **Safety aliases you start to rely on.** RHEL's root `.bashrc` defines `rm -i`, `cp -i` and `mv -i`; most other systems do not. Habits built on them become dangerous on the next server.
- **A broken file and no way back in.** A syntax error or a stray `exit` can end every new session at once. Keep a second SSH session open while you edit, and test before you log out.
- **Slow logins.** Version managers and environment tools that initialize on every shell can add seconds. Measure, then load them lazily or only where needed.

## Test and debug safely

```bash
bash -n ~/.bashrc                                               # syntax check, runs nothing
time bash -i -c exit                                            # startup time of an interactive shell
PS4='+ ${BASH_SOURCE}:${LINENO}: ' bash -xic exit 2>&1 | less   # every line run at startup
source ~/.bashrc                                                # apply to the current shell
```

From your own computer, check that non-interactive sessions stay silent: `ssh deploy@203.0.113.10 true | wc -c` must print `0`. Anything else is output that will break `scp`, `sftp` or `rsync`.

## Share your settings across servers

Keep your dotfiles in a Git repository and link them into place, or copy one `~/.bashrc.local` to each machine:

```bash
git clone https://git.example.com/you/dotfiles.git ~/.dotfiles
ln -sf ~/.dotfiles/bashrc.local ~/.bashrc.local
```

On CloudSat, a [startup script](https://cloudsat.io/docs/startup-scripts) can write that file on every new server at first boot, so each machine starts with the same prompt and history settings. Keep secrets out of these files: anything in `.bashrc` is readable by whoever can read your home directory, and it ends up in every copy of the repository.

For the scripting side of Bash, see [functions, arguments and scope](https://cloudsat.io/learn/bash-functions-arguments-return-scope) and [conditionals with if, test and case](https://cloudsat.io/learn/bash-conditionals-if-test-case).

## FAQ

### Why does my alias not work after sudo?

Bash expands aliases only in the first word of a command, and there the first word is `sudo`. Add `alias sudo='sudo '` with a trailing space, which tells Bash to check the next word for an alias too.

### Should PATH go in .bashrc or .profile?

In `~/.profile` (or `~/.bash_profile`), so it is set once per login and inherited by everything started from it. Appending to `PATH` in `.bashrc` adds a duplicate entry each time the file is reloaded.

### Where is the system-wide bashrc?

`/etc/bash.bashrc` on Debian and Ubuntu, `/etc/bashrc` on Fedora, RHEL and their rebuilds. Login-only settings for all users go in `/etc/profile` or a file under `/etc/profile.d/`.

### Why do my changes not show up?

Startup files only run when a shell starts. Run `source ~/.bashrc` in the current session, or open a new one, and check the table above to make sure the file you edited is one that kind of shell reads.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Linux](https://cloudsat.io/learn/topic/linux)

- [Bash functions with arguments, return values and scope](https://cloudsat.io/learn/bash-functions-arguments-return-scope): Linux Bash functions with arguments, return values and scope 8 min read
- [Bash if statements with [ ], [[ ]], (( )) and case](https://cloudsat.io/learn/bash-conditionals-if-test-case): Linux Bash if statements with [ ], [[ ]], (( )) and case 8 min read
- [Linux commands you need on a new server](https://cloudsat.io/learn/essential-linux-commands-for-servers): Linux Linux commands you need on a new server 8 min read
- [Find what is filling your Linux disk with df, du and ncdu](https://cloudsat.io/learn/find-what-is-filling-your-linux-disk): Linux Find what is filling your Linux disk with df, du and ncdu 7 min read
