Ravindra BagaleCourses & study guides मराठी Track your progress

4. Amazon EC2: Launch and Connect to Your Linux Server

4.4 Key Pairs, chmod 400 and SSH from Linux/Mac

Key pair mhanje tumchi digital killi. AWS kade public key rahte, private key (.pem) fakt tumchyakade – ti ekdach download hote. Ti haravli tar AWS punha deu shakat nahi.

Default usernames

AMI SSH user
Amazon Linux 2023 / Amazon Linux 2 ec2-user
Ubuntu ubuntu
CentOS Stream 9 (official) ec2-user (older CentOS 7/8 AMIs: centos)
RHEL ec2-user
Debian admin
SUSE ec2-user

From Linux or macOS (OpenSSH)

cd ~/Downloads
chmod 400 mykey.pem                                  # private key must not be readable by others
ssh -i mykey.pem ec2-user@<PUBLIC_IP>                # Amazon Linux / CentOS Stream
ssh -i mykey.pem ubuntu@<PUBLIC_IP>                  # Ubuntu

Type yes when asked "Are you sure you want to continue connecting?" (first time only — it stores the server fingerprint in ~/.ssh/known_hosts).

UNPROTECTED PRIVATE KEY FILE!

If you see Permissions 0644 for 'mykey.pem' are too open, SSH refuses to use the key. Fix with chmod 400 mykey.pem. Never share your .pem, never upload it to GitHub, never e-mail it.

Why chmod 400? SSH client refuses a private key that other users on your laptop can read, because a readable key is as good as a stolen key. 400 means owner read-only, nobody else anything.

ls -l mykey.pem          # -rw-r--r--  -> too open, SSH will refuse
chmod 400 mykey.pem
ls -l mykey.pem          # -r--------  -> correct
ssh -v -i mykey.pem ec2-user@<PUBLIC_IP>     # -v shows every step if login fails

Why this matters for security

Private keys leaked on GitHub, in e-mail or in shared folders are a common cause of cloud compromise. Treat a .pem like the password to the whole server: one key per person, chmod 400, never committed to Git, and rotated when a team member leaves.

Ravindra Bagale's Tip

When "Permission denied (publickey)" appears, many students go to download the key again – but you can't get it again! Most of the time the mistake is the username: ec2-user for Amazon Linux, ubuntu for Ubuntu. First check the username and chmod 400, then run ssh -v.

Practice task

Create a folder ~/aws-keys, move your key into it, run chmod 400, and connect to both instances using the correct user names.

Extended teaching notes

Why this lesson matters

Hardening is deleting unnecessary risk: fewer packages, fewer open ports, fewer privileged accounts, faster patches, better logs.

Hardening related services

[('SSH', 'Key auth, no root login, Fail2ban or equivalent, SG limited to your IP/VPN.', '', ''), ('SMB / NFS / LDAP / SNMP', 'Do not expose to the internet; patch; strong auth; disable unused versions (e.g. SMBv1).', '', ''), ('SMTP', 'Authenticated relay only; SPF/DKIM/DMARC for your domains.', '', ''), ('DNS', 'Restrict zone transfers; monitor for unexpected records (crt.sh for certs).', '', ''), ('Firewalls', 'Host firewall + cloud SG/NSG; default deny; document every allow.', '', '')]

Real examples

Global: CIS Benchmarks exist for Linux, Windows and clouds. Indian company (sample): a Pune team requires SSH keys only and weekly patch windows for internet-facing hosts.

Mini case study

Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.

Defender's worksheet (do this today)

  1. Write the lesson title, date and the exact lab target you are allowed to use.
  2. List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
  3. Run one safe check from the lesson on a lab system only. Paste the command and output.
  4. Write a two-sentence fix you would give a teammate.
  5. Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.

Indian workplace (sample data)

Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).

Global practice

Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.

Everyday analogy

Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.

Key terms

  • Control: a safeguard (patch, config, process, detection).
  • Finding: a weakness backed by evidence.
  • Remediation: the fix plus how you verified it.
  • Scope: systems you have written permission to touch.
  • Least privilege: only the access the task needs.
  • Defence in depth: several layers so one failure does not end the story.

Common mistakes

  • Skipping written permission "just for a quick scan".
  • Practising on hostel, office or public targets without authorisation.
  • Collecting tool output without writing the fix.
  • Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
  • Publishing screenshots that contain secrets, cookies or internal hostnames.

Recap

  • Evidence, fix, owner, date.
  • Own lab only.
  • Fix > spectacle.