Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Also remember that -i can take a parameter to append to the backup file. Without any option, it will just overwrite the file without keeping a backup. For example,
Lots of comments bemoan the manpages - I use them as the first reference always. Then "info" in need.
Mind you, some could do with polish for clarity - need I mention "man find" ?.
Always be as precise as possible!
A step to more precision is
Code:
sed -i.orig 's|\<debian10\>|mybox|g' /etc/hosts
Now debian10 is substituted but not debian100
Recent GNU Linux also takes (incorporated from PCRE)
Code:
sed -i.orig 's|\bdebian10\b|mybox|g' /etc/hosts
In case /etc/hosts is a link (true on Solaris but not yet Linux), a copy changes the data but leaves the inode (the linkage, the owner, the permissions) intact
Code:
cp -p /etc/hosts /etc/hosts.bak && sed 's|\<debian10\>|mybox|g' /etc/hosts.bak > /etc/hosts
The sed -i takes care about owner and permissions as well - but breaks the link.
Last edited by MadeInGermany; 02-13-2021 at 07:25 AM.
While perhaps not directly applicable to the question at hand, I prefer using specialized tools wherever possible.
Code:
$ hostctl -h
hostctl is a CLI tool to manage your hosts file with ease.
You can have multiple profiles, enable/disable exactly what
you need each time with a simple interface.
Usage:
hostctl [command]
Available Commands:
add Add content to a profile in your hosts file.
backup Creates a backup copy of your hosts file
disable Disable a profile from your hosts file.
enable Enable a profile on your hosts file.
help Help about any command
list Shows a detailed list of profiles on your hosts file.
remove Remove a profile from your hosts file.
replace Replace content to a profile in your hosts file.
restore Restore hosts file content from a backup file.
status Shows a list of profile names and statuses on your hosts file.
sync Sync some system IPs with a profile.
toggle Change status of a profile on your hosts file.
Flags:
-c, --column strings Column names to show on lists. comma separated
-h, --help help for hostctl
--host-file string Hosts file path (default "/etc/hosts")
--no-color force colorless output
-o, --out string Output type (table|raw|markdown|json) (default "table")
-q, --quiet Run command without output
--raw Output without borders (same as -o raw)
-v, --version version for hostctl
Use "hostctl [command] --help" for more information about a command.
Code:
$ hostess -h
An idempotent tool for managing /etc/hosts
Commands
fmt Reformat the hosts file
add <hostname> <ip> Add or overwrite a hosts entry
rm <hostname> Remote a hosts entry
on <hostname> Enable a hosts entry
off <hostname> Disable a hosts entry
ls List hosts entries
has Exit 0 if entry present in hosts file, 1 if not
dump Export hosts entries as JSON
apply Import hosts entries from JSON
All commands that change the hosts file will implicitly reformat it.
Flags
-n will preview changes but not rewrite your hosts file
Configuration
HOSTESS_FMT may be set to unix or windows to force that platform's syntax
HOSTESS_PATH may be set to point to a file other than the platform default
About
Copyright 2015-2020 Chris Bednarski <chris@cbednarski.com>; MIT Licensed
Portions Copyright the Go authors, licensed under BSD-style license
Bugs and updates via https://github.com/cbednarski/hostess
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.