Add host to /etc/hosts

Hi. For some testing purposes I need to add a specific host to /etc/hosts file. Is it possible somehow?

sudo echo " example" >> /etc/hosts

Should do the trick

1 Like

Hi @nononoy,

You can use below command to add host to /etc/hosts file.

sudo echo " example" | sudo tee -a /etc/hosts



Hi @weide-zhou 

I add this to my yml file:

    - name: Add hosts to /etc/hosts
      run: |
        sudo echo " example.test" | sudo tee -a /etc/hosts
        sudo echo " example2.test" | sudo tee -a /etc/hosts

But I still getting errors when I run some request tests with capybara:

       Unable to load URL: http://example.test:34717 because of error loading http://example.test:34717: Unknown error

What I am doing wrong?

Thanks for your help.

Hi @elalemanyo , is a sample IP, please use another one which can be reached from your runner.

I’m not expert of capybara, i think you can ping example.test:34717 for a check.


I try using instead of your example ip 🙈 just and now capybara test runs 🎊

Thanks for your help!

Tried that, but got Permission denied (predictably?)

Run sudo echo "" >> /etc/hosts
  sudo echo "" >> /etc/hosts
  shell: /bin/bash -e {0}
/home/runner/work/_temp/ line 1: /etc/hosts: Permission denied
##[error]Process completed with exit code 1.

The YML code was:

- run: sudo echo "" >> /etc/hosts

This worked:

- run: echo "" | sudo tee -a /etc/hosts
1 Like