Change hostname permanently in Google Compute Engine instance after reboot - google-cloud-platform

I've created a Google instance in Google Compute Engine with CentOS operating system, then I installed Cpanel. My problem is with WHM/Cpanel, it needs a hostname to be FQDN hostname, specifically for updating Cpanel or it will fail.
My problem is that after changing the hostname the instance reverts back to the old hostname after rebooting the operating system or resetting/stopping/starting the instance.
I've checked most questions before and I've tried most of the solutions with no luck. It keeps changing after reboot, I've try all the methods below and more:
create sh script in:
change hostname in
edit file
then add inside it, for my network interface:
supersede host-name ""
in crontab add to the end:
#reboot hostname=""; sed -i "s/.*Google.*//" /etc/hosts; hostname "$hostname"
But after reboot, the hostname changes back to the instance name.
Is there any other workaround to permanently change my hostname even after reboot.?

You could create a similar crontab entry, but instead of using the line in your post, you could use hostnamectl to set the hostname on start-up.
I've tested this with Google's Centos7 and Debian9 images and it works for both. However, I found that with Centos, I had to add a delay before the commands execution (see below).
So for example, open crontab:
sudo crontab -e
Then enter this line for Centos:
#reboot sleep 15 && hostnamectl set-hostname YOUR_HOSTNAME
For Debian this worked:
#reboot hostnamectl set-hostname YOUR_HOSTNAME
I didn't experiment too much with the crontab Centos timings (you may be able to use a lower figure than 15 seconds), but from my experience, using #reboot alone didn't seem to initiate the change on start-up.


Recover 'sshd_config' file in CentOS server

I have a 10 node cluster. Out of which, I was working on of the node(which is not a master node). I was trying to configure IPython and had made some changes to sshd_config file. To make those changes come into effect, I ran this command:
sudo service sshd restart
From then,I was not able to login into that machine. I tried connecting from the other 9 nodes, but in vain. (I tried ssh user#IP)
I don't know what happened.
I have searched for recovering the file..but, all the solutions are when we are able to login. But here, we are not even able to login into machine..then how come I change there any way that I can do from the other 9 nodes.....something like...apply the same sshd_config of the remaining 9 nodes to the damaged node,please help..and BTW..the 10 node cluster is AWS EC2 instance.
It's possible that there is an error in the sshd_config file that prevents sshd from starting up successfully. Normally a sudo service sshd restart will check for errors before restarting (using sshd -t), but it doesn't seem like that happened.
It sounds like you've lost your connection to this server, and since sshd now won't start up you're locked out.
If this EC2 instance is EBS-backed, you can fix this by mounting the root volume on another instance, editing the sshd_config file to fix the error, and restarting the original instance. The procedure described in this question is basically what you need to do.

How do I fix `no_running_cluster_nodes` when clustering RabbitMQ?

I'm trying to cluster two RabbitMQs.
I have two machines (rabbit1 & rabbit2) with Debian Wheezy running rabbitmq-server 2.8.4-1. (I know that's old, but that's what comes with Debian Wheezy.)
On rabbit1 I run...
# rabbitmqctl stop_app
Stopping node rabbit#rabbit1 ...
# rabbitmqctl reset
Resetting node rabbit#rabbit1 ...
# rabbitmqctl cluster rabbit#rabbit2
Clustering node rabbit#rabbit1 with [rabbit#rabbit2] ...
Error: {no_running_cluster_nodes,[rabbit#rabbit2],[rabbit#rabbit2]}
But I don't get past this error. What am I missing?
Things I checked:
I'm using the same cookie in /var/lib/rabbitmq/.erlang.cookie on both machines.
I added entries to /etc/hosts and can ping each machine from the other (using only rabbit1, resp. rabbit2 as a name).
Iptables are empty.
From rabbit1 I verfied with nmap that epmd on rabbit2 is listening on 4369.
root#rabbit1:~# nmap -p 1- rabbit2
4369/tcp open epmd
5672/tcp open amqp
51629/tcp open unknown
Solution: (Cheating)
It was neither the erlang cookie, nor the hostnames, nor the firewall. Actually I still don't know what it was. But I finally upgraded to RabbitMQ 3.5.4 by installing the Debian package provided by RabbitMQ. (I know this is cheating.) After the upgrade everything worked as expected with no further changes to my setup.
I ran into this issue as well, turned out to be my .erlang.cookie even though I was sure I'd set them to the same value on both machines (always good to double check).
It's possible that another .erlang.cookie is being used, depending on how rabbitmq is started. If it's been started with the init script, then /var/lib/rabbitmq/.erlang.cookie should be correct. If you started rabbitmq like this, rabbitmq-server -detached, then it may be in the user's home directory.
You can check the cookie hash rabbitmqctl is using (will be the same as rabbitmq-server if it can connect when you don't supply a -n flag) by running rabbitmqctl -n notarealnode status which will print out diagnostics.
Hostnames can also be tricky, as both the local node and the remote node need to identify each other and themselves with the same names. In other words, rabbit1's
Also, you didn't mention where you're running this, but mine are in an EC2 VPC which means I had to set the Security Group to allow traffic between the nodes. If you're not in EC2, then maybe there is some other sort of external firewall that needs configuration.

How could I make my perl script add a server's key to SSH's list?

I have a very annoying problem that has left me scratching me head when my script does not work.
My script need to SSH into my VMware box to run some commands, well I had moved my work from one computer to another (Both running Ubuntu 10.10). And when I ran my script it failed gloriously. I looked through and found that nothing was wrong... and worked on the other machine which was sitting next to me. I had found the problem to be the SSH client, the box I had moved my work to had never logged into the VMware server before so it did have the SSH key for that server stored. Once I ran SSH on its own the script worked fine from then on.
Which brings me to my question.
In perl is there some way I can allow my script to add a SSH key to machine that has never been connected to before? Instead of having to run SSH and answer "yes" to the question it presents me with asking if I want to permanently add the servers key to my SSH file(s).
The script can run ssh with -o StrictHostKeyChecking=no to automatically accept host keys from unknown hosts. This has security implications, and I believe even with this option ssh will refuse to connect if the host is already known and the key changes, although that's not clear to me from the documentation.

Android hosts file usage

I have the following problem:
I make a custom hosts file to test some features of my application and then push it to my android emulator.
The thing is that these settings do not take effect immediately. I have to wait about 10 minutes before they become active.
So my question is: how to make the new hosts file active instantly? I have many different settings to test and I can't wait 10 minutes every time.
Java maintains its own internal DNS cache. The operating system will reflect the new hosts file immediately (verify that with ping on the command line) but you'll need to tell java not to cache anything. Add these lines to your test application:
System.setProperty("networkaddress.cache.ttl" , "0");
System.setProperty("networkaddress.cache.negative.ttl" , "0");
For more information on these properties, see here:
I just edited my hosts file on my rooted Samsung Galaxy S, and the changes took effect immediately. Perhaps the problem you're seeing is something to do with ADB?
I did this:
Using Busybox, copy the hosts file to an editable location with cp /etc/hosts /mnt/sdcard/;
Edit /mnt/sdcard/ using the pre-installed text editor, adding the two entries I need. I used IP, then unqualified hostname, then FQDN, eg siva, but other formats should in theory work too;
In BusyBox again, su to root;
/system is ro by default, so I had to make it rw with mount -o remount,rw /system;
To save typing later, cd /etc (whereupon the shell prompt showed /system/etc rather than /etc, which makes me suspect symlink shenanigans);
Back up the default hosts file (which contained only localhost) with mv hosts hosts.old;
Install new hosts file with mv /mnt/sdcard/ hosts;
Execute sync (merely because I am paranoid - this shouldn't be necessary);
Remount /system fs ro with mount -o remount,ro /system;
Exit BusyBox;
Fired up web browser (FireFox) and entered siva in the combined URL/search field thingy (siva being one of the two hosts entries I added).
Prior to these changes, step 11 resulted in a stupid Google search for 'siva' or something; immediately after them, I get my LAN httpd vhost's front page, as I expect.
There was well under 10 minutes elapsed between it working and it not working.
The link to Sun's Java doco may or may not be relevant (probably it isn't).
Android doesn't contain a Java VM at all, let alone Sun's one. It runs a different VM called Dalvik - see Wikipedia entry:
The fact that you can program Android phones in a language that looks a lot like Java is beside the point.
I haven't tried using the hosts file; but I have tried using custom DNS...
Start a DNS server somewhere (you can load DD-WRT, have a fancy router, or run a daemon on your PC).
OPTIONAL: Configure your local router (DHCP server) with the location of the running DNS server (make it first in the DNS server list).
IF SKIPPING #2: Configure your phone for static IP (Wi-Fi Settings->Menu button->advanced). Set the DNS1 to be your server.
Have your phone connect to this network over WIFI.
Your done! Now you can manage DNS Names/IP parings along with lease times (e.g. so the phone will update the IP every 30s if you need). Somewhat complicated, but you don't have to load the hosts file on the phone :-).

Default Server Directory in Putty [duplicate]

On Windows I use PuTTY to log in a remote server via SSH. Is is possible to change the default directory entry point I get to after I connect and login? (That would be a nice time safer)
e.g. from server/home/ to server/home/subdir/subdir
forgot to add that the remote webserver is linux based
Add cd subdir/subdir to ~/.bashrc or ~/.bash_profile, or
change the home directory of the user.
You can also change the directory with Putty itself. Go to PuTTY Configuration -> Connection -> SSH. Fill in the "Remote command" field with the following:
cd subdir/subdir ; /bin/bash
You could use screen to keep your current directory by always logging into the same screen session.
Add the screen command with parameters to your putty saved session under SSH>Remote Command.
See the screen documentation for which parameters to use.
screen -R -O -t mysession -S mysession -f
Depends on a lot of things. Mainly the server operating system. Most SSH servers run Linux or Unix of some flavor. When you connect via any method of remote text login (as SSH is one member of this category) such a system will automatically spawn a shell process to talk to you. That's probably "bash", and it runs various things on startup. Check out
man bash
to learn more. One of the many things you can do is cd to a subfolder.