Change the color and format of the Bash prompt

Recently I got bored with the standard prompts and colors of the Bash shell in Linux, and decided to tweak them a bit.  What I decided to do was to make the root user account have a red prompt, mainly so I wouldn’t forget to log out once I was done and also to distinguish it from normal user accounts.

Editing the Bash Prompt

In order to change the color of the bash prompt, you need to edit the PS1 environment variable.  You can do this by typing the following in the bash shell:

export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m"


This will temporarily change the color of the bash prompt to red, and some other parts of the prompt as well.

Here is a basic breakdown of each part of the command:

The export PS1= is telling the shell that you are changing the PS1 variable

The \e[31;1m controls what foreground and background the prompt will have. 31 = Red foreground, 1 = black background.

The "[\u@\h \@ \W]#" means “your username”@”hostname” “localtime” “working directory”.

Making PS1 Environment Variable changes Permanent

If you want to make the changes permanent, you will have to append this line of code to the end of your “.bashrc” file in your home directory. You can do this with by editing the .bashrc file with any text editor in linux (nano, vi, vim, gedit) and manually add in the following line export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" or this way:

1.  Put export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" into a text file called "filenamehere"
2.  Then type the following command:

cat filenamehere >> ~.bashrc

3.  Then type exit in the terminal
4.  Open up a new terminal and you then should see the changes to the bash prompt.

This effectively uses cat to pint the export PS1 command to standard output in the shell, then with the append operator ">>" it will redirect the export PS1 command and add it to the end of the .bashrc file.  It's a fun and simple shell script! :)   CAUTION!!  Make sure you use the append operator ">>" and NOT the redirection ">" operator which will delete everything in the .bashrc file and replace it with what ever you redirected.

Reverse changes to PS1 Environment Variable
If you ever want to reverse the changes you have made to your PS1 environment variable, do the following:

1.  Open a new terminal window
2.  Type "vim .bashrc"
3.  Use the arrow keys on the keyboard to scroll down to where it has the "Export PS1=" line.
4.  Press the letter "d" twice on the keyboard to delete the whole line.
5.  Press escape
6.  press ":"
7.  after the colon, type "qw" for quit and write.  This will quit vim and save the file.
8.  Exit the terminal
9.  The next time you open a terminal for that user, you will have the default PS1 settings again.

If you would like to get more details and info about editing PS1 environment variables, visit the site in the references section below.  It has a wealth of info on the subject.  Enjoy!!

References:
http://www.funtoo.org/en/articles/linux/tips/prompt/

  • Share/Bookmark

How to prevent yourself from accidentally deleting files in Unix/Linux

Everyone at some point has faced that horrible moment when you realize that you just deleted a critical file, and start scrambling to find ways to recover it before it is too late.  If you haven’t faced this situation yet, you are the lucky few who have been spared from this horrible experience so far.  Just the other day, I found a really creative solution in Linux that will prevent you from accidentally deleting files by mistake.  It’s actually not that complicated either, and I am sure you will be using this technique to protect your most valuable files after your through reading my how-to.

Step 1 – Choose a Directory

HOWTO-prevent-accidental-delete

As you can see, I have created a directory called “test” and have created a ton of empty files and directories for testing purposes.  It was fun writing the Perl script to create all those “crap” files.

Step 2 – Create a empty file call “-i” in the chosen directory

HOWTO-prevent-accidental-delete-2

To do this simply use the touch command to create the empty file “-i”, as such: “touch ./-i”

Step 3 – Verify that “-i” exists in the current directory

HOWTO-prevent-accidental-delete-3.1

To make sure “-i” was created successfully, list the directory contents by invoking the “ls” command as show above.  Once you do that you will notice a new file called “-i” listed in the current directory.

Step 4 – Try to delete the files in the current directory

HOWTO-prevent-accidental-delete-3.9

Try deleting all of them at once by using “rm *”.

WARNING – make sure you have the “-i” the the directory before you try to remove any files!  Without the “-i” file there will be nothing to stop the rm command from removing your precious files!!

Below is a screenshot of what should happen when you try to delete any file in that directory.

HOWTO-prevent-accidental-delete-4

Did you notice what happened?  When we tried to delete any file in that directory, It would prompt us to confirm if we really wanted to delete each file.  This happens because the “-i” file is immediately interpreted by the Unix/Linux command line as a option for the “rm” command, which prompts before deleting a file.  If you man rm and look through the man page you will find the following entry:

  -i, --interactive
	      prompt before any removal

So that is the magic behind creating the empty “-i” file and how it helps prevent you from deleting file by mistake. Now don’t forget that if you use the rm -f command, it will delete the file no matter what!! That’s why it’s called the FORCE option!

Use this technique to your advantage and spread the word!

  • Share/Bookmark

Understanding Unix/Linux Load Averages

I was looking at some linux server the other day, and was taking a look of the load averages using the “top” command.  After staring at the load averages for a while, I thought to myself ‘What does that mean exactly?” and “How do they get those numbers?”.  Well after some research I have found out that the load average is not a % of cpu usage, but is a representation of the load on the CPU by processes waiting for their time to use the CPU.

Below is a screenshot of the top command for reference.  Notice in the top right are the load averages “1.23″ 1.17″ “1.05″.  The first load average is for 1 minute, the middle is for 5 minutes, and the last one is for 15 minutes.

top and load averages

Now the important thing to remember about Unix/Linux load averages is that it is not a % of cpu usage, but how many processes are waiting to use the cpu.  The higher the load average, the more load the server in under.  Right about now you are probably wondering ” well what is a normal load average?” or “What is a high load average?”, the easiest way to determine if your load average is normal or high is to look at it in the following way:

CPU Cores Normal Load Average
1 1.00
2 2.00
3 3.00
4 4.00
8 8.00
16 16.00

As you can see a normal load average for a single core cpu is 1.00 while a dual core cpu can handle a load average of 2.00.  If it helps you out, you can think of 1.00 to be %100 for a single core and 2.00 be %100 for a dual core, and so on.

top showing each individual core (8 Cores)

Above is a screenshot of top showing each individual cores ultilization.  This is an 8 core cpu, so it’s normal load average is 8.00.  You can see this output in top by pressing “1″ on the keyboard.  Also, if you were wondering how I got top to display different colors, just press “z” on the keyboard.

The key thing to remember is that load averages in Unix/Linux are not % for cpu utilization, it is merely a representation of how many processes are waiting for their turn to use the processor.

References
http://www.linuxjournal.com/article/9001

http://blog.raamdev.com/2008/11/22/understanding-the-linux-load-averages

  • Share/Bookmark

Using scp with spaces in the file path

I just recently learned how to upload a file from my linux box to my webserver using scp the other day. Normally its pretty straightforward to use scp to upload or download a file to a server, however I was having the hardest time uploading this one file that had spaces in the file path.

Here is what it looked like:

scp -r /home/username/Desktop/Sysadmin\ Tools/sysadmin-tools.zip user@website.com:public_html/somefolder/

When i would run this command, the following error would be displayed:

zip warning: name not matched: /home/username/Desktop/Sysadmin
zip warning: name not matched: Tools/*

As you can see, it was breaking up the file path at the space in “Sysadmin Tools” folder, and breaking it up into different arguments. The solution to this problem is to do the following:

Solution:

scp -r /home/username/Desktop/Sysadmin\\ Tools/sysadmin-tools.zip user@website.com:public_html/somefolder/

All I needed to add was an extra “\” backspace character after “Sysadmin”. This is because when the file is uploaded to the remote server, the “\” escape character will disappear (I am not sure why), and will cause this kind of error. However if you double escape the space “\\”, the first escape character will be removed but the second will remain, thus keeping the correct file path.

References:

http://www.thingy-ma-jig.co.uk/blog/14-05-2007/how-to-scp-a-path-with-spaces

  • Share/Bookmark

Really slow performance using Remote Desktop

Remote_desktop_connection_iconLately I’ve run into a problem while doing maintenance on a remote server.  When I connect to the remote server via rdesktop on my Fedora 11 desktop, The windows move extremely slow and it appears that the system is taking a long time to respond to my commands.

After doing much research, it looks like the problem is with the new RDPv6 that shipped with Windows Vista, and 7.  Apparently this server 2003 box was updated with this new RDP verison which has some new bandwidth negotiation features, that cause RDPv5 clients to perform horribly as I experienced today.

UPDATE – The version of RDP seems to have nothing to do with the slow performance I or others have experienced lately.  Just last week the same server which was horrendously laggy when using remote desktop, is now lag free and very responsive via remote desktop.  At this point I am guessing that it has something to do with saturated networks or bigfoot! (LOL), only mere mortals can guess why M$ servers decide to work.

To check your RDP version do the following:

1.  Got to Start -> Run

mstsc-run

2.  Type “mstsc” then press enter

3.  The Remote Desktop Connection Window will then appear

mstsc

4.  Right click on the title bar, then select about

mstsc-about

5.  In the about window look for “shell version” and that will tell you the RDP version number.  In this example below the RDP verion is 5.2.

mstsc-rdp 5

Solution:

The only way I was able to get around the ridiculously slow performance in rdesktop, was to use a windows machine with an updated RDP client. Once I used a up to date client, the performance issues dissapeared.  I’ve searched linux forums and other sites to see if rdesktop has been updated to RDPv6 but have not found anything yet.  I know this isn’t ideal but for now it seems to be the only way untill another solution is found.

References:

http://www.vistax64.com/vista-networking-sharing/79317-fixed-remote-desktop-connection-rdc-slow-no-connection-issue.html

http://blog.tmcnet.com/blog/tom-keating/microsoft/remote-desktop-slow-problem-solved.asp

  • Share/Bookmark