[LINUX] Add SSH connectable users to EC2

Contents

Use public key authentication when connecting to EC2 by SSH, and if it is Amazon Linux 2, use other than ec2-user Create a new user to be able to connect.

Target device

EC2(Amazon Linux2)

Reference procedure

The procedure is based on the following AWS formula. [Create a key pair using Amazon EC2](https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ec2-key-pairs.html#having-ec2-create-your-key -pair) Create a user account

Hands-on

Creating a key pair (using the AWS Management Console)

** If there is a key pair to connect to EC2, it is not necessary. ** **

Log in to the AWS Management Console and select Services ⇒ Computing ⇒ EC2. ss_000.JPG

From the menu on the left, select [Network & Security] ⇒ [Key Pair]. ss_001.JPG

Click Create Key Pair from the key pair list screen. ss_002.JPG

Select [Create Key Pair] for [Name] and [File Format], and click [Create Key Pair].

User Creation & Settings (Linux)

Connect to EC2 and switch to the root user.

User created

# adduser Inon

Password setting

# passwd Inon
Changing password for user Inon.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Switch to the created user

# exit
logout

$ su - Inon
Password:

$ whoami
Inon

ssh / authorized_keys settings

Make sure you are in the home directory of the user (Inon) you created

$ pwd
/home/Inon

Create & configure .ssh directory

$ mkdir .ssh
$ ls -la
total 16
drwxrwxr-x 2 Inon Inon   6 Mar 12 01:09 .ssh
$ chmod 700 .ssh
$ ls -la
total 16
drwx------ 2 Inon Inon   6 Mar 12 01:09 .ssh

Obtaining a public key

Transfer the key pair used to connect to EC2 to the server. This time, I placed it under ** / tmp **.

$ ssh-keygen -y -f /<Key pair storage location>/<Created key pair name>.pem 
ssh-rsa ...******<Character strings are lined up in a row>******...

Creating an authorized_keys file

$ touch .ssh/auhthorized_keys
$ chmod 600 .ssh/authorized_keys
$ ll .ssh
total 0
-rw------- 1 Inon Inon 0 Mar 12 01:12 authorized_keys
$ vi .ssh/authorized_keys

Paste the public key obtained with the ssh-keygen command with the vi command.

the end

You can now connect to EC2 with the created user.

Recommended Posts

Add SSH connectable users to EC2
Add users with SSH access to your Amazon EC2 Linux instance
Add convolution to MNIST
AWS EC2 2nd SSH connection to EC2 Instance (Amazon Linux2)
SSH connection to a private server using a bastion server on EC2
Add / remove kernel to JupyterLab
Add a dictionary to MeCab
Add page number to PDF
Add System to pyenv-win versions
Add user dictionary to MeCab