6.10 Remote Access: ssh, scp, rsync and Environment Variables
ssh -i ~/keys/mykey.pem ec2-user@203.0.113.10 # log in
ssh -i ~/keys/mykey.pem ubuntu@203.0.113.10 'uptime' # run one command remotely
scp -i ~/keys/mykey.pem index.html ec2-user@203.0.113.10:/home/ec2-user/ # upload file
scp -i ~/keys/mykey.pem -r ./site ubuntu@203.0.113.10:~/ # upload folder
scp -i ~/keys/mykey.pem ec2-user@203.0.113.10:/var/log/nginx/error.log . # download
rsync -avz -e "ssh -i ~/keys/mykey.pem" ./site/ ubuntu@203.0.113.10:~/site/ # smart sync
Save typing with an SSH config file ~/.ssh/config:
Host web1
HostName 203.0.113.10
User ec2-user
IdentityFile ~/keys/mykey.pem
Now just type ssh web1 or scp file.txt web1:~/.
echo $HOME $USER $PATH $SHELL
env | sort # all environment variables
export APP_ENV=production # set for this session and child processes
echo $APP_ENV
unset APP_ENV
echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc # make permanent for your user
source ~/.bashrc # reload without logging out
Keep secrets out of code
Store database passwords in environment variables or a .env file with permission 600, never hard-coded in files that go to GitHub.
Why this matters for security
Environment variables and .env files often hold database passwords and API keys. A web server that serves .env or a Git repo that contains it leaks everything. Keep secrets outside the web root with permission 600, and on AWS prefer IAM roles and Secrets Manager (Part 11).
Ravindra Bagale's Tip
Many students forget the colon (:) in scp – if you write scp file ec2-user@IP, the file doesn't go to the remote machine; a file called "ec2-user@IP" is created on your own laptop! Always write the path with the colon, like user@IP:~/.
Ravindra Bagale's Tip – मराठी
scp मध्ये बरेच students colon (:) विसरतात – scp file ec2-user@IP लिहिलं तर file remote ला जात नाही, तुमच्याच laptop वर "ec2-user@IP" नावाची file बनते! नेहमी user@IP:~/ असा colon सोबत path लिहा.
Ravindra Bagale's Tip – हिंदी
scp में बहुत से students colon (:) भूल जाते हैं – scp file ec2-user@IP लिखा तो file remote पर नहीं जाती, तुम्हारे अपने laptop पर "ec2-user@IP" नाम की file बन जाती है! हमेशा user@IP:~/ जैसा colon के साथ path लिखो.
Practice task
Create ~/.ssh/config with a web1 entry for your instance. Upload a folder with scp -r, download a log file back, and run uptime remotely with one ssh command.
Extended teaching notes
Why this lesson matters
Hardening is deleting unnecessary risk: fewer packages, fewer open ports, fewer privileged accounts, faster patches, better logs.
Hardening related services
[('SSH', 'Key auth, no root login, Fail2ban or equivalent, SG limited to your IP/VPN.', '', ''), ('SMB / NFS / LDAP / SNMP', 'Do not expose to the internet; patch; strong auth; disable unused versions (e.g. SMBv1).', '', ''), ('SMTP', 'Authenticated relay only; SPF/DKIM/DMARC for your domains.', '', ''), ('DNS', 'Restrict zone transfers; monitor for unexpected records (crt.sh for certs).', '', ''), ('Firewalls', 'Host firewall + cloud SG/NSG; default deny; document every allow.', '', '')]
Real examples
Global: CIS Benchmarks exist for Linux, Windows and clouds. Indian company (sample): a Pune team requires SSH keys only and weekly patch windows for internet-facing hosts.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.