How to use ssh to run a local .sh file (present on a local linux machine) on a remote linux machine (an AWS ec2 instance) - linux

I am having a tough time figuring out a way to execute a .sh file present on my local linux machine on to a remote linux machine which happens to be an AWS ec2 instance.
Here's what I am doing on my local machine:
ssh -i sample.pem ec2-user#server_name.amazon.com 'bash -s' < file_to_remotely_execute.sh
Error that I get is:
Warning: Identity file sample.pem not accessible: No such file or directory.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Host key verification failed.
This was referred to arrive at the above approach.

Possible issues :
The PEM file doesn't exist.
The PEM file doesn't have proper permissions.
The host is not allowed in ~/.ssh/known_hosts file.
Solutions :
Navigate to the location where PEM file exists or give full path of PEM file.
Give 400 permissions to the PEM file, (sudo chmod 400 /path/to/file.pem).
Login to the server by ssh once, and allow the host.

Related

Run local command in ssh

I want to run local commands with remote files in ssh, such as:
ssh server
# on server
!<local command> and remote files for executing
# still on the server
some other commands
Both remote and local are Linux systems. I guess this should be feasible, which is equivalent to passing a remote file to a local command for execution without exiting the ssh session (for example, using a local compiler to compile a remote file and generate a compiled file to the remote path where the ssh session is located). But I'm not sure if this is possible with the current ssh.

Copying files from a linux machine to an aws ec2 instance

I want to write a jenkins pipeline in which at a particular step i have to copy few zip files from a different linux machine. The pipeline will be running on an AWS EC2 agent.
I have to copy the zip files from linux machine to AWS EC2 instance.
i tried using few ways to handle this using curl and scp but not able to achieve it. Is there a better way to achieve it.
With curl : i am facing connection reset by peer error. Please help
I would use scp for this task. Here's an example of me copying over a file called foo.sh to the remote host:
scp -i mykey.pem foo.sh "ec2-user#ec2-123-123-123-123.compute-1.amazonaws.com:/usr/tmp/foo.sh"
in the example:
mykey.pem is my .pem file
foo.sh is the file I want to copy across
ec2-user the user on the host
123-123-123-123 the (fake) public ip address of the host
/usr/tmp/foo.sh the location where I want the file to be

Linux subsystem ssh don`t see id_rsa.pub

I used to have ssh connection to my server from bash console on Linux subsystem in Windows 10.
I reinstalled Windows and moved id_rsa, id_rsa.pub and known_hosts to exact the same folder where it was on previous system.
But now ssh dont see keys and ends up with error Permission denied (publickey).
But I still can connect using CMD with those keys so issue is not dependig on key file.
On previous system the ssh keys was stored on path: C:\Users\My_Win10_User_Name\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home\My_Linux_Subsystem_User_Name\.ssh so I moved keys to this folder.
What steps should be taking to make ssh on Linux subsystem works again with my old keys?
ssh requires permissions to be correct. Your ~/.ssh directory must be 0700, and the files inside must be 0600. You also don't mention your ~/.ssh/authorized_keys file, which must contain your public key file (the contents of id_rsa.pub.) That file, too, must be chmoded to 0600.

Using SCP command to download files from Linux server to client server

I'm creating files on a Linux server that I'm logged into and I'm adding the ability for the user to download these files from the Linux server on to the connecting computer. I'm writing a scrip and using the scp command to download these files:
scp data.txt user#usraddress:/home/usr
However, I don't want to specify "user#usraddress:/home/usr" to be just my computer. I want whoever is logged onto the linux server to be able do download these files. Is there a way to get the address of the connecting computer?
How would I do this?
Forgive me if this seems elementary, I'm very new to scripting.
When you open a remote session in a GNU/Linux machine, the ssh server sets the environment variable SSH_CONNECTION with some connection information. You can use this variable and the $USER variable to fill that parameters:
scp data.txt $USER#${SSH_CONNECTION%% *}:/home/$USER
Note that as far as I know you couldn't assume the client home directory is at /home. As said by chepner, you could omit the destination directory to use the default location, the home directory.
scp data.txt $USER#${SSH_CONNECTION%% *}:

How do I remove default ssh host from ssh configuration?

I used to connect to Amazon web services using ssh command and application.pem key. Now when I try to connect to other platforms such as Github my ssh client looks for same application.pem key and tries to connect to AWS. How do I connect to Github or change the default host and key configuration.I am using a Ubuntu 13.10 system and following is my ssh output.
pranav#pranav-SVF15318SNW:~/.ssh$ ssh
Warning: Identity file application.pem not accessible: No such file or directory.
You need the identity file to login to the box. Use the command:
ssh -i (identity_file) username#hostname"
This worked for me. Write just the filename (without any slashes), unlike Amazon EC2 tutorial which asks you to enter:
ssh -i /path/key_pair.pem ec2-user#public_dns_name
and also check the permission

Resources