I regularly log into different Linux machines on the cloud and find complicated to identify on which machine I am by looking at the bash prompt. In fact, in cloud environments the bash prompt tends to show an ugly internal IP address which I can never memorize. Plus, I always fear to log into the production environment by mistake and run experimental commands intended to be executed in the development environment.
In order to overcome my fears, I put together few lines of bash code to be added at the beginning of ~/.bashrc:
# get public IP address
aliasmyip="curl -s http://whatismyip.akamai.com"
# display environment in bash prompt
18.104.22.1687)PS1="[\u@\h DEV \W]\$ "
22.214.171.1248)PS1="[\u@\h STAGE \W]\$ "
126.96.36.1999)PS1="[\u@\h LIVE \W]\$ "
*)PS1="[\u@\h OTHER \W]\$ "
Line 2 retrieves the IP address from an external web service and map it to myip. Lines 5-14 modify the bash prompt (PS1) based on the value of myip and each IP address (e.g. 188.8.131.527) is mapped to a string appearing in the prompt (e.g. DEV).
While using cloud services like AWS it sometimes occurs to transfer files from a local machine to a remote server which is only accessible via SSH keys. Linux utilities like rsync and scp come to rescue in this case. Here is an example of how to transfer files via rsync.
First, make sure that the destination folder on the remote server is writeable. If not, log into the remote server and change the permissions, e.g.:
Then go back to the local machine and transfer the file to the remote machine:
where your_ssh_key.pem is the SSH private key to access the remote server, file.zip is the file to transfer, ec2-user is the user on the remote server, 184.108.40.2067 is the IP address of the remote server, and /home/ec2-user/destination_folder is the folder on the remote server where you want to transfer the file to.
rsync is a truly file synchronization tool and comes with lots of options, look at its man page for more examples.