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