XK0-006 System Management Practice Question
An administrator needs to replace all occurrences of 'oldhost' with 'newhost' in the configuration file /etc/hosts. Which command will perform the replacement and save the changes directly to the file?
⚠ Common exam trap
The trap here is that candidates often forget the `-i` flag for in-place editing, assuming `sed` modifies the file by default, or they confuse `sed`'s stream behavior with editors like `vim` that directly change the file.
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
The `-i` flag (in-place editing) tells `sed` to write the changes directly back to the file specified. Without `-i`, `sed` only prints the modified output to stdout and does not alter the original file. The substitution command `s/oldhost/newhost/g` performs a global replacement of all occurrences of 'oldhost' with 'newhost' on each line.
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 's/oldhost/newhost/g' /etc/hosts
Why it's wrong here
This prints the result to stdout but does not modify the file.
- ✗
awk '{gsub(/oldhost/,"newhost")}1' /etc/hosts
Why it's wrong here
This prints to stdout but does not modify the file.
- ✗
grep -r 'oldhost' /etc/hosts | sed 's/oldhost/newhost/g'
Why it's wrong here
This does not modify the file and is incorrect syntax.
- ✓
sed -i 's/oldhost/newhost/g' /etc/hosts
Why this is correct
Correct: -i edits file in-place, s/oldhost/newhost/g replaces all occurrences.
Go deeper
Related to this question
About these practice questions
Courseiva writes every XK0-006 question from scratch — 979 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.