I am trying to create a shell script that allows me to automatically remote into my AWS EC2 instance from my local machine. I also want this script to log me in as 'root' user and to a directory named '/var/files'. Currently, the script allows me to connect into my EC2 instance but not as 'root' user or into the directory I want.
My question is, how do I pass the "sudo su" and "cd /var/files" to the command line after remoting in? Here is what I have so far but it doesn't pass the $USER and $PATH variables to the command-line.
cd "C:\Operations\sys_admin"
ssh -i "mykeypair.pem" ec2-user@[IP].[location].compute.amazonaws.com
USER='sudo su'
PATH='cd /var/files'
echo $USER
echo $PATH
How about writing these command into the ~/.bashrc
of the ec2-user? You would need to add sudo su
and cd /var/files
as two lines. That would of course mean, that every time you log onto the server you would change to root and cd into the directory...
Otherwise you could use ssh -t
in the following way:
ssh -i "mykeypair.pem" -t ec2-user@[IP].[location].compute.amazonaws.com 'sudo su -c "cd /var/files; bash -l"'
Basically as @blake-percy pointed out but with the -l
option