Lab2 Linux Refresher and Automation With Bash Scripts
Lab2 Linux Refresher and Automation With Bash Scripts
Submission requirements:
Submit screenshots with functionality of tasks 2, 3, 4, 5.
Submit screenshot that verifies your identity
STYLE: SEN-Footer 1
Task 1: Create a folder structure using basic
Linux commands
mkdir -p Fall/October
mkdir -p Fall/November
mkdir -p Summer/July
mkdir -p Summer/August
touch Fall/October/October_28.txt
cat Fall/October/October_28.txt
vim Fall/October/October_28.txt
STYLE: SEN-Footer 2
Task 2: Working with the Linux package
manager
In your Cloud9 environment, install the latest docker package and make
sure the docker service is running when the VM is restarted.
1.
Find out if this is a Debian or an RMP distribution. What is the version
of this distribution? What chip manufacturer and architecture?
Version uname –r
Manufacturer uname –m
2.
Find all the repos available on Cloud9 VM
STYLE: SEN-Footer 3
Using YUM yum info docker
4.
Identify the package that provides traceroute command
5.
Is the docker package installed? If yes, uninstall the existing docker
package and re-install it
6.
Try running docker commands. Does it work? Use systemctl
commands to start and enable docker daemon
STYLE: SEN-Footer 4
Make sure docker is sudo systemctl enable docker
running after reboot
STYLE: SEN-Footer 5
aws ec2 run-instances --image-id
resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-
hvm-x86_64-gp2 --instance-type t2.micro --key-name week2
Note: if you are getting SSM access error please run the command below:
# Install jq
# Download IP ranges
wget https://ip-ranges.amazonaws.com/ip-ranges.json
jq '.prefixes[] | select(.service=="CLOUD9")|
select(.region=="us-east-1")' < ip-ranges.json
STYLE: SEN-Footer 6
aws ec2 authorize-security-group-ingress --group-name
acs730-week2-sg --ip-permissions
IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=35.1
72.155.192/27},{CidrIp=35.172.155.96/27}]
IpProtocol=icmp,FromPort=-1,ToPort=-
1,IpRanges=[{CidrIp=35.172.155.192/27},
{CidrIp=35.172.155.96/27}]
export INSTANCE_ID=i-039846ca048ac228a
export SG_ID=sg-03c8aacfc095f4070
dig ec2-54-161-142-127.compute-
1.amazonaws.com
STYLE: SEN-Footer 7
Is the local link up? ip link
telnet ec2-54-161-142-127.compute-
1.amazonaws.com 22
id
ssh ec2-user@ec2-54-161-142-
Let's debug 127.compute-1.amazonaws.com -i
~/.ssh/week2 -vvv
STYLE: SEN-Footer 8
6. Cloud 9 environment connects to our EC2 instance using its private IP
address. Let’s fix our security group.
The content of the script is below. You can find this script in the Lab2
folder.
STYLE: SEN-Footer 9
#!/bin/bash
4.
STYLE: SEN-Footer 10
5. Create a new folder ~/myscript and move the run_docker.sh to this
folder. Grant ec2-user execution permissions on this script.
mkdir ~/myscript
v mv ~/run_docker.sh ~/myscript/
~/myscript/run_docker.sh
sudo -i
mv /home/ec2-user/myscript/run_docker.sh /usr/bin/
vi /usr/bin/run_docker.sh
STYLE: SEN-Footer 11
Update the last line in the script.
#!/bin/bash
vi /etc/systemd/system/run_docker.service
[Unit]
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
STYLE: SEN-Footer 12
RestartSec=1
User=ec2-user
ExecStart=/usr/bin/run_docker.sh
[Install]
WantedBy=multi-user.target
Reload the daemon the ensure all service definitions have been
updated.
systemctl daemon-reload
Start the service and enable the service. Make sure to run these
commands as ec2-user.
exit
STYLE: SEN-Footer 13
9. Debug the issues with your service using journalctl
10. Create a new Linux user, grant same permissions as the existing
ec2-user. Does this user have a sudo permissions?
# ec2-user groups
groups
groups week2
sudo visudo
curl localhost:80
STYLE: SEN-Footer 14
Task 5: (OPTIONAL) Linux Networking -
Troubleshooting web server connectivity
1. Send HTTP request to the Nginx webserver running on your EC2 from
your Cloud9 environment
curl http://ec2-44-201-108-170.compute-1.amazonaws.com/ -v
2. Let’s troubleshoot
ip link
STYLE: SEN-Footer 15
DNS is resolving, run in Cloud 9:
nslookup google.com
ip link
Opening port 22 to the world is a security concern and is not considered the
best practice by AWS. Moreover, user activities on the EC2 performed by
user connected via SSH are not reflected by any of the AWS logs making
forensics often impossible is the local logs are deleted by the malicious
actor.
STYLE: SEN-Footer 16
We can use AWS SSM Session manager for this purpose.
Pre-requisites:
STYLE: SEN-Footer 17
Run the command below to associate the instance profile with EC2
instance.
STYLE: SEN-Footer 18
Important Security Improvement: all the
processes running on EC2 instance can now
call AWS API without any credentials stored
locally on the VM.
We can verify that AWS SSM agent is running on EC2 instance in two
ways:
Click on Actions => Monitor and Troubleshoot => Get system Log.
STYLE: SEN-Footer 19
This is the log that captures all the services initialized as part of Linux
OS boot up process.
Open your Cloud9 environment and find out the chip type that is used
by Cloud9 VM.
uname –m
curl "https://s3.amazonaws.com/session-
manager-downloads/plugin/latest/linux_64bit/session-manager-
plugin.rpm" -o "session-manager-plugin.rpm"
Run the commands below to connect to the instance via AWS SSM
session manager:
STYLE: SEN-Footer 21