Toggle Hosts is a simple command-line tool that automates switching between localhost and remote IP entries in your /etc/hosts file. It eliminates the need for manual editing and makes environment switching seamless.
-
Quick Environment Switching: Easily toggle between
localhost(127.0.0.1) andremote IPfor specific domains. -
Current Environment Detection: Run without arguments to see whether you’re in local or remote mode.
-
Automated Sudo Privileges: If you forget to run the script with
sudo, it will prompt for the password and rerun itself.
-
Download the script:
$ curl -o ~/bin/toggle_hosts https://raw.githubusercontent.com/ulasarican/toggle-hosts/main/toggle-hosts -
Make it executable:
$ chmod +x ~/bin/toggle_hosts -
Add the script to your PATH (if ~/bin is not already in your PATH):
• For zsh users:
$ echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc $ source ~/.zshrc
• For bash users:
$ echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc $ source ~/.bashrc
-
Show Current Environment
To check which environment is active (local or remote):
$ toggle_hosts
Output Example:
$ Current Environment: LOCAL (localhost is active).
-
Switch to local
Activate the localhost entries in the /etc/hosts file:
$ toggle_hosts localOutput
Switched to LOCAL mode (localhost is active). Hosts file updated successfully.
-
Switch to remote
Activate the remote entries in the /etc/hosts file:
$ toggle_hosts remote
Output
Switched to REMOTE mode (remote IP is active). Hosts file updated successfully.
• The script requires root privileges to edit the /etc/hosts file.
• If you forget to run it with sudo, the script will automatically re-run itself and prompt for your password.
You can modify the script to fit your domain names or IP addresses. Simply replace the following lines in the script:
LOCAL_LINE="127.0.0.1 your-domain.com"
REMOTE_LINE="x.x.x.x your-domain.com"Contributions are welcome! If you find a bug, need additional features, or want to improve the script, feel free to submit a pull request.
-
Fork this repository.
-
Create a branch (feature/my-feature).
-
Commit your changes.
-
Submit a pull request.
If you have any feedback, suggestions, or issues, please open an issue in this repository.
This project is licensed under the MIT License. See the LICENSE file for details.