0% found this document useful (0 votes)
17 views

EC2's Commands

1. The document provides instructions for several tasks related to managing EC2 instances: changing the hostname; adding new users; adding users to the sudo group; viewing user history; modifying files without editing; mounting an EFS file system; and creating symbolic links. 2. Specific steps are outlined for adding a new user, setting permissions on the user's home directory and SSH keys, and verifying the user can connect via SSH. 3. Commands are shown to get a list of EC2 instances with their IDs, private IPs, states, and tag values for the "Name" key.

Uploaded by

kolofelik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

EC2's Commands

1. The document provides instructions for several tasks related to managing EC2 instances: changing the hostname; adding new users; adding users to the sudo group; viewing user history; modifying files without editing; mounting an EFS file system; and creating symbolic links. 2. Specific steps are outlined for adding a new user, setting permissions on the user's home directory and SSH keys, and verifying the user can connect via SSH. 3. Commands are shown to get a list of EC2 instances with their IDs, private IPs, states, and tag values for the "Name" key.

Uploaded by

kolofelik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

A.

Change hostname
1. Edit /etc/hosts

127.0.0.1 localhost new-hostname

2. run

sudo hostnamectl set-hostname new-hostname

3. reboot

B. Add a new user to the EC2 Ubuntu Linux instance


1. Connect to your Linux instance using SSH.

2. Use the adduser command to add a new user account to an EC2 instance
(replace new_user with the new account name). The following example creates an
associated group, home directory, and an entry in the /etc/passwd file of the instance.

$ sudo adduser new_user

The home directory might not be created by default in some configurations. Verify that the
home directory was created before continuing.

Note: If you add the new_user to an Ubuntu instance, then include the --disabled-
password option to avoid adding a password to the new account:

$ sudo adduser new_user --disabled-password

3. Change the security context to the new_user account so that folders and files you
create have the correct permissions:

$ sudo su - new_user

Note: When you run the sudo su - new_user command, the name at the top of the
command shell prompt changes to reflect the new user account context of your shell
session.
4. Create a .ssh directory in the new_user home directory:

$ mkdir .ssh

5. Use the chmod command to change the .ssh directory's permissions to 700. Changing
the permissions restricts access so that only the new_user can read, write, or open
the .ssh directory.

$ chmod 700 .ssh

6. Use the touch command to create the authorized_keys file in the .ssh directory:

$ touch .ssh/authorized_keys

7. Use the chmod command to change the .ssh/authorized_keys file permissions to 600.
Changing the file permissions restricts read or write access to the new_user.

$ chmod 600 .ssh/authorized_keys

Create a key pair for new user


https://eu-west-1.console.aws.amazon.com/ec2/home?region=eu-west-1#KeyPairs:

Get Public key from puttygen


Update and verify the new user account credentials
After you retrieve the public key, confirm that you have permission to add the public key to
the .ssh/authorized_keys file for this account:

1. Run the Linux cat command in append mode:

$ cat >> .ssh/authorized_keys

2. Paste the public key into the .ssh/authorized_keys file and then press Enter.

Note: For most Linux command line interfaces, the Ctrl+Shift+V key combination pastes the
contents of the clipboard into the command line window. For the PuTTY command line
interface, right-click to paste the contents of the clipboard into the PuTTY command line
window.

3. Press and hold Ctrl+d to exit cat and return to the command line session prompt.
Verify that the new user can use SSH to connect to the EC2
instance
1. Run the following command from a command line prompt on your local computer:

$ ssh -i /path/new_key_pair.pem
new_user@public_dns_name_of_EC2_Linux_instance

To connect to your EC2 Linux instance using SSH from Windows, follow the steps
at Connect to your Linux instance from Windows using PuTTY.

Note: If you receive errors when trying to connect, then see Troubleshoot connecting to your
instance.

2. Run the id command from the instance's command line to view the user and group
information created for the new_user account:

$ id

The id command returns information similar to the following:

uid=1004(new_user) gid=1004(new_user) groups=1004(new_user)

3. Distribute the private key file to your new user.

C. Add user to sudoers


usermod -aG sudo username

D. Examples of users history on creating new ec2


#root:
1 apt update
2 apt upgrade
3 apt autoremove
4 apt clean
5 reboot
6 .. ..
7 hostnamectl set-hostname ProdSQLserver
8 apt install mc, micro, ncdu
9 apt install mc
10 apt install micro
11 apt install ncdu
12 mc
13 micro
14 ncdu
15 xtop
16 htop
17 micro /etc/hosts
18 reboot
19 adduser icon
20 su - icon
21 usermod -aG sudo icon
22 su - icon
23 exit
24 EDITOR=micro visudo
25 exit
26 history

# icon:
1 mkdir .ssh
2 chmod 700 .ssh
3 touch .ssh/authorized_keys

5 chmod 600 .ssh/authorized_keys


6 cat >> .ssh/authorized_keys
7 cat .ssh/authorized_keys
8 id
9 exit
10 sudo whoami
11 exit

E. Changing a value to a file without edit


sed -i 's/Production/Production/' /etc/filebeat/filebeat.yml

The above will change PmiProduction string with Production in the filebeat.yml file

F. Make an efs mount


1. Install nfs utils:

sudo apt install nfs-common

2. add to fstab:

# Mount EKS efs filesystem


# <EFS-DNS-Name>:/ /mnt/efs nfs defaults,_netdev 0 0
# for Prod:
fs-590.efs.eu-west-1.amazonaws.com:/ /mnt/prod-efs nfs defaults,_netdev 0 0

3. reboot

if we need to mount without reboot:

#sudo mount -t nfs -o


nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 <EFS-DNS-
Name>:/ /mnt/efs

# in Prod:
sudo mount -t nfs -o
nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-
590.efs.eu-west-1.amazonaws.com:/ /mnt/prod-efs

G. Making a soft link (alias or shortcut) to mounted efs


folder
ln -s /mnt/prod-efs/dynamic_provisioning/pvc-b51-4970-80b3-
30e9a9dca858/integration_shared/ /opt/

This will create a /integration_shared/ ln under /opt/ and will point to efs related folder

To remove the ln:

unlink /opt/integration_shared

H. Get a list of ec2s and their names and private ips

aws ec2 describe-instances --query 'Reservations[*].Instances[*].


[InstanceId,PrivateIpAddress,State.Name,Tags[?Key==`Name`].Value | [0]]' --
output table

You might also like