SCP Command for EC2 Instances

SCP Command for EC2 Instances

Accessing EC2 Command

  • Open Your CLI (Command Line Interface).

  • Copy your Key Pair file from Downloads and paste it into a new folder/directory.

  • You can create a new directory using (mkdir).

  • For copy and paste you can use ( cp Downloads/"key-pair name" "folder name where you want to paste/".) ex - cp Downloads/nidhish-ec2-demo.pem launch/

  • After this, you should switch to your directory where you have pasted the file using the cd command. (ex-cd launch)

  • Firstly provide the permissions to the file in Linux. Use chmod 400 "file-name". (ex- chmod 400 nidhish-ec2-demo.pem).To provide the permissions.

  • After all this use the ssh command to access your instance.

  • The proper command is ssh -i "key pair name" "username of instance"@"Public IPv4 address".

  • ex - ssh -i nidhish-ec2-demo.pem ubuntu@ 3.110.85.20

  • Here is the username of every OS Image.

  • For Amazon Linux 2 or the Amazon Linux AMI, the username is ec2-user.
    For a CentOS AMI, the user name is centos.
    For a Debian AMI, the user name is admin.
    For a Fedora AMI, the user name is ec2-user or fedora.
    For an RHEL AMI, the user name is ec2-user or root.
    For a SUSE AMI, the user name is ec2-user or root.
    For a Ubuntu AMI, the user name is ubuntu.
    For an Oracle AMI, the user name is ec2-user.
    For a Bitnami AMI, the user name is bitnami.

  • The below image depicts where you can get your public address.

  • Type exit to get out of your Instance.

  • Don't forget to STOP your EC2 Instance.

How to Access Multiple Instances

  • First, create 2 EC2 Instances on AWS.

  • If you do not know how to create an EC2 Instance you can check my previous blog.nidhishmalav.hashnode.dev/ec2-with-linux

  • Here I am using the same Private key for both Instances(nidhish-ssh-keypair.pem).

  • Access your Instances using the ssh command.

  • If you want detailed info about how to connect the EC2 Instance from Linux. Have a look at my previous blog.nidhishmalav.hashnode.dev/ec2-with-linux

  • You can click here to open multiple tabs in your terminal. Refer to the below image.

  • The below image shows the local machine terminal.

  • The below Image shows the First EC2 Instance terminal.

  • The below Image shows the Second EC2 Instance terminal.

Understanding SCP Command

What is SCP?

In the Linux operating system, the SCP (secure copy) command is used to transfer files securely between servers. Secure file transfer between a local host and a remote host, or between two remote hosts, is possible with the SCP command, also known as secure copy. It employs the same security and authentication measures as the Secure Shell (SSH) protocol.

How to Use SCP for File

  • The proper Command is (scp -i nidhish-ssh-keypair.pem nidhish-ssh-keypair.pem ubuntu@13.235.244.1**:~).

  • Now we will discuss the command in detail.

  • At first, the scp is written which means to copy the file securely.

  • Now we will write the option with the command (-i).

  • After this, we will write the private key of the instance which is used to access that Instance. In short, you will write the private key of the instance in which you want to copy the file.

  • Here I used the nidhish-ssh-keypair.pem to access my EC2 Instance demo-1.

  • Here I am copying the keypair from the local machine to EC2 Instance demo-1.

  • After this, we will write the file with the path which we want to copy.

  • After this, we will write the username and IP address of the Instance in which we want to paste the file with the location where to paste using(:"location").

  • Here I am Copying the file from my local machine to EC2 Instance demo-1. So I have written the username and IP address of EC2 Instance demo-1 and pasted the file in Home(~).

  • Hit the enter Button on your file. The file will be Pasted successfully into your EC2 Instance.

  • You can check it by going to the Instance terminal and run the ls -al Command.

How to use SCP Command for Folder

  • Create a folder if you do not have it. Here folder means Directory.

  • I am creating a new folder named (new) using mkdir command.

  • The command will be the same as we used for the file procedure.

  • We will add a new option (-r).

  • Here I am in my EC2 Instance demo-1 and copying the folder from it.

  • Pasting my folder to the EC2 Instance demo-2.

  • Just hit the enter button your folder will be pasted.

Debugging

  • If you face any error regarding permissions of the private key file use (chmod 400 "private key name").

  • If the SCP Command is not running properly. It is possible because the Instance is not permitted to connect with your IP address.

  • Here's the solution adding a new Inbound Rule.

  1. Go to your EC2 Instance Security section and click on the security group.

  2. Then select the type of network you want to connect.

  3. Then Type the IP address of the machine or Instance you wanted to give permission of accessing.

  4. Then save the rule.

  5. I hope this will resolve your issue.

Sagar Utekar Helped me to understand the scp command.