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.
EC2(Amazon Linux2)
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
** 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.
From the menu on the left, select [Network & Security] ⇒ [Key Pair].
Click Create Key Pair from the key pair list screen.
Select [Create Key Pair] for [Name] and [File Format], and click [Create Key Pair].
Connect to EC2 and switch to the root user.
# adduser Inon
# passwd Inon
Changing password for user Inon.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
# exit
logout
$ su - Inon
Password:
$ whoami
Inon
$ pwd
/home/Inon
$ 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
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>******...
$ 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.
You can now connect to EC2 with the created user.
Recommended Posts