Saturday, August 18, 2012

Testing a Hard Disk

SKILLS -> Knowing your command line.

http://www.cyberciti.biz/tips/linux-find-out-if-harddisk-failing.html

This is the source article that I got the information from.  They do a great job of detailing the operation.  If the link is broken, here's the basic outline.

Install the software

apt-get install smartmontools -y

This basic command installs the smartmontools. You figured that didn't you :)

(sdx refers to one of the hard disks on your sytem.  sda,sdb,sdc,sdd)  If you don't know how to tell I would suggest using sda since that's the first in line.  If you have a second hard disk generally that's sdb.  That's generally of course.



(therefore smartctl -s on -d ata /dev/sda tests the first hard disk)





IS my harddisk smartcapable.  Usually it is but if you want to check:


smartctl -i /dev/hdx

[NOTE: If its not throw it out.  It's a junk hard disk ] 


This enables the function.


smartctl -s on -d ata /dev/sdx


Now we can read the error logs

smartctl -l error /dev/hdx

Send data to your email to yourself if you want.

/dev/sdx -m fred@myemail.com


The tests are more and more detailed but here are the basic command line structures.

 smartctl -d ata -H /dev/sdx
smartctl --attributes --log=selftest /dev/sdx
smartctl -d ata -a /dev/sdx

Short and Long tests for the hard disk

smartctl -d ata -t short /dev/sdx
smartctl -d ata -t long /dev/sdx

(output)
smartctl -a /dev/sdx

What's this?  Well if you want it all combined into a graphic interface, they use gsmartcontrol which is the graphic frontend.  
 
apt-get install gsmartcontrol

Have fun!
 






Wednesday, August 15, 2012

Remove old Kernels

Thanks to this website:

http://askubuntu.com/questions/138026/how-do-i-delete-kernels-from-a-server

At the bottom of the page is the actual script to do the job.

Ever had a problem of endless kernels on your system without the storage to manage it?

No prob.  You can remove old kernels with this script.

IMPORTANT:

If using GRUB legacy, be sure the menu.1st file contains an actual kernel you can use.  Its easy to check the current kernel running with this command:

uname -R

NOTE this will reference the running kernel and is tied to the menu.1st.  If you deleted the running kernel, just make sure you have an alternate.  ALL kernels are stored in:

/boot.

Just ls to get a list of kernels. 


DELETE all kernels.  Note this is actually one line of text.  The '\' is used to separate lines.  Save it as a file.

#!/bin/sh
dpkg -l linux-*  | \
awk '/^ii/{ print $2}' | \
grep -v -e `uname -r | cut -f1,2 -d"-"` | \
grep  -e '[0-9]' | xargs sudo apt-get -y purge

Monday, May 14, 2012

Mounting a USB device

ASSUME:
Command line knowledge
How to get to be a root user

Despite all the promises, Linux often chokes when it comes to mounting a USB device such as a camera, phone, etc, it often chokes and its frustrating not knowing a way to get it to work.  Here it goes:

FIRST:
You need to be a root user to get this to work.  Often times that just means SUDO but you can also use SU as well.  If you type SUDO, type your username

(e.g. SUDO foo)...or whatever your name is.

Type in your normal password and you're a root user.  SU works the same way but you don't need to type a username...just SU (super user in other words) and you need to know the ROOT password to do it.  Most distros are going the SUDO route so chances are try that.

SECOND:
lsusb  (list USB devices...linux people hate to type)

This will just ensure you have a USB device in there. Look for the term 'usb in the list.

Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
Bus 004 Device 002: ID 045e:00dd Microsoft Corp. Comfort Curve Keyboard 2000 V1.0
Bus 004 Device 003: ID 045e:0040 Microsoft Corp. Wheel Mouse Optical
Bus 001 Device 006: ID 22b8:70c6 Motorola PCS

In this example, you'll see the USB-2.04-Port HUB.  Ya, that looks about right and this tells us that USB is working. 

You could also type:

lsusb | grep USB

This would filter out the list to only show anything with USB in the name.  Don't ya love greg!

THIRD
Now that we KNOW we have a USB device, let's see what the drive is that mounts it.  Fortunately, we have a miracle command called DMESG.  This lists all the messages recently printed.  Naturally we want the find any USB devices. 


dmesg (let it scroll past you.  We want the end of the list...NOT...anything else.


[ 2432.904237] sr 8:0:0:2: Attached scsi generic sg6 type 5
[ 2432.914538] sd 8:0:0:1: [sde] Attached SCSI removable disk
[ 2432.918523] sd 8:0:0:0: [sdd] Attached SCSI removable disk
[ 2435.563830] sd 8:0:0:1: [sde] 15572992 512-byte logical blocks: (7.97 GB/7.42 GiB)
[ 2435.565942] sd 8:0:0:1: [sde] No Caching mode page present
[ 2435.565950] sd 8:0:0:1: [sde] Assuming drive cache: write through
[ 2435.570814] sd 8:0:0:1: [sde] No Caching mode page present
[ 2435.570820] sd 8:0:0:1: [sde] Assuming drive cache: write through
[ 2435.573677]  sde: sde1
[ 2441.705601] sd 8:0:0:0: [sdd] 9050112 512-byte logical blocks: (4.63 GB/4.31 GiB)
[ 2441.707717] sd 8:0:0:0: [sdd] No Caching mode page present
[ 2441.707720] sd 8:0:0:0: [sdd] Assuming drive cache: write through
[ 2441.712716] sd 8:0:0:0: [sdd] No Caching mode page present
[ 2441.712718] sd 8:0:0:0: [sdd] Assuming drive cache: write throug




NOTICE: the 'removable disk.  Ya, that's what we want.  In this example we have sdd and sde telling us that there are two drives associated with the device.  We'll mount both.

Now, on some distro's they create a /media directory and you mount things there.  The default is always /mnt but honestly you can mount it anywhere.  I tend to use /mnt but its up to you.

mkdir /mnt/FRED
mkdir /mnt/FRED2

mount /dev/sdc /mnt/FRED
mount /dev/sde /mnt/FRED2

HU???

Well, easy.  mount is the command that tells us to mount the device.  It takes two parameters.  The first is the source and the second is where you want to mount it.

/dev/sdc comes from dmesg and /mnt/FRED is the directory (folder) I created to mount the device.  Ya, Fred isn't too original but you can come up with something more original.

That's it.  You should now see the data in /mnt/FRED and /mnt/FRED2

I tend to just cd into that directory (I'm not covering cd here) but you can use a file manager such as nautilus if you want.


Sunday, May 13, 2012

Kernel Parameters

ASSUME:
This is advanced so no beginners for this one.
How to edit grub to modify the kernel parameters
Know what a kernel parameter is:


Editing the kernel is one thing but modifying boot options is another.  In this entry, we're going to modify some kernel parameters that are really useful -- especially when headaches arrive. 
In Grub1, or Grub2 you are allowed to edit the Kernel parameters by simply modifying the line that launches the kernel.



(Yes I know it wraps to the next line...sorry
kernel        /vmlinuz-3.2.0-23-generic-pae root=UUID=65c84575-96be-4380-b4d8-1b1d6292816d ro

This is a typical line for a kernel in the grub menu.

kernel - saying we have a kernel
where to find the kernel - /vmlinuz-3.2.0-23-generic-pae root=UUID=65c84575-96be-4380-b4d8-1b1d6292816d

ro - make it read only...handy

Anyway, here are some items you can place to the right of that line to make it more functional.

quiet - 
I don't like this but its typically found to display displaying the bootup process.

splash -
 throw up a splash screen which I don't like either.

text - 
VERY handy.  Boot to text mode to avoid autoloading the graphic screen (GNU, XFCE, etc.)

nomodeset
Some distros (Ubuntu in particular) make the text screen a huge tiny size.  If you just want 80 * 25 for the size, make this option available.
NOTE: For Ubuntu, if you do that, you'll get one UGLY font.  Do this>

dpkg-reconfigure console-setup

check VGS not FIXED and you'll get a nice clean font.

noacpi, pci=noacpi
These are two separate options but essentially work on the same idea.  If you are having hang problems with an operating system, try this option(s).  It disables polling of the PCI bus for interrupts.  It can stabilize a system.  Treat this as a diagnostic.  If it works after this, chances are your BIOS is a problem.  Upgrade the bios.

That's it.

Wednesday, May 9, 2012

Smurf bug in Adobe

If you:

(a) Have the latest Adobe Flash player plugin
(b) Have the proprietary NVidia driver

You will see blue people due to a hue problem with the flash player. 


In short, try this:

sudo add-apt-repository ppa:tikhonov/misc
sudo apt-get update
sudo apt-get install libvdpau1
 
 
The patch is located here:
 
http://lists.freedesktop.org/archives/vdpau/2012-May/000022.html
 
This is a patch that works around the adobe flash problem. What is the problem????
 
Read this link in its entirety.
 
http://askubuntu.com/questions/117127/flash-video-appears-blue

Sorry this is so short but there are only so many hours in a day.

 
Try another link:
http://www.nvnews.net/vbulletin/showthread.php?t=177380

Monday, May 7, 2012

Install Grub2 from a LiveCD

KNOW:
Command line basics
chroot
grub

You would think that installing Grub...or GRUB2 from a LiveCD will be an easy task -- think again.  There is a lot to know so let's get to it.

1) Boot to a liveCD.  Most graphic LiveCD's take 20 years to bootup -- Ubuntu being a great one for that but you can get text only LiveCD's so look for em.

2) Run terminal.  You need to get to a terminal to make this work.

3) We're now going to make the installed version of linux work instead of the LiveCD. This is tricky so let's get to it.

(a) you MUST know the partitian for your boot (/boot) and your mnt (/) locations.
In this example, we're going to use /sdb6 for our root (/) and /sdb1 for our boot.  Most installations don't separate the two but I'm giving you the extreme example.

sudo /mnt/sdb6 /mnt

Here we mount the entire partitian to the /mnt directory.

sudo /mnt/sdb1 /mnt/boot

(IF your boot partitian is different, otherwise leave alone)

Now we're going to point three directories (/proc /dev /sys) to our new mount point.

mount -t proc none /mnt/proc
mount -o bind /sys /mnt/sys
mount -o bind /dev /mnt/dev  (the devices)
cp -L /etc/resolv.conf /mnt/etc/resolv.conf

The purpose of this is to simply avoid host name errors

chroot /mnt/ /bin/bash  (most critical)
env-update 
source /etc/profile 
export PS1="(chroot) $PS1"

NOTE:
cp /proc/mounts /etc/mtab

(use this if you plan to reinstall grub)

sudo grub-install /dev/sdb1

That should shove it on the boot of the drive.

 

Monday, March 19, 2012

Bad video display in XFCE fix from command line

BUG:

XFCE is kind of dumb. It doesn't back out of bad display setting such as 1280*1024 which, for whatever reason, you're card or monitor can't handle. Instead it leaves that whole crappy display up there for you to suffer with.  Here's how to fix it.

You would think that modifying /etc/xorf.conf would be your go-to center for changing the display settings from the command line in XFCE.  NO!

Nope, there is a profile saved with each user -- which I suppose has its benefits.  Here is where you modify it...get ready for a long path.

vim ~/.config/xfce4/xfconf/xfce-perchannel-xml/displays.xml

Set the proper resolution in displays.xml.

Whatever it is, a safe choice is 800*600.  Then you can take it from there. 

I am also told that:

xfconf-query works but I've never found it useful.

Monday, February 13, 2012

VIM - Search and Replace

Assume:
You know some VIM.

VIM has an amazing and daunting search and replace command but we're going to ignore most of it and show you the basic search and replace. 

The Search/Replace command is activated with the command mode (:).  Incidentally, the command mode actually triggers ex which is the command line editor in Unix/Linux.  Ex was the grandfather of Vim (or actually Vi).  Ex was the main reason why so many people wanted to burn their computer to the ground.  It's a line editor which means you only see one line at a time.  In the DOS world, it was Edlin (edit a line).  I actually used Edlin once...notice the Once!

 Anyway, to activate search/replace you actually do (substitute -- which is the actual name of the command). 

:s


That triggers the search replace command and does nothing since you've told it nothing.  The basic thing it does is:

:s/search text/replace text/

So
:s/fred/bob/

will search for fred on the current line and replace it with bob.  I know not very exciting.  VIM can get really complex when you start specifying line numbers in searches but what we really want to start of is a global search and replace with a prompt.  That's what every single editor does on the planet. Here's how.

:%s/search text/replace text/gc

%s says all lines.
/g means global and that means do every single replacement on all lines -- not just the first time we find it (ya like that's useful right?)
/c means prompt you for the replacement.


:%s/fred/bob/gc

Will replace every instance of fred with bob in the entire document with a prompt warning.  (say y or n and ignore the rest for now).  That's about as basic as it gets.

Now, I'm going to give you one more idea before we depart.  Ever had an instance where you want to do a global search and replace BUT on only certain conditions.  For example, I want to replace every instance of Fred with Bob but only if the last name is Sampson.  Hmmm...can't be done on any editor I know...but VIM is NOT just any editor is it?

:g/Sampson/s/Fred/Bob/gc
 
 
In other words, globally search for Sampson but then search next to Sampson for Fred 
and replace it with Bob. Pretty neat hu!
 
 
That's about it.  Hope you enjoyed!!! 

VIM - Command mode

Assume:
You know VIM basics.


Most people poking around in VIM don't realize that the command mode (typing a : or a colon) can really lead to some powerful features.  Now did you know there are several commands that are of great use to VIM available in the command mode.  Here are my favorites.

Directory Listing (:Ex)
That's right.  You can get a directory listing of the current directory or anywhere you want with Ex -- think Execute directory.  Once you execute that you can get a directory listing.  This goes great with the tab command.

:tabnew | :Ex

This will create a new tab (see my tab blog) and give you a directory listing.  Notice the '|' pipe command.  That allows you to string two commands together.

Shell (:sh)
Ever wanted to execute a command from the shell without leaving VIM.  No problem.  Execute this command (sh) and you can get to a shell.  NOTE, just don't load VIM while you're in the shell.  Type quit to get back to VIM. 

Execute a command (!)
Ever wanted to just execute a command instead of a shell.  Use bang (!)

:! ls

That of course is the famous list command.


Quit all :qa
You know about (q) and (wq) and perhaps q!) but did you know about (qa).  That means quit all and it dumps you back to the prompt exiting all files.  Please note that it will prompt for saving the file.

MISC (:q, :wq, :q!)
Just in case, we'll be complete.
q -> means quit
wq -> means write current file and quit
q! -> means don't save the file even if there are changes and quit

There you go.  Not too bad a lesson right and don't forget the tab commands.

Tab commands (:tabnew, :tabn, tabp, tabn (number)
Just as a review...
:tabnew - opens a new tab
:tabn - next tab
:tabp - prior tab
:tabr - rewind to first tab
:tabl - last tab
tabn (number) just to a tab in the list.


Goto line number (: #)
Ever wanted to jump to a line number.  No problem.  Just literally type the line number.
:50 - go to line 50
:20 - go to line 20

That's it for today.

Monday, January 30, 2012

Vim Visual Mode

Assume - You know VIM to some degree
Assume - You want to make your life easier


Believe it or not, there is something -- somewhat reasonable in VIM and that's visual mode -- NO NOT ANOTHER MODE !!! Isn't Command mode and Normal (ya right normal) mode Insert mode enough!!!

Well, relax, Visual mode is actually intuitive and easy to access.  Anytime you are in Normal mode (press ESC to get to it, type:

---------------------------------------------------
v
---------------------------------------------------

(For Visual mode of course) When in Visual mode, any movement key you press -- including cursor keys will highlight text!  That's right, you can see what you are doing. Remember all the commands I'm going to give you require you to first highlight the text.

Well, here are my favorite things to do in Visual mode (you do know to highlight some text right???)

Cut/Copy/Paste
When you have your text highlighted -- do that of course first -- press
---------------------------
d
----------------------------
That deletes text but it does more than that -- it sticks it into the copy buffer for you to use later (sort of like copy and paste)
-------------------------
y
-------------------------
Yank (y) will take your highlighted text and stuff it into the copy buffer keeping the original on screen.

ONCE you have done either command...move the cursor somewhere else and press:
------------------------
p
-------------------------
This will paste it into your document.  There you have cut/copy/paste...but there is one more.

Indent
Nothing is more important or more frustrating that looking at code where someone refuses to indent.  You know the type:

if ($Fred == 5)
{
echo ("Foo");
if ($Bob == 4)
{
echo ("Stew");
}
}
Hideous!!!
Now highlight all the text you WANT TO INDENT.  Once you do that type:

<< OR >>
All the text moves giving us something like this:

if ($Fred == 5)
{
      echo ($Foo);
      if ($Bob == 4)
     {
          echo $Stew;
      }
}
Much more readable.  Good luck!

Tuesday, January 24, 2012

PHP String Heredoc

Assume:
You know how to program in php

Any Web Server programmer will encounter PHP and one of the biggest headaches in PHP (IMHO) is understanding strings. 


First, this is an excellent article on php strings.

http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm


Today though I want to concentrate on a tool that can help out.  As you know when you insert HTML or Javascript code in PHP things can get ugly in a hurry mainly due to the problems with quotes.   Sadly a basic keyboard only has two characters for quotes :  ( double "  and a single ')  But look at this example:

echo ( "onclick='DoAJavaScript ( 'Fred', 'Bob')' ");

This code won't work for several reasons but as you notice when we came to the variables of (Fred and Bob) we needed yet another set of quotes to cover that issue.  The problem is we used the double quotes to start the string and then the single quotes to start the Javascript call 'DoAJavaScript' leaving nothing left for the parameters. 

WHAT DO I DO????
Well there are a couple of solutions.  You can take advantage of the ESC '\' character sequence.  The key idea is that you use \" instead of a " to wrap a string and now the code gets really ugly:

echo ("onclick='DoAJavaScript ( \"Fred\", \"Bob\" ");

Wow this is getting really ugly and there is another problem.  PHP will only evaluate code with a double quote.  Everything else it takes literally...a real mess!!!

Example:

$Fred = "Hello";
echo ("$Fred");
// You get:   Hello

echo ('$Fred');
// You get: $Fred

However...don't fret.  There is a most convenient but hardly talked about function in PHP that is a lifesaver.  It is called heredoc and despite the convoluted name it works magnificently.

$Fred = <<<BLOCK

onclick='DoAJavaScript ('Bob', 'Fred');

BLOCK

Sweet.  With this crazy block of code you don't need starting and ending quotes saving you a serious headache.  Now take a look at the format.

BLOCK -> Notice that BLOCK starts and ends the string.  They have to be the same name and have to be upper case...AND have to be on the left hand side of the document.  (In other words, don't indent otherwise it won't work.

<<<  (This sends the information to the string.)

$Fred (The string variable.)

Now take advantage of this the next time you create a messy string structure.

Vim and Tabbed Windows

Assume:
You know how to use Vim,
You want to continue to use Vim,
You have a streak of nerd in you!

OK, in Vim, there is a typical way to split windows but want I want to talk about today is how to show multiple files as if they are tabbed windows.  Granted, its not FireFox but it works very well and will save you a lot of trouble. 

(Of course the problem with Vim is that nothing is every easy)

First, let's launch vim preferably with a file.

---------------------------------------------------------------------------------------------------------------------------------
vim (name of file)

---------------------------------------------------------------------------------------------------------------------------------

Now once in Vim we now need file number 2.  The problem is if we just load it, we delete file number 1.  Not good!  
Instead we type this into our Vim editor.


---------------------------------------------------------------------------------------------------------------------------------
:tabnew (name of file)
---------------------------------------------------------------------------------------------------------------------------------

Now you should see two tabs on the screen.  Yes!

There are a few ways to get to the file in question but my favorite is this:


---------------------------------------------------------------------------------------------------------------------------------
tabn 1 (name of file)

---------------------------------------------------------------------------------------------------------------------------------
 In this example it will go to tab 1.

---------------------------------------------------------------------------------------------------------------------------------
tabn 4

---------------------------------------------------------------------------------------------------------------------------------
This will go to document 4.

To close a tab, its the standard:


---------------------------------------------------------------------------------------------------------------------------------
:q

---------------------------------------------------------------------------------------------------------------------------------

(or :wq) if you need to write the file to disk.

That's it. 

To be complete:

:tablast
:tabfirst
:tabprior
:tabnext

Again all you're doing is tab + (first,last,next,prior)

Enjoy your tabs!

Wednesday, January 18, 2012

Check a log of user logins

Assumptions: You can type a command at the command prompt


Ever wanted to simply find out who logged in when and what users you have on the system?  Easy!!!

You can type:


------------------------------------------------------------------------------
lastlogin
-----------------------------------------------------------------------------

It will give you a detailed list of users and when/if they ever logged in.  Want to filter the list?  Easy!!!


------------------------------------------------------------------------------
lastlogin | grep (name of user)
-----------------------------------------------------------------------------

The command grep, as you know (or don't know) is an amazing command for filtering information displayed on a screen.  You simply pipe out the information from lastlogin into grep and grep will filter the content based on that user.  For example, if you have the user Fred, type this:


------------------------------------------------------------------------------
lastlogin | grep fred

-----------------------------------------------------------------------------

That's it.  No quotes or special characters required!

Tuesday, January 17, 2012

Change The Name of your Ethernet Card in Linux

Assumptions:
- You know about editing files:
- You know about the command line:
- You know what (eth0, eth1) is:


As you know (or don't) linux automatically assigns a name for any Ethernet card in the system.  This can be helpful -- unless you change cards and discover all your links to that card are...gone.  How do you fix it???

Well, Linux has one directory that you should know about and its called /etc.  That directory is off of  / (main root directory).  If you want to change anything, try to find the file in there first. 

First using your favorite editor type this:

------------------------------------------------------------------------------------------------------
vim /etc/udev/rules.d/70-persistent-net.rules
------------------------------------------------------------------------------------------------------

Now I'm using vim for an editor but you can use anything you want.  Inside the file though will contain
obvious entries with eth0, eth1, etc.  Change the names as desired and save the file.

THAT IS IT!!!!

First Entry:

This Website (Blog) is really about me recording information so that users don't have to deal with the headaches I dealt with.

I will:

Give you tested tips:

Give you detailed steps:

Try not to be too serious: