Table of Contents
Find the location of the hosts file
Edit hosts with administrator permissions
Notes and FAQs
Home System Tutorial LINUX how to edit the hosts file in linux

how to edit the hosts file in linux

Jul 21, 2025 am 03:02 AM

To modify the hosts file of the Linux system, you need to edit it with administrator permissions. 1. The hosts file is located in the /etc/ directory, and cat /etc/hosts can be viewed; 2. Use sudo to modify it with editors such as nano/vim/gedit, and the format is the IP address and the host name; 3. After modification, it may need to clear the DNS cache or restart the NetworkManager service to take effect; 4. Pay attention to the correct format, avoid frequent modifications, and commenting indeterminate entries to operate safely.

how to edit the hosts file in linux

It is actually not difficult to modify the hosts file in the Linux system, but many people will be a little hesitant when operating the first time, mainly because they are afraid that the system file will affect network access. In fact, just follow the correct method, editing hosts is a simple and practical little skill.

how to edit the hosts file in linux

Find the location of the hosts file

The hosts files of Linux systems are usually placed in the /etc/ directory, and the full path is /etc/hosts . It is a plain text file that can be opened with any text editor, such as nano, vim, or gedit.

You can enter the following commands to view the hosts content of the current system through the terminal:

how to edit the hosts file in linux
 cat /etc/hosts

Usually you will see something like the following:

 127.0.0.1 localhost
::1 localhost

This is the default local loopback address setting of the system.

how to edit the hosts file in linux

Edit hosts with administrator permissions

Because hosts is a system-level configuration file and ordinary users do not have write permissions, they must use sudo to obtain temporary administrator permissions. For example, using the nano editor can do this:

 sudo nano /etc/hosts

If you are more used to using vim, change to:

 sudo vim /etc/hosts

You can also use gedit in a graphical interface environment:

 sudo gedit /etc/hosts
  • When adding a new entry, the format is: IP address one or more spaces/tab hostname (multiple)

  • For example, if you want to point a website to the local testing environment, you can add a line:

     127.0.0.1 example.test
  • If you want to block a website, you can point it to 127.0.0.1:

     127.0.0.1 badsite.com

Remember to confirm that the syntax is correct before saving, do not have more spaces or fewer spaces, otherwise it may cause parsing errors.

Notes and FAQs

Not all programs will take effect immediately after editing hosts. Some applications (such as browsers) may cache DNS records, and at this time, you need to clear the cache or restart the service.

  • On Ubuntu, you can try restarting NetworkManager:

     sudo systemctl restart NetworkManager
  • Or use the nscd tool to clear the cache:

     sudo nscd -i hosts

    However, not all systems have nscd installed, so if you don't install it by default, you don't have to worry too much.

    Also note that:

    • Modifying hosts is only valid for this machine and does not affect other devices.
    • It is not recommended to frequently modify hosts, especially on production environment servers, as it can easily cause confusion.
    • If you are not sure about the function of a certain line, it is best not to delete it randomly. You can comment out first (add # at the beginning of the line) and try it.

    Basically that's it. Although the operation is not complicated, it is easy to ignore permissions and format details, and you can do it smoothly with a little attention.

    The above is the detailed content of how to edit the hosts file in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1511
276
System requirements to install linux System requirements to install linux Jul 20, 2025 am 03:49 AM

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

How to Securely Erase a Hard Drive on Linux How to Securely Erase a Hard Drive on Linux Jul 24, 2025 am 12:08 AM

Confirm the target hard disk device name (such as /dev/sda) to avoid accidentally deleting the system disk; 2. Use sudoddif=/dev/zeroof=/dev/sdXbs=1Mstatus=progress to overwrite the zero value in full disk, which is suitable for most scenarios; 3. Use sudoshred-v-n3/dev/sdX for three random data overwrites to ensure that it cannot be restored; 4. Optionally execute sudobadblocks-wsv/dev/sdX for destructive write tests; finally use sudohexdump-C/dev/sdX|head to verify whether it is all zero and complete safe erasing.

how to add a user in linux how to add a user in linux Jul 21, 2025 am 03:32 AM

Add useradd or adduser commands commonly used by users in Linux. 1. When using useradd, you need to manually set the password and home directory. Add the -m parameter to create the home directory; 2. You can specify the shell, group and UID through parameters such as -s, -G, and -u; 3. Adduser is an interactive command, suitable for novices to automatically complete the configuration; 4. Pay attention to permissions, username uniqueness and home directory permissions; 5. Userdel can be used to delete users and home directory by mistake. Mastering these key points allows you to manage users efficiently and securely.

How to get help for a command in Linux? How to get help for a command in Linux? Jul 17, 2025 am 12:55 AM

There are four ways to obtain command help in Linux: First, use --help to view basic usage, which is suitable for quickly understanding common options and parameters of commands; second, use man to view the complete man page, providing detailed command descriptions and examples; third, use info to view structured help, which is suitable for information navigation of complex commands such as gcc and make; fourth, refer to network resources and communities, such as Linux China, StackOverflow and other platforms to obtain Chinese materials or solve specific problems. It is recommended for beginners to master it step by step from --help and man.

How to check my Linux system information and version? How to check my Linux system information and version? Jul 26, 2025 am 02:59 AM

If you want to know the Linux system information and version, you can use the following steps: 1. Use lsb_release-a to view the distribution information; 2. View the /etc/os-release file to obtain version details; 3. Use uname-r or uname-mrs to confirm the kernel version and system architecture; 4. Run lscpu, free-h or dmidecode (requires root permissions) to obtain hardware information. The above method is applicable to different distributions, and some commands may require installation or permission adjustment.

LFCA: Understanding Linux Operating System – Part 1 LFCA: Understanding Linux Operating System – Part 1 Jul 17, 2025 am 09:14 AM

The Linux Foundation has unveiled a new pre-professional IT certification known as the Linux Foundation Certified IT Associate (LFCA). This is a new entry-level certification that focuses on testing fundamental IT concepts such as basic systems admin

Different Ways to Use Column Command in Linux Different Ways to Use Column Command in Linux Jul 17, 2025 am 09:20 AM

Have you ever found yourself needing to process CSV files and generate output in a structured table format? Not too long ago, I was dealing with a messy file that wasn't properly formatted. It had excessive spaces between each column, and I needed to

how to edit the hosts file in linux how to edit the hosts file in linux Jul 21, 2025 am 03:02 AM

To modify the hosts file of the Linux system, you need to edit it with administrator permissions. 1. The hosts file is located in the /etc/ directory, and cat/etc/hosts can be viewed; 2. Use sudo to modify it with editors such as nano/vim/gedit, and the format is the IP address and the host name; 3. After modification, it may need to clear the DNS cache or restart the NetworkManager service to take effect; 4. Pay attention to the correct format, avoid frequent modifications, and comment on uncertain entries to operate safely.

See all articles