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

How to Set Up Passwordless SSH Between Linux Servers

Real projects never have just one server. A controller server logs in to the others to deploy code, run checks or collect logs, and it does that with SSH keys, not passwords. This chapter builds exactly that on three EC2 servers: you make a key pair on one server, put its public key on the others, open port 22 between them, and log in with no password. On the way you pick up the everyday commands you use on every server: reading ls -l, sorting with ls -ltr, seeing hidden files, building folders with mkdir -p, checking sizes with du and free space with df, and surviving vi. It starts with an interview favourite: moving a server to another Region.

What you'll learn in this class

  • Setting up a three-server lab, and the checks before you log in
  • Moving an EC2 server to another Region with an AMI copy
  • A quick recap of cat >, >>, merging files, tac, stat and touch
  • The vi and nano survival kit
  • Reading ls -l, sorting with ls -lt and ls -ltr, hidden files with ls -a and ls -la, and ll
  • mkdir -p, cd, cd ../.. and pwd
  • du -sh for a file or folder, and df -h for the whole disk
  • mv to move and to rename
  • Passwordless SSH between EC2 servers as ec2-user: ssh-keygen, authorized_keys, chmod 700/600 and port 22
  • Why the trust is one way, and why four servers both ways need 12 paths
  • The ports you should know by number

1. The lab: three servers, and the checks before you log in

Why: Passwordless SSH only makes sense between several servers. Building three identical ones, and checking they can reach each other first, means any later failure is in the keys, not in the network.

What: three t3.micro servers with Amazon Linux 2023, in the same Region and VPC, launched with the same key pair and security group. Name them linux1, linux2 and linux3 in the Name tag.

How:

  1. Launch one instance with Number of instances: 3, or launch one and then two more. The 7 launch steps are in Your first EC2 instance.
  2. Rename each one in the console (click the pencil in the Name column).
  3. Wait until Status check shows all checks passed. There is a system check (the AWS host and its network) and an instance check (your OS). Newer, Nitro-based types such as t3.micro also run an attached EBS check, so they show 3/3; older types show 2/2. Details: Status checks: 2/2 or 3/3?.
  4. Log in with SSH and your key, or with EC2 Instance Connect in the browser.

Classroom line

Small mistakes become big ones.

Two things that help in a multi-server lab:

  1. .pem or .ppk: .pem is the OpenSSH format, used by the ssh command on Windows, Mac and Linux. .ppk is PuTTY's format; PuTTYgen converts one into the other. More in Step 4: Key pair.
  2. Which server am I on? The prompt shows the host name, which is ip- plus the private IP by default, not the Name tag. To see the Name in the prompt, run this once on each server, then log out and back in:
    sudo hostnamectl set-hostname linux1
    

2. Interview question: move an EC2 server to another Region

Why: you can't drag an instance from Mumbai to Singapore. An instance, its volumes, its key pair and its security group all belong to one Region. You copy the image instead.

Classroom line

They really ask this in interviews.

Move an EC2 server to anotherRegion: AMI, copy, launch1. Create imageAMI of the serverin Mumbai2. Copy AMIto Singapore:new AMI ID3. Launchfrom the copyin Singapore4. Check, thenstop or terminatethe old oneNot copied with the AMI, set them up inthe new Region:• Key pair: create one there, or import the samepublic key (key pairs belong to one Region).• Security group: create it again (port 22, 80...). • IP addresses: the new server gets newones.• The copied AMI has its own snapshots inSingapore, billed there until you delete them.

Figure 1. Create an AMI in Mumbai, copy it to Singapore (the copy gets a new AMI ID), launch from the copy, check it, then stop or terminate the old server. The key pair, security group and IP addresses are set up again in the new Region.

How:

  1. In Mumbai, select the instance, then Actions > Image and templates > Create image. Wait until the AMI is Available. (AMIs are explained in AWS Elastic IP, AMI and EBS Snapshots.)
  2. In AMIs, select it, then Actions > Copy AMI, and choose Asia Pacific (Singapore) as the destination Region.
  3. Switch the console to Singapore. The copy has a new AMI ID; wait until it is Available.
  4. Launch instance from AMI. Pick a key pair that exists in Singapore (create one, or import your existing public key) and a security group with the ports you need.
  5. Test the new server. Then stop or terminate the old one, and deregister AMIs and delete snapshots you no longer need, because each Region bills its own copy.

Anything written on the old server after step 1 is not in the copy, so plan a short cut-over. If an interviewer then asks you to rate yourself out of 10, be honest: a 6 you can defend is better than a 9 you can't.

3. Quick recap: cat, tac, stat and touch

Why: These commands come back in this chapter: cat to read the public key, stat and touch while checking files. A short refresher keeps the setup steps fast.

These come from earlier chapters, so here is only the short version:

cat > file1
cat >> file1
cat file1 file2 > file3
tac file3
stat file3
touch f1 f2 f3
  1. cat > file1 writes what you type; press Ctrl+D on an empty line to save. Ctrl+C cancels the command and can leave you with an empty or half-written file.
  2. >> appends at the end; a single > replaces the content.
  3. cat file1 file2 > file3 joins two files into a third; tac prints the lines in reverse.
  4. stat shows Access, Modify, Change and Birth. On EC2 the clock is in UTC, so add 5:30 to read it in India time.
  5. touch on an existing file sets Access and Modify to now (Change follows); on a new name it creates an empty file. It never touches the content.

Classroom line

Never press Ctrl+C here, it's Ctrl+D.

The full explanations: cat and the > trap, the three timestamps and cat and tac.

4. The vi and nano survival kit

Why: you will edit authorized_keys in section 14, and on many servers vi is the only editor installed.

vi survival kit: three modes Normal mode vi opens here; keys are commands (arrows move, dd deletes a line) i / o Insert mode -- INSERT -- at the bottom: now you type text Esc Normal mode : Command line :wq save and quit :q! quit, no save :wq! force save, quit Enter nano instead: type straight away; Ctrl+O saves, Ctrl+X exits(Y, Enter to save on the way out). Lost in vi? Press Esc twice, then type :q! and Enter. Nothing is saved, andyou are back at the prompt.

Figure 2. vi opens in normal mode. i or o switch to insert mode for typing, Esc goes back, and : opens the command line for :wq, :q! and :wq!. nano needs no modes: Ctrl+O saves, Ctrl+X exits.

How, in vi:

  1. vi notes.txt opens the file in normal mode: keys are commands, not text.
  2. Press i to type at the cursor, or o to open a new line below. The bottom shows -- INSERT --.
  3. Type your text, then press Esc to go back to normal mode.
  4. Type :wq and Enter to save and quit, :q! to quit without saving, or :wq! to force the save (for example on a read-only file you own).
  5. Useful in normal mode: G jumps to the last line, dd deletes the current line.

In nano: type straight away; Ctrl+O saves, Ctrl+X exits (answer Y and Enter if it asks to save).

Classroom line

Work calmly, with presence of mind.

The browser terminal and zoom: in EC2 Instance Connect, zooming the browser while vi is open can scramble the screen, because the browser terminal doesn't pass the new size to vi cleanly. vi itself is fine. Set the zoom before you open vi, press Ctrl+L to redraw, or use a normal SSH terminal. More on both editors: nano and vi.

5. Reading ls -l

Why: SSH refuses keys and folders with loose permissions. Reading ls -l correctly is how you spot that before SSH silently ignores your key.

What: ls -l prints one item per line with its type, permissions, owner, size and time. The first character alone tells file from folder (more on it).

Reading one line of ls -l -rw-r--r--. 1 ec2-user ec2-user 103 Mar 1 10:00 file2 - type: - file, d folder, l link rw-r--r-- permissions: owner, group, others . . = SELinux label (Amazon Linux) 1 number of links ec2-user owner ec2-user group 103 size in bytes (-h: K, M, G) Mar 1 10:00 last modified (Modify time) file2 name ls -l lists one item per line. ll is a shortcut for it on Amazon Linux (ls -lwith colours).

Figure 3. One line of ls -l: type, permissions, the SELinux dot on Amazon Linux, links, owner, group, size in bytes, last modified time and name.

  1. Type: - file, d directory, l link.
  2. Permissions: three groups of rwx for owner, group and others. The . after them on Amazon Linux means the file has an SELinux label.
  3. Links, owner, group.
  4. Size in bytes. Add -h (ls -lh) to see K, M and G.
  5. Last modified time, then the name.

6. Sorting by time: ls -lt and ls -ltr

Why: "which file changed last?" is the first question when something breaks.

How:

touch file7
touch file8
ls -lt
ls -ltr
  1. ls -lt sorts by Modify time, newest first: file8 is at the top.
  2. ls -ltr reverses it, newest last: file8 is at the bottom, right above your prompt, where your eyes already are. ls -lrt is the same command; the letters can go in any order.
  3. On a server, ls -ltr /var/log shows the log that was written most recently at the bottom.

7. Hidden files: ls -a, ls -la and ll

Why: The SSH files live in the hidden folder ~/.ssh. A plain ls never shows it, so you need -a to see what you are about to change.

What: a name that starts with a dot is hidden. ls skips it; ls -a shows it.

ls -a
ls -la
ll
  1. ls -a lists everything, including . (this folder), .. (the folder above), .bashrc, .bash_profile and .ssh.
  2. ls -la gives the same list in long format: use it to check permissions on .ssh.
  3. ll is a shortcut (an alias). On Amazon Linux it means ls -l with colours; on Ubuntu it means ls -alF. Type alias ll to see what it means on your server.
  4. A typo such as la -la gives command not found: read the first word of the error.

Classroom line

Hidden files and directories start with a dot.

ls options you will use every day ls names only, A to Z   file1 file2 notes ls -l long list: one item per line   -rw-r--r--. 1 ec2-user ... file1 ls -lt newest first (by Modify time)   notes file2 file1 ls -ltr newest last; same as ls -lrt   file1 file2 notes ls -a also hidden names (start with .)   . .. .bashrc .ssh file1 ls -la hidden too, in the long list   drwx------. ... .ssh ls -lh sizes as K, M, G   ... 4.9K ... big.bin ll shortcut (alias) for ls -l   Amazon Linux; on Ubuntu ll = ls -alF Letters combine in any order: ls -ltr = ls -lrt = ls -l -t -r. Orange = thetwo most used on servers.

Figure 4. ls, ls -l, ls -lt, ls -ltr, ls -a, ls -la, ls -lh and ll, with what each shows. The letters combine in any order.

More on hidden files: Hidden files: a dot at the start of the name.

8. Folders: mkdir -p, cd and pwd

Why: You will create and move between folders constantly while setting up servers. -p and relative cd paths save many commands.

How:

mkdir dir1
mkdir -p dir2/dir3/dir4/dir5
cd dir2/dir3/dir4/dir5
pwd
cd ../..
cd
  1. mkdir -p makes the whole chain at once. Without -p, mkdir dir9/dir8 fails with No such file or directory because dir9 doesn't exist. With -p, running it again is not an error.
  2. cd dir2/dir3/dir4/dir5 goes down four levels in one step; pwd prints the full path.
  3. cd ../.. goes up two levels. cd alone always takes you home (/home/ec2-user).
  4. cd dir1/dir2 fails with No such file or directory when that path doesn't exist. Read the error, then check the path with ls and pwd.

Classroom line

How do we know which directory we're in?

mkdir -p, cd and pwd, step by step $ mkdir -p dir2/dir3/dir4/dir5 /home/ec2-user/ ● └─ dir2/ └─ dir3/ └─ dir4/ └─ dir5/ $ pwd → /home/ec2-user One command makes all four folders, each inside the one before. $ cd dir2/dir3/dir4/dir5 $ pwd → /home/ec2-user/dir2/dir3/dir4/dir5 Go down four levels in one step. pwd prints where you are. $ cd ../.. $ pwd → /home/ec2-user/dir2/dir3 .. is the folder above. ../.. goes up two levels: back to dir3. $ cd $ pwd → /home/ec2-user cd alone always takes you home, from anywhere. Orange = where you are now

Figure 5. Animation: mkdir -p makes dir2 to dir5 in one step, cd dir2/dir3/dir4/dir5 goes down four levels, cd ../.. comes back up to dir3, and cd alone returns home. pwd shows the path each time.

Classroom line

In DevOps, the more errors you hit, the more you learn.

The details of paths and cd are in Linux directory commands.

9. du: how big is this file or folder?

Why: When a disk fills up you need to know which folder is eating it. du answers that.

What: du (disk usage) shows the space that the things you name take on disk.

du -sh file7
du -sh dir2
du -sh *
  1. -s means summarize: one total per item, instead of a line for every folder inside.
  2. -h means human-readable: K, M and G instead of blocks.
  3. du -sh * gives one line for each item in the current folder (hidden items are skipped by *).
  4. An empty file shows 0. A small file shows 4.0K, because disk space is used in 4 KB blocks; ls -l shows the exact bytes.
  5. For folders you can't read, such as /var/log, use sudo du -sh /var/log.
A black terminal with two columns: sizes 0 or 4.0K on the left, and the names dir1, dir2, f1 to f4, file-007, file-123, file-7, file-xyz, file1, file2, file3 and file8 on the right.

Console: du -sh * on the lab server: empty files and folders show 0, and each small file takes one 4.0K block.

10. df: how full is the disk?

Why: A full disk stops databases and websites. df is the first check, and with passwordless SSH you can run it on every server from one place.

What: df (disk free) shows each mounted filesystem: its size, how much is used, how much is free, and where it is mounted.

df -h
df -h /
A black terminal with the df -h table: Filesystem, Size, Used, Avail, Use% and Mounted on, with rows for devtmpfs, tmpfs, efivarfs, /dev/nvme0n1p1 of 8.0G mounted on /, /dev/nvme0n1p128 on /boot/efi, and more tmpfs lines.

Console: df -h on a t3.micro: the root volume /dev/nvme0n1p1 is 8.0G with 21% used. The tmpfs lines are in RAM.

  1. The line mounted on / is your root EBS volume: 8 GiB by default on Amazon Linux.
  2. Use% is the number to watch. Near 100%, programs start failing with No space left on device.
  3. The tmpfs and devtmpfs lines live in RAM, not on the disk.
du vs df: one folder, or the whole diskdu = disk usage of what you name$ du -sh dir216K dir2$ du -sh *8.0K big.bin16K dir24.0K file1-s = summarize: one total per item-h = human-readable: K, M, GUse it to find what fills the diskdf = disk free, per filesystem$ df -h /Filesystem Size Used Avail Use%/dev/nvme0n1p1 8.0G 1.7G 6.4G 21%Size, Used, Avail and Use% of each disk/ is the root volume (8 GiB by default)Use% near 100% = time to clean up or growIgnore the small tmpfs lines: they are RAMFirst df -h to see if a disk is full, then du -sh * insidefolders to find what is big.

Figure 6. du answers "how big is this?" for the files and folders you name; df answers "how full is the disk?" for each filesystem. Check df -h first, then use du -sh * to find what is big.

Worked example: the disk is full.

  1. An app fails with No space left on device.
  2. df -h / shows Use% at 100%.
  3. sudo du -sh /var/* shows that /var/log is the biggest.
  4. sudo du -sh /var/log/* points to one huge log file. Clean or rotate it, or grow the EBS volume.

11. mv: move and rename

Why: Moving and renaming files is part of every setup, including moving a copied key into place.

mv file3 dir1
mv file2 notes.txt
mv dir2 dir1
  1. mv file3 dir1 moves file3 into dir1.
  2. mv file2 notes.txt renames it, because notes.txt is not an existing folder.
  3. mv moves folders without -r.
  4. A wrong name gives mv: cannot stat 'file99': No such file or directory.
  5. mv overwrites an existing file without asking; mv -i asks first.

The full chapter: Linux mv and rm.

12. Passwordless SSH: why servers need it

Why: in a real setup, one server sends work to the others. Ansible connects from a controller to every managed server over SSH. A Jenkins controller starts builds on agent servers over SSH. (Older documents call them master and slave.) If a person had to type a password for every login, nothing could be automated.

Classroom line

I'm tired; how much can I do alone?

Think of a team of four where three are on leave and one does all the work. A controller that can log in to every server spreads the work out, without a person in between.

The goal: linux1 logs in to linux2 and linux3 as ec2-user with no password, using a key pair that linux1 owns.

13. What goes where: keys, authorized_keys and port 22

Why: Most failed setups put a key on the wrong server or in the wrong file. Knowing exactly which file sits where avoids that.

Passwordless SSH: what goes where linux1 (the one that logs in) $ ssh-keygen -t ed25519 makes two files in ~/.ssh: id_ed25519 private key: never leaves linux1 id_ed25519.pub public key: one line, safe to copy copy the .pub line linux2 ~/.ssh/authorized_keys + the line from linux1 (append) chmod 700 ~/.ssh; 600 file linux3 ~/.ssh/authorized_keys + the same line from linux1 chmod 700 ~/.ssh; 600 file Security groups of linux2 and linux3: inbound SSH, TCP 22,from linux1 Source = linux1's private IP/32, or linux1's security group. Then: sshec2-user@<linux2 private IP>.

Figure 7. linux1 makes a key pair; the private key id_ed25519 stays on linux1, and the one-line public key id_ed25519.pub is appended to ~/.ssh/authorized_keys on linux2 and linux3. Their security groups allow TCP 22 from linux1.

  1. The key pair is made on linux1: id_ed25519 (private) and id_ed25519.pub (public), in ~/.ssh.
  2. The private key never leaves linux1. Anyone who has it can log in wherever its public key is trusted.
  3. The public key is one line. It goes at the end of ~/.ssh/authorized_keys of the user you will log in as on linux2 and linux3: /home/ec2-user/.ssh/authorized_keys.
  4. Permissions: ~/.ssh must be 700 and authorized_keys 600, owned by that user. SSH ignores the file when they are more open.
  5. Port 22: the security groups of linux2 and linux3 must allow inbound SSH, TCP 22 from linux1: its private IP with /32, or better, linux1's security group as the source. The launch wizard's default rule allows 22 from anywhere, so the lab works with it, but in real setups you narrow it.
  6. Log in as ec2-user, not root. On Amazon Linux 2023, root's authorized_keys already contains your launch key, wrapped in a command that only prints Please login as the user "ec2-user" rather than the user "root". and closes the connection, and password logins are off. Use ec2-user and sudo for admin work.

14. How: set it up step by step as ec2-user

Why: Tools like Ansible and Jenkins log in to servers with no one there to type a password. These steps are exactly what they need.

On linux1, make the key pair:

ssh-keygen -t ed25519
ls -la ~/.ssh
cat ~/.ssh/id_ed25519.pub
  1. Press Enter three times: the default file name, then an empty passphrase twice. (An empty passphrase lets scripts use the key. On a laptop, set a passphrase.)
  2. ls -la ~/.ssh now shows id_ed25519 with -rw------- and id_ed25519.pub with -rw-r--r--.
  3. cat prints the public key: one line starting ssh-ed25519 AAAA... and ending with a comment such as ec2-user@linux1. Select the whole line and copy it. (ssh-keygen with no -t on Amazon Linux 2023 makes an RSA key, id_rsa.pub, which works the same way.)

Classroom line

When copying, the first letter often gets missed; don't make that mistake.

On linux2, add it (log in there as usual with your .pem):

vi ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
  1. In vi, press G to go to the last line, o to open a new line below it, paste, press Esc, and type :wq.
  2. Don't delete the line that is already there. It is the public half of your .pem key; without it you lock yourself out of linux2.
  3. The chmod lines make sure the permissions are right. On EC2 they already are, but run them anyway; they are harmless.
  4. Repeat on linux3.

Classroom line

Don't fiddle with configuration files unnecessarily.

Check the security groups of linux2 and linux3: an inbound rule for SSH (22) from linux1's private IP or security group.

Back on linux1, test it:

ssh ec2-user@LINUX2_PRIVATE_IP
hostname
exit
ssh ec2-user@LINUX3_PRIVATE_IP 'df -h /'
  1. Use the private IP shown in the console (or hostname -I on linux2): the servers are in the same VPC, so the traffic stays inside AWS.
  2. The first time, SSH asks Are you sure you want to continue connecting (yes/no/[fingerprint])?. Type yes; the server's key is saved in ~/.ssh/known_hosts and you won't be asked again.
  3. No password prompt appears, and the prompt changes to linux2. exit brings you back to linux1.
  4. The last line runs one command on linux3 and prints the result on linux1: this is how tools like Ansible work.
How a key login works, without a password linux1 linux2 id_ed25519 authorized_keys private key linux1's public key 1. linux1 connects to linux2 on port 22 linux1 → linux2: connect to port 22 The security group must allow 22 from linux1, or it times out. 2. linux2 finds linux1's public key in authorized_keys linux2 → linux1: random challenge It sends back a random challenge to sign. 3. linux1 signs the challenge with its private key linux1 → linux2: signature only Only the signature travels. The private key never leaves linux1. 4. linux2 checks the signature with the public key It matches: a shell opens as ec2-user, with no password. $ whoami (on linux2) ec2-user $ exit (back to linux1)

Figure 8. Animation: linux1 connects to port 22, linux2 finds linux1's public key and sends a random challenge, linux1 signs it with the private key, and linux2 checks the signature with the public key and opens a shell. Only the signature travels.

ssh-copy-id can install a key for you, but only when you can already log in to the target. Between fresh EC2 servers you can't yet, so the copy and paste above is the way in.

15. The trust is one way

What: linux1 can log in to linux2 because linux1's public key is in linux2's authorized_keys. Nothing allows the opposite: ssh from linux2 to linux1 ends with Permission denied (publickey).

The trust is one way linux1 ssh works: linux1's key is inlinux2's file linux2 linux1 ssh works: same key in linux3'sfile linux3 linux2 → linux1: Permission denied (publickey) Only the login permission is one way. Once logged in, the session carriesdata both ways (you type, output comes back). For linux2 → linux1 too: ssh-keygen on linux2, and its .pub line into linux1'sauthorized_keys.

Figure 9. linux1 logs in to linux2 and linux3; linux2 to linux1 is denied. Only the login permission is one way; a session carries data both ways.

  1. The one-way part is the permission to log in. Once you are in, the session carries data both ways: you type, the output comes back.
  2. For linux2 to log in to linux1 too, run ssh-keygen on linux2 and put its public key into linux1's authorized_keys.

16. Task: four servers, both ways

The task: four servers, linux-1 to linux-4. Every server must log in to every other one without a password.

Four servers, both ways: 12 one-way pathslinux-1linux-2linux-3linux-4The countEach server logs in tothe other 3, not to itself:4 × 3 = 12(not 4 × 4 = 16)On each server:• 1 ssh-keygen (own key pair)• authorized_keys holds 3 lines: the other 3 .pub• SG: port 22 from the others

Figure 10. Four servers, every pair in both directions: 4 × 3 = 12 one-way paths. Each server has its own key pair, an authorized_keys file with the other three public keys, and port 22 open to the others.

Worked example: count the paths.

  1. Each server logs in to the other three, not to itself: 3 paths per server.
  2. 4 servers × 3 = 12 one-way paths, not 4 × 4 = 16.
  3. On each server: run ssh-keygen once, and put the other three public keys in its authorized_keys (3 lines added per server, 12 in total).
  4. Test from each server, for example from linux-1: ssh ec2-user@IP_OF_LINUX_2 hostname, then linux-3 and linux-4.
  5. The count grows fast: 10 servers both ways need 10 × 9 = 90 paths. That is why real setups use one controller (9 paths for 10 servers) and tools to hand out keys.

Classroom line

Do it once and you'll never forget it.

Classroom line

It's hard until you learn it; after that it's child's play.

17. Ports you should know

Why: Every security group rule and every connection error names a port. Knowing the common ones tells you at once which service is involved.

A port number runs from 0 to 65,535 (65,536 ports). Ports 0 to 1023 are the well-known ports for standard services (0 itself is reserved). How ports work is in Ports: the IP reaches the server, the port reaches the app.

Port Service Where you meet it
21 FTP Old file transfer, sends passwords in plain text
22 SSH Logging in to Linux servers; this chapter
25 SMTP Servers sending email
53 DNS Name lookups; Route 53 is named after this port
80 HTTP Websites without encryption
443 HTTPS Websites with encryption
3306 MySQL / MariaDB Databases

Classroom line

Your neighbour won't come to your interview; you'll have to answer yourself.

18. When something looks wrong

Why: Each error points to one link in the chain: network, user, key or permissions. Reading it correctly saves you from redoing the whole setup.

When ssh between servers fails port 22: Connection timed out → The security group of the target does not allow 22 from this server Permission denied (publickey) → Wrong user (root vs ec2-user), key line cut or broken over two lines, → ~/.ssh or authorized_keys too open (777, 666) or owned by root Are you sure ... (yes/no/[fingerprint])? → Normal on the first login: type yes; it is saved in ~/.ssh/known_hosts REMOTE HOST IDENTIFICATION HAS CHANGED → A new server reused the IP: ssh-keygen -R <IP>, then connectagain Debug from linux1 with ssh -v ec2-user@<IP>: it prints each step,including which key it offered.

Figure 11. A timeout on port 22 is the security group. Permission denied (publickey) is the wrong user, a broken key line, or permissions that are too open or a file owned by root. The fingerprint question is normal the first time. A changed host key after an IP is reused is fixed with ssh-keygen -R.

  1. Connection timed out on port 22: the target's security group doesn't allow 22 from this server. Add the rule.
  2. Permission denied (publickey):
    1. You used the wrong user: ec2-user on Amazon Linux, ubuntu on Ubuntu, not root.
    2. The key line lost its first characters, or was pasted across two lines. It must be one complete line.
    3. ~/.ssh is more open than 700, or authorized_keys is writable by others (such as 666): SSH refuses it.
    4. You created authorized_keys with sudo, so root owns it. Fix it with sudo chown ec2-user:ec2-user ~/.ssh/authorized_keys.
  3. REMOTE HOST IDENTIFICATION HAS CHANGED: a new server got an IP you used before. Run ssh-keygen -R THAT_IP on linux1, then connect again.
  4. Still stuck: ssh -v ec2-user@IP prints every step, including which key was offered.
  5. vi looks scrambled after zooming: press Ctrl+L, or quit with :q! and reopen.
  6. du shows 0 for a file: the file is empty. ls -l shows the size in bytes.

Ravindra Bagale's Tip

Treat a private key like the key to your house: it stays on the machine that made it, it is never pasted into chat or email, and it never goes into a Git repository. Move the public key around freely. If a private key ever leaks, delete its public line from every authorized_keys it was added to, and make a new pair.

19. The whole class on one page

  1. Launch three t3.micro servers with the same key pair and security group; wait for 3/3 status checks (system, instance, attached EBS).
  2. To move a server to another Region: create an AMI, copy it, launch from the copy, and set up the key pair and security group again there.
  3. cat > with Ctrl+D, >> to append, cat a b > c to join, tac to reverse, stat for the times (UTC on EC2), touch to create or update.
  4. vi: i or o to type, Esc, then :wq or :q!. nano: Ctrl+O, Ctrl+X.
  5. ls -l shows type, permissions, owner, size and time; ls -ltr puts the newest at the bottom; ls -la shows hidden files; ll is ls -l on Amazon Linux.
  6. mkdir -p makes a whole chain; cd ../.. goes up two levels; cd alone goes home; pwd tells you where you are.
  7. du -sh sizes what you name (-s summarize, -h human-readable); df -h shows how full each disk is.
  8. mv moves and renames, folders included.
  9. Passwordless SSH: ssh-keygen on linux1, its .pub line appended to ec2-user's authorized_keys on the others, 700/600, and port 22 open from linux1.
  10. The private key never leaves its server. Log in as ec2-user, not root.
  11. Trust is one way. Four servers both ways need 4 × 3 = 12 paths.
  12. Ports run from 0 to 65,535: 22 SSH, 53 DNS, 80 HTTP, 443 HTTPS.

20. Hands-on lab: passwordless SSH from linux1 to linux2

Why this lab. This lab sets up one-way trust from linux1 to linux2, checks every file and permission on the way, and proves it by running a command on linux2 without typing anything.

  1. Name the servers
    • Command: sudo hostnamectl set-hostname linux1
    • You should see: Nothing.
    • Check: After you log out and in again, the prompt shows ec2-user@linux1. Do the same on the second server with linux2.
    • Why: With identical servers, the prompt is the only way to know where you are. Running a key command on the wrong server is the most common mistake here.
  2. Check that linux1 reaches port 22 on linux2
    • Command: ssh ec2-user@LINUX2_PRIVATE_IP
    • You should see: Permission denied (publickey...), quickly, not a timeout.
    • Check: A fast "denied" means the network is fine; Connection timed out means the security group blocks port 22.
    • Why: Use the private IP: the servers talk inside the VPC. Testing the network first separates network problems from key problems.
  3. Make a key pair on linux1
    • Command: ssh-keygen -t ed25519
    • You should see: Three questions; press Enter for each. Then Your identification has been saved in /home/ec2-user/.ssh/id_ed25519.
    • Check: ls -la ~/.ssh shows id_ed25519 (-rw-------) and id_ed25519.pub.
    • Why: An empty passphrase is what makes it passwordless. The private key never leaves linux1; only the .pub file is shared.
  4. Copy the public key
    • Command: cat ~/.ssh/id_ed25519.pub
    • You should see: One line starting with ssh-ed25519 AAAA and ending with ec2-user@linux1.
    • Check: Select and copy the whole line.
    • Why: The whole line, in one piece, is the public key. A line broken in two doesn't work.
  5. Add it on linux2
    • Command: vi ~/.ssh/authorized_keys
    • You should see: The file opens with the key you launched with. Press o, paste the line, Esc, :wq.
    • Check: wc -l ~/.ssh/authorized_keys → 2
    • Why: Do this on linux2, using your laptop login to it. Keep the existing line, or your own laptop key stops working.
  6. Check the permissions on linux2
    • Command: chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
    • You should see: Nothing.
    • Check: ls -ld ~/.ssh starts with drwx------; ls -l ~/.ssh/authorized_keys with -rw-------.
    • Why: SSH ignores authorized_keys if others can write to it or its folder. These are usually already right; setting them rules it out.
  7. Log in from linux1
    • Command: ssh ec2-user@LINUX2_PRIVATE_IP
    • You should see: The first time: Are you sure you want to continue connecting (yes/no/[fingerprint])? Type yes. Then the prompt ec2-user@linux2.
    • Check: No password asked. Type exit to return to linux1.
    • Why: The yes saves linux2's identity in ~/.ssh/known_hosts, so later logins can detect a server that has been swapped.
  8. Run a command remotely
    • Command: ssh ec2-user@LINUX2_PRIVATE_IP 'df -h /'
    • You should see: linux2's disk usage, and you stay on linux1.
    • Check: The prompt still says linux1.
    • Why: This is what automation tools do: run commands on many servers from one, with no one typing.
  9. Prove the trust is one way
    • Do: On linux2, run ssh ec2-user@LINUX1_PRIVATE_IP.
    • You should see: Permission denied (publickey...)
    • Check: linux2 has no key of its own in linux1's authorized_keys.
    • Why: Each direction needs its own key pair and its own authorized_keys line. Terminate the servers when you finish.

21. Try at home

Try at home

Task 1: passwordless SSH

  1. Launch linux1, linux2 and linux3, and set each host name with hostnamectl.
  2. Set up passwordless SSH from linux1 to the other two as ec2-user, then run ssh ec2-user@LINUX2_PRIVATE_IP 'df -h /' from linux1.
  3. Try ssh from linux2 to linux1 and read the error. Then make it work too.

Task 2: break it on purpose

  1. On linux2, run chmod 666 ~/.ssh/authorized_keys and try again from linux1. Put it back to 600.
  2. Remove the security group rule for 22 and see the timeout. Add it back.

Task 3: commands

  1. Make dir2/dir3/dir4/dir5 in one command, go into dir5, then come back to dir3 with one cd.
  2. Run du -sh * in your home folder and df -h /. How much of the root volume is used?
  3. Create file7 and file8 a minute apart, and check their order with ls -lt and ls -ltr.

Recap

In short

  1. A server moves to another Region as an AMI copy; the key pair and security group are set up again there.
  2. ls -l, ls -ltr and ls -la answer "what is here, what changed last, and what is hidden".
  3. mkdir -p builds folder chains; cd ../.., cd and pwd move you around and tell you where you are.
  4. du -sh sizes files and folders; df -h shows how full the disk is.
  5. Passwordless SSH needs a key pair on the source server, its public key in ec2-user's authorized_keys on the target with 700/600, and port 22 open between them.
  6. The trust is one way, the private key stays put, and four servers both ways mean 12 paths.

Practice Tasks

About this scenario

Lisboa Tiles is a ceramic tiles business in Lisbon. João Silva looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-west-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 11

Easy Protect an SSH key file

Why: SSH refuses to use a key file that other users can read.

What to do: Make lisboatiles-key.pem readable only by its owner and nobody else (no write).

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Connect to an EC2 server

Why: SSH with a key file is the standard way to log in to a Linux EC2 instance.

What to do: Log in to the server 13.47.20.5 as ec2-user using the key lisboatiles-key.pem.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Port for SSH

Why: Security group rules need the right port.

What to do: Which TCP port must the security group open for SSH?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Check free disk space

Why: If a disk is 100% full, apps and databases stop working.

What to do: Show free and used space of all mounted disks in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check a folder's size

Why: du tells you how much space a folder uses, in easy units with -h.

What to do: Show the total size of /var/www/lisboatiles as one line in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check memory

Why: If RAM is full the server slows down or kills apps.

What to do: Show used and free memory in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Upload a file to the server

Why: scp copies files over SSH, so it uses the same key and user.

What to do: Upload index.html from your laptop to /home/ec2-user/ on 13.47.20.5 using lisboatiles-key.pem.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Copy your public key to another server

Why: After this, you can SSH to that server without a password.

What to do: Copy your public key to user ec2-user on 10.0.3.25.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Create an SSH key pair

Why: Passwordless SSH between servers needs a key pair on the first server.

What to do: Create a new SSH key pair of type ed25519.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Find big files

Why: When the disk is full, find large files first.

What to do: Find files bigger than 100 MB anywhere under /var.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Block other Regions (SCP style)

Why: Data rules (like keeping data in one country) are enforced with aws:RequestedRegion.

What to do: Deny ec2:* on * when aws:RequestedRegion is not eu-west-1.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: du vs df. du: Disk usage: space used by files and folders. df: Disk free: space of whole filesystems.
Comparison: du vs df · Download PNG
Comparison card: root vs ec2-user. root: The super user with full power. ec2-user: The default login user on Amazon Linux.
Comparison: root vs ec2-user · Download PNG
Revision card: Disk usage: du -sh and df -h. A full disk stops logs, uploads and databases. df -h shows free space on the whole disk. du -sh shows the size of one file or folder. df to see if the disk is full, du to find what filled it. Biggest log folder:
Revision card: Disk usage: du -sh and df -h · Download PNG
Revision card: Passwordless SSH with ssh-keygen. Scripts and admins must hop between servers without typing passwords or copying .pem files. ssh-keygen makes a key pair. The public key goes into authorized_keys on the target. Generate on A, append id_rsa.pub to B's ~/.ssh/authorized_keys, chmod 700 .ssh and 600 the file. From A:
Revision card: Passwordless SSH with ssh-keygen · Download PNG
Revision card: Sort with ls -lt and ls -ltr. You want the newest log or the file you just changed. -t sorts by time (newest first). -r reverses, so the newest is at the bottom. Use -ltr in long folders so the newest is near your prompt. Newest logs:
Revision card: Sort with ls -lt and ls -ltr · Download PNG
Revision card: Ports to know by number. Security groups and errors talk in port numbers. A port is a door number on a server for one service. Open only the ports you need in the SG. Common ports:
Revision card: Ports to know by number · Download PNG
Interview card: The disk is full. How do you find what filled it?. df -h confirms which disk; du -sh on folders finds the big one. Drill down folder by folder. Commands:
Interview card: The disk is full. How do you find what filled it? · Download PNG
Interview card: Why is passwordless SSH trust one way?. Only the server holding your public key trusts your private key. B trusting A says nothing about A trusting B. 4 servers all ways = 4 × 3 = 12 paths.
Interview card: Why is passwordless SSH trust one way? · Download PNG
Interview card: Passwordless SSH still asks for a password. What do you check?. Permissions: ~/.ssh must be 700 and authorized_keys 600, owned by the user. SSH ignores keys in files others can write. Fix:
Interview card: Passwordless SSH still asks for a password. What do you check? · Download PNG
Interview card: How do you move an EC2 to another Region?. Create an AMI, copy it to the new Region, launch from it there. AMIs are regional; a copy makes it available in the target. ap-south-1 › Copy AMI › us-east-1 › Launch.
Interview card: How do you move an EC2 to another Region? · Download PNG

More cards: Ravi Sir's Cards.