Linux bulk modifies host names for multiple servers (hostname)

Source: Internet
Author: User
Tags pscp

One. Scenario Description:

There are a lot of servers in an environment, each server hostname are different, there are hundreds of servers, if one by one through SSH modification, then for us, the workload is very large, and inefficient, and this hostname for our peacetime operation is also very important, When we ssh to a server, if the host name is not set, then we need to know the location of the server by other means, and so on, to our operators to bring a great operation and maintenance trouble.

Two. Preparatory work

1. All server operating system root account password needs to be consistent, and network interoperability

2.PSSH Tools: Can implement batch execution commands, send files and so on, detailed attention to my other blog "Linux batch operation Pssh Tool"

        3.SSH key synchronization: That is, Mr. Cheng Ssh-key, and through the PSCP tool sent to each server/ROOT/.SSH, the implementation of SSH password between the server login, detailed attention to my other blog Strong style= "Color:rgb (44,44,44); font-family:arial;font-size:14px;white-space:normal;padding:0px;margin:0px;" > "Linux Password-free login"

4. Design the corresponding relationship between each server IP and host name hostname/root/host for example:

192.168.225.128 Server1

192.168.225.129 Server2

192.168.225.130 Server3

192.168.225.131 Server4

192.168.225.132 Server5

192.168.225.133 SERVER6

192.168.225.134 Server7

192.168.225.135 Server8

192.168.225.136 Server9


Three. Bulk Modify host Names

1. Add a hostname.sh file

[Email protected] ~]# vim hostname.sh

ip= ' Ifconfig eth0 | grep ' inet ' | awk ' {print $} '

Hostname= ' Cat/root/host | grep $ip | awk ' {print $} '

Echo $ip

Echo $hostname

Hostnamectl set-hostname--static $hostname

Hostname $hostname

Note: We are using the server IP address to modify the hostname, the above interface must be accurate, the command is relatively simple.

2. Send the generated two files (host and hostname.sh) via the PSCP tool to any of the same directories on all servers

pscp-h iplist/root/host/root/hostname.sh/root/# iplist is all server IPs, if not used, see Prepare for work 2nd.

3. Through the Pssh tool, batch give hostname.sh this file executable permissions

pssh-h iplist-i ' chmod +x/root/hostname.sh ' # iplist is all server IPs, if not used, see Prepare for work 2nd 。

4. Batch execution of hostname.sh files via Pssh tool

pssh-h iplist-i ' sh/root/hostname.sh '

Four. Other

This batch modification method is relatively simple, suitable for this kind of people who do not use the script frequently, the above operating system is more than CENTOS7, if your operating system is CENTOS7 below, according to the operating system modified hostname method is different , the method of bulk modifying hostnames is very similar. Other buddies if there is a better and easier way, please also guide the guidance, thank you.

This article is from the "Children's Blog" blog, please be sure to keep this source http://bingotcr.blog.51cto.com/11710314/1914994

Linux bulk modifies host names for multiple servers (hostname)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.