Linux modified hostname Script-no restart-support Redhat, SUSE

Source: Internet
Author: User

The host name needs to be modified with a script, involving the Redhat, SUSE system, and requires that the modifications take effect immediately without restarting, and here is my script.

Here's how to use the script:

1 First create a script file, if named ModifyHostname.sh:touch modifyhostname.sh

2 then give this file to this file grant Execute permission: chmod a+x modifyhostname.sh

3 Copy the following script contents to the modifyhostname.sh file

4 Execute Script

SH modifyhostname.sh newhostname #其中Newhostname就是新主机名

#!/bin/Bashpath=/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/x11:/usr/x11r6/bin:/usr/games :/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/Sbinexport Pathnew_hostname=$1Osrelease= ' lsb_release-i|awk-F:'{print $}'|sed 's/\t//g'`#1modify/etc/hosts or add one loop entryold_hostname=`grep${hostname}/etc/hosts|WC-l 'if["$Old _hostname"!="0"X]; Then    sed-I."s/${hostname}/${new_hostname}/g"/etc/hostsElse    Echo "127.0.0.10 ${new_hostname}">>/etc/hostsfi#2Modify different OS's hostnameif[[${osrelease} =~'SUSE']]; Then    Echo "This is the SUSE machine"; #modify/etc/hostnamefile    Echo${new_hostname}>/etc/HOSTNAME; #immediate effect/etc/rc.d/boot.localnet stop; /etc/rc.d/boot.localnet start;elif[[${osrelease} =~'RedHat']]; Then    Echo "This was RedHat machine"; sed-I."s/${hostname}/${new_hostname}/g"/etc/sysconfig/Network; Else     Echo "This is Donnotknow";fi#3Modify variblehostname${new_hostname}export Hostname=${new_hostname}echo "Hostname Modify successfull!" #今天在写一个脚本遇到问题,----need to introduce shell variables in sed, search the Internet to find the weather, after testing # #这四种方法都可用, here to share with you,=---at the same time thank Netman eldest brother, senior is senior haha # #1. Evalsed' s/$a/$b/' filename#2.sed "s/$a/$b/"filename#3.sed' s/' $a '/' $b '/' filename#4.seds/$a/$b/filename

Linux modified hostname Script-no restart-support Redhat, SUSE

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.