Application of Linux in batch server Management PS1 command prompt format implementation method

Source: Internet
Author: User
At this point, we will often encounter such a problem: the operation of the server because of something interrupted, back to continue to operate the time will certainly be ifconfg to confirm whether I want to operate the server, because it can not be recognized from the image.

Therefore, it is necessary to optimize this PS1 command-line prompt. Every operation and siege Lion must have its own habits, but I still recommend a server batch management in the comparison of the use of PS1 format it!

PS1 is God horse? PS1 is a default environment variable inside Linux, and you can use the command "Env|grep PS1" to see how the current system PS1 is set up.

In fact, PS1 is the environment variable used to set the command prompt format.

The following PS1 configuration parameters:

\d: Represents the date, in the format weekday month date, for example: "Mon 1" \h: Full host name. For example: My machine name is: Fc4.linux, then this name is fc4.linux\h: only the first name of the host, as in the example above, then the fc4,.linux is omitted \ t: Display time in 24-hour format, such as: hh:mm:ss\t: Display time is 12 hours format \a: Display time is 24 hours format: hh:mm\u: The current user's account name \v:bash version information \w: The full working directory name. Home directory will replace \w: with the basename to get the working directory name, so only the last directory \#: The first command issued \$: prompt character, if it is root, the prompt is: #, ordinary users are: $

Of course, in order to better identify, we can also add some color settings, this will not repeat, Baidu shell color can, of course, remember to refer to the following PS1 for customization.

To better differentiate the server, I recommend the following format:

[Username@ipaddress/pwd]#|$ such as: [root@192.168.1.1/data/]

So PS1 can be set as follows:

Export ps1= ' \[\e[32m\][\u@192.168.1.1:\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\$ '

But there are too many machines, this IP can not be manually modified every time, so write a script to modify it! (Of course, you can also get the IP first, the assignment variable joins the PS1)

The script is simple:

#!/bin/sh########################################################################## Update PS1 like [ root@192.168.1.113/data]# ########################################################################## #先判断网卡是否存在, My side eth1 is intranet nic Ifconfig eth1 >/dev/null 2>&1if [[$?! = 0]]then echo ' interface eth1 not exsit! ';  Exit 1fi#centos/redhat 7 Ifconfig shows the result is not inet addr: But inet directly with the IP, so here to determine: function Get_eth1ip () {if [[$1-eq 7]] Then #for CentOS 7 eth1_ip=$ (ifconfig eth1 |awk '/inet/{print $} ' |awk ' {print $} ') Else eth1_ip=$ (ifconfig eth1 |awk-f " : "'/inet addr:/ {print $} ' |awk ' {print $} ') fi} test-f/etc/redhat-release && grep 7/etc/redhat-release >/dev/null 2>&am P;1 && get_eth1ip 7test-f/etc/centos-release && grep 7/etc/redhat-release >/dev/null 2>&1 &am p;& Get_eth1ip 7 | | Get_eth1ip Echo $eth 1_ip | Grep-e "[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} ">/dev/null 2>&1 if [[$?! = 0]]then echo ' eth1_ip IS empty! ' exit 1fifunction Export () {echo ' Export ps1= ' \[\e[32m\][\u@${eth1_ip}:\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[32m\ ]]\[\e[m\]\\$ ' ">>${1} && \ echo-e" \033[32m Update \033[0m \033[33m${1}\033[33m \033[32msuccess! Relogin your system for Refresh ... \033[0m "} function home_env () {if [[!-Z]] then home=$1 else home=/root F I #有的用户可能会在家目录下自定义一些配置, that is. Proflie this hidden file, so also need to update test-f $home/.profile && (sed-i '/export ps1=/d ' $home/.profil  E Export $home/.profile)} #获取当前用户id, if it is the root group, you can manipulate/etc/profileuserid=$ (id | awk ' {print $} ' | sed-e ' s/=//'-E ' s/(/ /'-e ' s/)//' |awk ' {print $} ') if [[$userid = 0]]then #for all sed-i '/export ps1=/d '/etc/profile export/etc/profil E #for root home_env #如果其他用户需要修改, just turn on three lines and change other to username #id other >/dev/null 2>&1 && (# home_env ~oth ER #) Else #for userself home_env ~fi

Well, at last direct source./update_ps1.sh can see the effect:

Re-login or source/etc/profile, you can see the effect:

Once this is set up, you will be able to clearly know which server is being operated on, without confusing it.

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.