Ravindra BagaleCourses & study guides

6. Linux Advanced Commands

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

scp madhe khup students colon (:) visartat – scp file ec2-user@IP lihila tar file remote la nahi, tumchyach laptop var "ec2-user@IP" navachi file banate! Nehmi user@IP:~/ asa colon sobat path lihaa.

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.