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
Without the -i flag, sed writes the transformed lines to standard output, leaving /etc/hosts unchanged on disk. The in-place edit is what the task demands; plain substitution suits piping results elsewhere or previewing changes before committing them.
- ✗
awk '{gsub(/oldhost/,"newhost")}1' /etc/hosts
Why it's wrong here
awk prints the substituted lines to standard output only; it never writes back to /etc/hosts, so the file stays unmodified. This pattern is correct when reformatting or filtering text into a new file or pipeline rather than editing in place.
- ✗
grep -r 'oldhost' /etc/hosts | sed 's/oldhost/newhost/g'
Why it's wrong here
grep merely selects matching lines and pipes them to sed, so only those lines reach standard output and the file is never rewritten. This pipeline suits searching and previewing matches, not performing an in-place edit across the whole file.
- ✓
sed -i 's/oldhost/newhost/g' /etc/hosts
Why this is correct
The `-i` flag makes sed edit /etc/hosts in place, satisfying the requirement to save changes directly to the file. The `g` suffix replaces every occurrence of 'oldhost' on each line, not merely the first, meeting the "all occurrences" constraint without redirection or a temporary file.
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.