Harden SSH configuration using precise
sed
substitutions against a test
sshd_config
file. You will disable root login, disable password
authentication, and change the SSH port using CLI-only edits.
Your task is to harden SSH settings using the
sed
command. You have a sample SSH daemon configuration at
/tmp/sshd_config
, and you must apply three changes:
PermitRootLogin
to
no
,
PasswordAuthentication
to
no
, and the SSH
Port
to
2222
.
This lab reflects a standard production workflow: assess the current state, apply targeted configuration changes, and verify the results. In a live environment, you would also validate the syntax and restart sshd, but the focus here is on safe and controlled text manipulation.
cat
.
sed
substitutions to change specific directives.
/tmp/sshd_config
.
sed -i
for in-place edits.
^
to avoid accidental matches.
cat /tmp/sshd_config
Confirm the current directive values before modifying anything. This prevents you from “hardening” the wrong file or guessing the current state.
# SSH Daemon Configuration
PermitRootLogin yes
PasswordAuthentication yes
Port 22
PermitRootLogin
to
no
.
sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /tmp/sshd_config
The pattern is anchored with
^
so only the directive line starting with
PermitRootLogin
is changed.
PasswordAuthentication
to
no
.
sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /tmp/sshd_config
This performs a targeted substitution for the
PasswordAuthentication
directive only.
2222
.
sed -i 's/^Port 22/Port 2222/' /tmp/sshd_config
This updates the
Port
directive line from
22
to
2222
without touching other lines.
PermitRootLogin no
PasswordAuthentication no
Port 2222
cat <file>
: Prints file contents to standard output.
sed -i 's/pattern/replacement/' <file>
: Replaces the first match of
pattern
on each line and writes changes back to the file.
-i
: Edit the file in-place (modify the file directly).
^
: Anchors the match to the start of the line.
s/old/new/
: Substitution operator (replace
old
with
new
).
/tmp/sshd_config
: A temporary test configuration file used in this lab.
PermitRootLogin
: Controls whether the root user can log in over SSH.
PasswordAuthentication
: Controls whether SSH allows password-based logins.
Port
: Specifies the TCP port where
sshd
listens for incoming connections.