How does `sed` command modify files?

QuestionsQuestions8 SkillsProDec, 19 2025
0149

That's a great question! sed stands for Stream Editor. It is a very powerful tool used to parse and transform text in a file or from a command's output.

In the command you are about to run:

sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config

Here is the breakdown of how it modifies the file:

1. The -i flag (In-place)

By default, sed just prints the modified text to your screen and doesn't change the actual file. The -i flag tells sed to edit the file "in-place", meaning it saves the changes directly back into /etc/ssh/sshd_config.

2. The s command (Substitute)

The s at the beginning of the expression stands for substitute. It follows a specific format:
's/find_this/replace_with_this/'

  • PasswordAuthentication no: This is the pattern sed searches for in the file.
  • PasswordAuthentication yes: This is what sed will put in its place once it finds a match.

3. How it processes the file

  1. sed reads the file line by line.
  2. It looks at a line and checks: "Does this line contain 'PasswordAuthentication no'?"
  3. If no, it moves to the next line.
  4. If yes, it replaces that specific text with 'PasswordAuthentication yes'.
  5. Because of the -i flag, it writes these changes back to the disk.

Why use sed instead of a text editor?

In Linux administration and cybersecurity, sed is preferred for these tasks because:

  • Speed: It’s much faster than opening a file in nano or vim, searching for a line, and typing.
  • Automation: You can put this command in a script to configure 1,000 servers instantly!

Pro Tip: If you ever want to be safe, you can use sed -i.bak .... This will create a backup of the original file (named sshd_config.bak) before making the changes!

Give the command a try now, and then you can restart the SSH service to apply the update

0 Comments

no data
Be the first to share your comment!