Add users with SSH access to your Amazon EC2 Linux instance

When you launch an instance on EC2, it launches with the default system user account, but if multiple users access it, it's a good idea to use a separate account for each user.

Preparation of key pair

First, prepare a key pair for the new user. It is easy to manage by creating with the ▼ network & security key pair on the EC2 dashboard.

スクリーンショット 2020-11-02 11.22.37.png

Obtaining a public key

After DLing locally in the pem file, get the public key of the key pair.

python


$ ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem 
//This is the path of the obtained pem.

The command returns a bubble key. (I will use it later.)

Add user

Add a new user. After connecting with SSH, add a user with the adduser command and set up with a new account.

python


$ sudo adduser new_user
// new_Replace user with new account name

python


$ sudo su - newuser //newuser is any username
$ mkdir .ssh // .Create ssh directory
$ chmod 700 .ssh // .Change permissions on ssh directory
$ touch .ssh/authorized_keys //authorized_The rule to create keys with this name
$ chmod 600 .ssh/authorized_keys // authorized_Change permissions on keys

Copy and paste the public key you got earlier into authorized_keys.

Check if you can SSH with a new user.

python


$ ssh -i /path_to_key_pair/my-key-pair.pem new_user@public_IPv4_DNS
//Replace with pem file path, new account name, ec2 IP address or DNS

python


Last login: Sun Nov  1 08:16:38 2020

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

I was able to SSH with my new account.

Pass sudo privileges without password

The user who created it cannot use the sudo command at this time.

python


$ sudo passwd new_user

Set the password with. If you enter the password at the time of sudo, you can use the sudo command.

However, if it is troublesome to enter each time, you can add a user to the group that does not require password entry, and you can do sudo without the above settings.

python


$ sudo visudo

#%wheel ALL=(ALL)ALL ← Comment out.
%wheel ALL=(ALL) ALL

Comment out the above of visudo and

$ sudo usermod -aG wheel new_user

Add a new user to the wheel group.

Recommended Posts

Add users with SSH access to your Amazon EC2 Linux instance
AWS EC2 2nd SSH connection to EC2 Instance (Amazon Linux2)
Add SSH connectable users to EC2
Creating an AWS EC2 EC2 Instance (Amazon Linux AMI) 2
Jupyter Notebook Settings-How to use (EC2 Amazon Linux 2)
Create Amazon Linux with AWS EC2 and log in
Yum command to access MySQL with Python 3 on Linux
Copy files directly from Amazon EC2 (Amazon linux) to S3
[AWS EC2] How to install Maven on Amazon Linux 2
ODBC access to SQL Server from Linux with Python
[AWS EC2] Settings you want to do on Amazon Linux 2
Create an AWS Cloud9 development environment on your Amazon EC2 instance
Seamlessly join a Linux EC2 instance to AWS Managed Microsoft AD
Prepare pipenv environment with amazon Linux 2
Add fields to features with ArcPy
Install pyenv on EC2 (Amazon Linux)
Introducing Amplify to Amazon Linux 2 AMI
[EC2] How to take a screen capture of your smartphone with selenium
Data integration from Python app on Linux to Amazon Redshift with ODBC
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
How to access data with object ['key'] for your own Python class
Install Python3 and Django on Amazon Linux (EC2) and run your web server