XK0-006 System Management Practice Question
An administrator needs to replace all occurrences of 'oldhost' with 'newhost' in the file /etc/hosts. Which sed command should be used?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
sed -i 's/oldhost/newhost/g' /etc/hosts
sed -i 's/oldhost/newhost/g' /etc/hosts performs an in-place substitution globally.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
sed -e 's/oldhost/newhost/' /etc/hosts
Why it's wrong here
Without the g flag, sed replaces only the first occurrence per line, leaving later 'oldhost' entries unchanged. It is tempting because the substitution syntax and file target are correct, but global replacement across every occurrence on each line requires the s/oldhost/newhost/g form.
- ✗
sed -n 's/oldhost/newhost/gp' /etc/hosts
Why it's wrong here
The -n flag suppresses automatic printing, so only substituted lines print, and it never writes changes back to /etc/hosts. It is tempting because the g flag correctly targets every occurrence, which is right when printing matches rather than editing the file in place.
- ✗
sed 's/oldhost/newhost/' /etc/hosts
Why it's wrong here
Omitting the g flag replaces only the first match per line, leaving later occurrences of oldhost untouched in /etc/hosts. It is tempting because the same syntax works for single-match substitutions, which is the correct choice when only one instance per line needs changing.
- ✓
sed -i 's/oldhost/newhost/g' /etc/hosts
Why this is correct
The `-i` flag edits /etc/hosts in place, satisfying the requirement to replace content within that file directly. The `g` suffix applies the substitution to every occurrence per line, not merely the first, matching "all occurrences". Without `g`, only the initial match on each line would change.
Go deeper
Related to this question
About these practice questions
Courseiva writes every XK0-006 question from scratch — 781 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.