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?

3 Likes
sudo echo "xxx.xxx.xxx.xxx www.example.com 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 "172.16.18.16 www.example.com example" | sudo tee -a /etc/hosts

addhost.png

2 Likes

Hi @weide-zhou 

I add this to my yml file:

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

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

Capybara::Webkit::InvalidResponseError:
32
       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 ,

172.16.18.16 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.

Thanks.

@weide-zhou 
I try using instead of your example ip 🙈 just 0.0.0.0 and now capybara test runs 🎊

Thanks for your help!

Tried that, but got Permission denied (predictably?)

Run sudo echo "127.0.0.1 local.civility.social" >> /etc/hosts
  sudo echo "127.0.0.1 local.civility.social" >> /etc/hosts
  shell: /bin/bash -e {0}
/home/runner/work/_temp/3420f070-1489-4e36-8b7a-407078f6101e.sh: line 1: /etc/hosts: Permission denied
##[error]Process completed with exit code 1.

The YML code was:

- run: sudo echo "127.0.0.1 local.civility.social" >> /etc/hosts

This worked:

- run: echo "127.0.0.1 local.civility.social" | sudo tee -a /etc/hosts
1 Like