>_ IzzyLab

Hands-on task

This task is step 6 of the course “Linux server security”

Remove the secret from the code

medium~10 min

The deploy script /opt/app/run.sh has a hardcoded database password — it's surely already in the git history, but you have to start somewhere. Move the secret into /etc/app/env with 600 permissions, make the script read it from there, and the password string must no longer appear in run.sh. The script must keep working.

What you'll practise:
  • security
  • secrets
  • chmod
  • source
  • environment variables

Topic breakdown

A hardcoded password in a script is a classic leak: the secret spreads through git history, backups, and to anyone who can read the script. The right pattern is to move the value into a separate file with tight permissions (600, owner-only access — set via chmod, checked via stat) and pull it in at runtime. Loading variables from a file into the current shell is done with the built-in source command (or its short form, a dot), so the variable appears exactly where the hardcode used to be, and the password line disappears from the script.

What's checked

  1. password removed from run.sh
  2. password moved into /etc/app/env
  3. /etc/app/env permissions are 600
  4. run.sh still works
  5. run.sh takes the password from /etc/app/env

What it looks like