问题
In my google cloud compute engine, the /etc/hosts file is overwritten automatically after a certain time.
Please suggest to me why it is happening and how to prevent it.
Initially the entries are as below:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.128.0.2 instance-1.c.concrete-craft-123421.internal instance-1 # Added by Google
and I have changed it to as below to start cloudera services.
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#10.128.0.2 instance-1.c.concrete-craft-123421.internal instance-1 # Added by Google
10.128.0.2 instance-1 instance-1
But after sometime, the original entries reappear. Any idea what is causing this?
Thanks,
Nanda
回答1:
What you can do instead of this
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#10.128.0.2 instance-1.c.concrete-craft-123421.internal instance-1 # Added by Google
10.128.0.2 instance-1 instance-1
is too add your line
10.128.0.2 instance-1 instance-1
before the google line. And no need to have cron ;) it won't be deleted !
Do like this ->
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.128.0.2 instance-1 instance-1
#10.128.0.2 instance-1.c.concrete-craft-123421.internal instance-1 # Added by Google
回答2:
On Google Compute Engine instance there are Google-daemons which runs in the background. These daemon services are responsible for the following services:
1. Creates new accounts based on the instance metadata.
2. Configures SSH to accept the accounts' public keys from the instance metadata.
3. Adds IP addresses of network load balancers as aliases of the external Ethernet interface
4. Resyncs clock if skewed due to live migration
The /etc/hosts is manged and overwritten by these daemon services. As such, if you wish to permanently change the hostname of the instance, you can do so by creating a cron job on the instance. You can go through this video which has the steps to create a cron job.
回答3:
I faced the same issue on RHEL7 + NetworkManager and this is how I prevented it to happen again:
tee /etc/dhcp/dhclient.d/1-google_hostname_off.sh << EOF
#!/bin/bash
# Block hosts changes by google-compute-engine
google_hostname_config() {
:;
}
readonly -f google_hostname_config
EOF
chmod +x /etc/dhcp/dhclient.d/1-google_hostname_off.sh
The reason is the google-compute-engine package places a script here: /etc/dhcp/dhclient.d/google_hostname.sh This script is getting executed on all reboot, dhcp request/renewal and executes this shell script: /usr/bin/google_set_hostname. What I did above is basically creates an empty function earlier with the same name like in the script made by Google but it's empty and read-only. This way:
- the google-compute-engine package will be updatable (you don't need to delete anything which would be recreated later),
- you don't need to set an immutable flag (like on /etc/hosts)
- no error will be thrown, only a few simple messages like:
nm-dispatcher[922]: /etc/dhcp/dhclient.d/google_hostname.sh: line 18: google_hostname_config: readonly function
I know, it's a pretty late answer but maybe it will be helpful for someone.
来源:https://stackoverflow.com/questions/35762778/google-cloud-compute-engine-etc-hosts