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.