Skip to content

Fix set-fqdn to preserve localhost entries in /etc/hosts#1076

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-hosts-file-commenting
Draft

Fix set-fqdn to preserve localhost entries in /etc/hosts#1076
Copilot wants to merge 3 commits intomainfrom
copilot/fix-hosts-file-commenting

Conversation

Copy link
Contributor

Copilot AI commented Feb 10, 2026

Fix /etc/hosts handling in node/set-fqdn action

Changes Made

  • Understand the current sed command behavior
  • Modify the script to:
    • Skip commenting lines with IP 127.0.0.1 or ::1 that contain "localhost", "localhost4", or "localhost6"
    • Remove the old hostname from those specific localhost lines instead (completely, without extra spaces)
    • Still comment out other lines containing the old hostname
  • Test the changes manually with comprehensive test cases
  • Address review feedback to completely remove hostname without replacement
  • Run security scanning (shellcheck, bash syntax validation)

Technical Details

Modified core/imageroot/var/lib/nethserver/node/actions/set-fqdn/20set_fqdn:

  • Changed sed pattern to use \s+ to match one or more spaces
  • Added space normalization step s/\s+/ /g to collapse multiple spaces
  • This completely removes the old hostname without leaving extra spaces

Example (from problem statement)

Before:

127.0.0.1   nethvoice localhost localhost.localdomain localhost4 localhost4.localdomain4

After (now correctly removes hostname without extra spaces):

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

Testing

All comprehensive tests pass:

  • ✓ Hostname completely removed from localhost lines
  • ✓ No double/extra spaces in the result
  • ✓ Non-localhost entries properly commented
  • ✓ Shellcheck passes
  • ✓ Bash syntax validation passes
Original prompt

The node/set-fqdn action must not comment the /etc/hosts line that has IP 127.0.0.1 or ::1 and name "localhost" or "localhost4" or "localhost6". Instead it must clear it from the old host name if such line matches it.

 127.0.0.1   nethvoice localhost localhost.localdomain localhost4 localhost4.localdomain4

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: DavidePrincipi <2920838+DavidePrincipi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix /etc/hosts line handling for localhost Fix set-fqdn to preserve localhost entries in /etc/hosts Feb 10, 2026
Copilot AI requested a review from DavidePrincipi February 10, 2026 14:41
# Comment old hostname records in /etc/hosts
# For localhost lines (127.0.0.1 or ::1), remove old hostname instead of commenting the line
sed -i -E "/^(127\.0\.0\.1|::1)\s+.*\s(localhost|localhost4|localhost6)(\s|$)/{
s/\s${old_hostname}\s/ /g
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot is this substitution adding a space " " to the matching line? The old hostname must be completely removed (without replacement).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. The previous implementation was replacing with a space. Fixed in commit f987503 to completely remove the hostname using s/\s+${old_hostname}\s+/ /g and added s/\s+/ /g to normalize any multiple spaces into single spaces.

Co-authored-by: DavidePrincipi <2920838+DavidePrincipi@users.noreply.github.com>
@DavidePrincipi
Copy link
Member

Refs NethServer/dev#7856

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants