缺失/etc/hosts文件——如何在Ubuntu中创建新的hosts文件?
Hey there, it's definitely odd that your Ubuntu system doesn't have a /etc/hosts file—even more so that your LAMPP installation is missing it too. Let's get this sorted out with a straightforward, step-by-step guide:
1. Create & Edit the Hosts File via Terminal
Since /etc is a system directory, you'll need root privileges to create and edit the file. The easiest tool for beginners is nano (no complex shortcuts to remember):
- Run this command to open a new hosts file in nano:
sudo nano /etc/hosts - Paste the sample content from the section below into the editor.
- Save and exit: Press
Ctrl+O, hit Enter to confirm the filename, then pressCtrl+X.
If you prefer using vim instead, run:
sudo vim /etc/hosts
- Press
ito enter insert mode, paste the sample content, then pressEsc, type:wq, and hit Enter to save and exit.
2. Sample Hosts File Content
Here's a standard, functional hosts file you can use as a base—make sure to replace your-hostname with your actual system hostname (you can check it by running hostname in the terminal):
127.0.0.1 localhost 127.0.1.1 your-hostname # IPv6 configuration (leave this in if your system supports IPv6) ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
3. Set Correct Permissions
For the hosts file to work properly, it needs the right ownership and permissions. Run these two commands to fix that:
sudo chown root:root /etc/hosts sudo chmod 644 /etc/hosts
This ensures only the root user can modify the file, while all users can read it—matching Ubuntu's default security settings.
4. Verify the Setup Works
To make sure everything is working, test with a simple ping command:
ping localhost
You should see responses coming from 127.0.0.1. If you added a custom entry (like mapping a domain to an IP), ping that domain to confirm it resolves correctly.
内容的提问来源于stack exchange,提问作者kerry

