How to View System Logs in Real-Time

If you are having an issue especially with hardware or some other reproducible manner, here is a solution to see just what is getting written to the sys log in real-time. Perhaps this will aid in trouble shooting the issue. I know it has helped me in the past identify a device’s mount point when it would not show with the mount command. I ran across this string a few years ago in a forum and have saved it ever sense. I want to share it with you here.

The command is the tail command run in a separate terminal window if needed as root or with escalated privileges with sudo.

# tail -f /var/log/syslog

That’s all there is to it. Of course adjust the path if your logs are in a different directory and read the man page if you want or need additional options. You can expect some output like this:

johnny@polarbear ~ $ sudo tail -f /var/log/syslog
[sudo] password for johnny:
Jan 17 20:29:25 polarbear rtkit-daemon[1811]: Successfully made thread 2111 of process 1809 (n/a) owned by ’1000′ RT at priority 5.
Jan 17 20:29:25 polarbear rtkit-daemon[1811]: Supervising 3 threads of 1 processes of 1 users.
Jan 17 20:29:29 polarbear NetworkManager[877]: <info> (eth1): IP6 addrconf timed out or failed.
Jan 17 20:29:29 polarbear NetworkManager[877]: <info> Activation (eth1) Stage 4 of 5 (IPv6 Configure Timeout) scheduled…
Jan 17 20:29:29 polarbear NetworkManager[877]: <info> Activation (eth1) Stage 4 of 5 (IPv6 Configure Timeout) started…
Jan 17 20:29:29 polarbear NetworkManager[877]: <info> Activation (eth1) Stage 4 of 5 (IPv6 Configure Timeout) complete.
Jan 17 21:17:01 polarbear CRON[2279]: (root) CMD (   cd / && run-parts –report /etc/cron.hourly)
Jan 17 21:48:19 polarbear goa[2395]: goa-daemon version 3.6.0 starting [main.c:112, main()]
Jan 17 22:17:01 polarbear CRON[2728]: (root) CMD (   cd / && run-parts –report /etc/cron.hourly)
Jan 17 23:17:01 polarbear CRON[3044]: (root) CMD (   cd / && run-parts –report /etc/cron.hourly)
Jan 17 23:31:20 polarbear anacron[3408]: Anacron 2.3 started on 2013-01-17
Jan 17 23:31:20 polarbear anacron[3408]: Normal exit (0 jobs run)
Jan 17 23:31:27 polarbear kernel: [10938.411516] usb 1-1.2: USB disconnect, device number 3
Jan 17 23:31:32 polarbear kernel: [10942.680548] usb 1-1.2: new full-speed USB device number 5 using ehci_hcd
Jan 17 23:31:32 polarbear kernel: [10942.775553] usb 1-1.2: New USB device found, idVendor=046d, idProduct=c52f
Jan 17 23:31:32 polarbear kernel: [10942.775563] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jan 17 23:31:32 polarbear kernel: [10942.775569] usb 1-1.2: Product: USB Receiver
Jan 17 23:31:32 polarbear kernel: [10942.775573] usb 1-1.2: Manufacturer: Logitech
Jan 17 23:31:32 polarbear kernel: [10942.778371] input: Logitech USB Receiver as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2:1.0/input/input13
Jan 17 23:31:32 polarbear kernel: [10942.778814] hid-generic 0003:046D:C52F.0003: input,hidraw0: USB HID v1.11 Mouse [Logitech USB Receiver] on usb-0000:00:1a.0-1.2/input0
Jan 17 23:31:32 polarbear mtp-probe: checking bus 1, device 5: “/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2″
Jan 17 23:31:32 polarbear kernel: [10942.781280] input: Logitech USB Receiver as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2:1.1/input/input14
Jan 17 23:31:32 polarbear kernel: [10942.782247] hid-generic 0003:046D:C52F.0004: input,hiddev0,hidraw1: USB HID v1.11 Device [Logitech USB Receiver] on usb-0000:00:1a.0-1.2/input1
Jan 17 23:31:32 polarbear mtp-probe: bus: 1, device: 5 was not an MTP device
^C johnny@polarbear ~ $

You can see where I disconnected and reconnected both my power cable and USB receiver for the mouse. A control c will stop the output. Put this tip away for the one time you may need it and if you ever do please come back and tell us how you used it and if it helped you solve a problem.

penguin2sm3

How To Get Information About Your CPU Using Linux and Bash

Generic CPU

So what do you do when you want to find out information on a CPU and you don’t want to wade through thousands of links? Using most Linux distributions whether they are installed or running live offers you some simple commands for identifying the model and more information.

The first example is extremely easy and provides a lot of information about the CPU. Open a terminal, I am using BASH, and type: lscpu and you will get something similar to this example.

lspcu command

As  you can see there is a lot of information here. You now know it is Genuine Intel in this case, handles 32 bit or 64 bit and what speed it runs and more.

But what if you already know it is a dual core Intel but are not sure what model it is? Can you run a command for just this information? The answer is yes. Open a terminal window and type this command:

 

grep “model name” /proc/cpuinfo and you will get output like this:

grep "model name" /proc/cpuinfo

Now you have the make, type, model and rated speed. These commands will work for virtually all CPU’s and almost all Linux distributions. ( I can’t say all here since I have not tested all CPU’s and distributions but it should hold true.) There may not be many times this information is really needed but if the need arises you now have a way that is much faster than searching for the model specs of the computer.

Which leads us to another short command using grep. What if the model sticker on your computer or laptop is gone or rubbed away and you need the model number for identifying it and troubleshooting a hardware problem? The answer is easy but this time it involves running the command as root or sudo and using the output piped to grep. Open a terminal and switch to root or use sudo like this example.

sudo dmidecode | grep Product

Now you have the make and model of the computer which can greatly speed up troubleshooting, finding parts etc..

I don’t classify these commands as anything more than time savers but sometimes time is already working against you and you need the information fast. If you have some great one liners post one or many in the comments. I like reading about what others use for simple yet important tasks.

 

Bash Command Line Blunder Using rm Not to be Repeated

This is a post the veterans of Linux can laugh about and the newbies better pay attention. It started last week when I announced to my sister-in-law I was going to give her a laptop. Of course said laptop would be sporting Linux. I spent much thought on the specs and distro combination and finally arrived at PCLinuxOS LXDE edition.

The laptop is a not so shiny HP ZE5385US sporting a 2.66 Mhz Pentium 4 and an entire 1 GB of RAM. Not what you would call a screamer but it will still get the job done and has lasted for nine years with only one motherboard change out due to some shoddy soldering by an inept craftsman. Yes that was my blunder too and it only cost me $85. Thanks eBay for coming through on this one.

So it was paramount that the laptop be outfitted with a usable OS and one that was still in the modern category and easy for a non-technical never used Linux before person. I have chosen PCLinuxOS LXDE since it will come with some major benefits. It is quick and low on resources, rolling OS so updates are easy, large upstream as well as its own repositories and it has a good community behind it for support.

So now the story of why has been told and if you have read this far I’m sure you want to know the blunder in the title of the post, ‘Bash Command Line Blunder Using rm Not to be Repeated’, so here it goes. I wans’t fully awake or still in the mid-morning Saturday fog when my wife said, “She will be here in about 30 minutes.” or something along those lines. Well I did not have a CD in the house to burn and I wanted to send her back with a live environment also just in case support was needed. So I popped in a 2Gb thumb drive only to find ArchBang Linux on it. A very cool distro I might add. Instead of just letting Unetbootin, (in most repositories for almost all distros), I thought I would head to the command line, (cli) and get the job done first of clearing the files. This is the command I used as root of course:

desert-bear johnny # rm -R * /media/ARCHBANG/       #This is where the veterans laugh and the newbies need to pay attention.

Looks innocent enough right? Mind you I have read the man file on rm and I have even read said warning on using wild card characters with it. But in my haste I still ran the command as is and was promptly met with a message my Dropbox folder was no longer in sync, would I like to re-sync? I knew instantly what I had done and a click on Nautilus proved it. My entire home directory was gone.Vanished into computer oblivion. Unlike Windows which prepares to delete when faced with such mass deletions and prepares to delete and prepares to delete and prepares to delete, Linux and the rm command is swift and ruthless. Cutting through my home folders like warm swiss cheese.

And to add insult to injury take a look at this screen capture from a few minutes ago.

Thumb Drive with ArchBang Linux

Notice how ArchBang survived. Okay veterans of Linux may laugh again here too. Noobs too if you want to.

To break it down a bit here is what happened and why. The rm command is short for remove or delete and does exactly what it is told to do. If you own the file(s), folders are files in Linux, you can rm them at will. By adding the -R I essentially told the command to go and delete every folder and sub-folder it finds. Well it found quite a few since I added a space followed by the wild card * . This essentially gave carte blanche to the rm command and it zipped by the terminal window only showing errors. Linux will not show a completed command’s output unless specified, usually with a  -v switch. The errors were the lack of ownership on the thumb drive containing ArchBang.

So to end the story my sister-in-law did not get the laptop as promised on time. I spent the entire day and most of the night letting recovery programs do their work. I have now 1000′s of small text files. I have back ups for most of the most important files like financials but I did lose some items like recent pictures and some creative writing.

On a last note here the real saving grace is built into the rm command itself. By default it does not delete ./folders or files. These are hidden by default and have to be explicitly named or given another switch. Lucky for me too since this is the meat of /home when it comes to preferences and things you set long ago but just don’t think about any longer. I also should have entered this command right before the rm command:

desert-bear johnny # cd /media/ARCHBANG

and everything would have been a bit better. I would still have lost some mount points and for all I know maybe more. But I can assure you I am not going to test it without adding the option -i. I hope you enjoyed reading this and had a good laugh as well as learned from my mistake. I laughed and you should too.

Bash Primer

As get to know Linux, you will inevitably use the command line (cli) at some point. Every distribution I have ever installed includes at least one terminal and at least one command interpreter. Most often the interpreter of choice is Bash. I am not going to do a history lesson on Bash other than to say it is an acronym meaning Bourne again shell. If you want to read the history follow the Wiki link above. Bash is a very versatile shell and thus has become widely popular amongst the FOSS community. I cannot think of a distribution that I have run that did not include it by default or at least make it available. Also I should point out that the terminal window itself is not Bash but rather a container for Bash to run in. This is why it might look different from one Linux distro to another.

There are two methods Bash can be run in; as an interactive shell you log into as a user and use the cli exclusively or as an interactive shell from within the graphical environment. Most of the time we are using the latter but if you work on a server, use ssh or are recovering a broken system, then you may find yourself at the login prompt of Bash. I hope that you are not facing that option until you at least have the basics down. System recovery is hard enough on its own let alone trying to remember commands.

To get started using Bash I recommend reading some blogs or websites devoted to Bash. You can also find plenty of books on Bash as well and since the core fundamentals have not changed drastically, even older books found in the used aisles or bargain bins are still very relevant to new users.

To illustrate a simple example you can open a terminal and type pwd. This is the output:

johnny@desert-bear ~ $ pwd
/home/johnny
johnny@desert-bear ~ $

The command pwd provides a method of telling you where you are in the file system. It can be helpful to verify this before running a rm command as root. Notice the prompt is a $ sign. This is the default prompt for an unprivileged user. As root the prompt will turn into the bang symbol # by default. Both of these values and many more can be customized. This is yet another reason that Bash is so popular is that it can be tailored to the user very easily, Any customizations values are stored in the file ~/.bashrc. This file is read upon opening a terminal window using Bash when in an interactive mode.

This versatility and the fact it is found in almost every distro is why you will often see help given in forums as cli commands. They are universal with very few exceptions. In the various distributions graphical environment, the instructions can vary from one distro to another pretty quick. That makes trouble shooting very difficult indeed.

So now we know what it is and the question now is how to use it? Single commands can be run one at a time and for most there will be a help file by typing -h, –help or mancommand‘ (no quotes and substitute command for the command you are needing to know more about). Commands can be connected together using the pipe symbol | and the command following the | symbol will be run after the first command. Commands can be run together as scripts and I’ll show an example later.

There are many websites and blogs devoted to Bash and the cli. I could fill an entire post with nothing but links there are so many. But this is one I have referred to over and over as I progress in my understanding of Bash; LinuxCommand.org. One more site that is sometimes a bit more advanced but gives practical uses of command is All commands | commandlinefu. The first of these is a basic primer moving into some more advanced techniques and the second allows others to post examples of commands they use for a variety of tasks.

Just today I ran across this example ot a Bash script that can be used for the discovery of large files. I needed this at work about six months ago. :) Notice the start of every Bash script is the same; #! /bin/bash . This #! symbol identifies the following commands are a script and /bin/bash is the working path for finding the commands. Open a text editor and paste the following code into it:

#!/bin/bash
# if nothing is passed to the script, show usage and exit
[[ -n "$1" ]] || { echo “Usage: findlarge [PATHNAME]“; exit 0 ; }
# simple using find, $1 is the first variable passed to the script
find $1 -type f -size +100000k -exec ls -lh {} ; | awk ‘{ print $9 “: ” $5 }’

Notice the two # symbols not followed by an !. These are used to comment on the script. To use this script type as root:

chmod a+x findlarge.sh

This sets the file as an executable and you do this in the same directory as the file or include the path. To run the script as root type:

./findlarge.sh / > largefiles.txt &

This will run the script and output to a text file in the current or working directory. The & symbol at the end tells Bash to run this in the background since this can take a while. Of course the variables of path and file size can be modified to suit your needs. Thanks goes out to Jarrod Goddard for sharing the script and Rackspace for sharing it with him.

Have a favorite example of your own or for more discussion, share in the comments.

 

 

 

How to Fix Apt Signatures

Occasionally I run across a great tip on the web somewhere and this is just one of those tips. I can’t remember where it came from so I can’t give credit at this time. This tip will fix bad signatures errors with apt and is assumed to be in a Gnome environment. Please share in the comments if any changes need to be applied for other variants like KDE or XFCE environments. If you see some lines like this after an apt-get update:

The following signatures were invalid: BADSIG….(followed by the lines with errors)

Run the following commands in a terminal window and this will fix them. ($ ans # are the prompt and not part of the commands)

$ sudo -s -H

$ enter password

# apt-get clean

# rm /var/lib/apt/lists/*

# rm /var/lib/apt/lists/partial/*

# apt-get clean

# apt-get update

After the update is finished be sure to close the root prompt # with exit. This should take care of any bad signatures in you apt sources list. If you continue to get errors on specific lines, you might want to examine those lines to ensure they are correct. In most Ubuntu variants you sill see a Software Sources option in the Administration menu. This gives you a graphical tool to edit the lists. In Debian and some Debian variants this might not be an option in the Administration Menu or if you prefer an editor, you can open a terminal and edit via sudo with your editor of choice this file: /etc/apt/sources.list . Another alternate method is to press alt+F2 and bring up the Run Application dialog and preface the choice of editor you want to use with gksu. This will allow you to run the application as the superuser and you can open the sources.list to edit.

One word of caution that should be said, backup the sources.list file before editing no matter which method you choose. Either copy it with a new name like sources.list.bak or do a save as sources.list.bak. It makes things much easier to return if you make a mistake.

Five Commands Beginners Should Learn

Like every OS Linux has many useful commands while using the command line or cli for short and an understanding of some of the basics will go a long way into understanding Linux. For starters I am using the Bash shell on LMDE. This is a short list for beginners and is in no way inclusive or put together in any particular order. I simply picked these at random since they are seen often in forums and blogs. The commands will be prefaced by the # sign if it should be run as root. Otherwise nothing will precede it here. Let’s begin:

  1. sudo – In some distributions this is built in and in others it must be created via a sudoers file. In LMDE and Debian derivatives it is usually built in. This command will precede any command that needs root or admin privileges to run. Once enter is pressed the user will be asked for their credentials and if the proper permissions are there the command will run. An example of usage is: sudo mkdir ~/comedy ; This command will create a folder or directory in the current user’s /home/user tree. Tip: If the sudo is forgotten and the command fails due to permissions, type sudo !! and press enter. The previous command will run with the elevated permissions after entering credentials.
  2. su – Unlike sudo, su is run by itself and elevates the current session to root privileges. In other words any command from this point forward will have the # sign in front of it and is elevated as root. The root password must be given to enter this mode. Most of the time this is not the recommended method for two reasons. One is it is easy to forget that everything is run as root and Linux only gives information on a completed command if it errors or is requested it the command via options. The other reason is that sudo will time out if unused for a period where su will not. An example of usage will be: su followed by entering root’s password. There are valid reasons to us su vs. sudo and we all must decide when that is. Tip: Remember to exit the su session by entering exit.
  3. rm – Quite possible the most dangerous and useful of all command. Simply put, rm removes what it is told to remove. This includes files and directories so caution is in order here. As a side note, all objects in Linux are considered files. To use rm you must use elevated permissions only on files you are not the owner or member of the group that owns the file. An example is: rm -v ~/letter . This will remove or delete the file letter from the users home directory. The optional -v is added for verbose output to the screen. This offers the user feedback on the command and is available on many commands. A dangerous example would be: #rm /*.* . This example will remove all files from the root directory and will not give any feedback nor ask permission. Do not do this!
  4. mkdir – This is a priviledged command and must be run as sudo or su. The advantage to using the cli with this command is you can be very precise as to where the directory is going to be, immendiantly apply permissions and copy or move files to it as fast as you can type. The example above shows a simple use of the command.
  5. man [command] This is the go to for all commands when you need more information on options, syntax or general usage. You can type this command as a normal user followed by the command to identify. The brackets are not needed and an example would be: man ls . The abbreviated output is as follows:

LS(1)                            User Commands                           LS(1)

NAME
       ls – list directory contents

SYNOPSIS
       ls [OPTION]… [FILE]…

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor –sort.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       -a, –all
              do not ignore entries starting with .

       -A, –almost-all
              do not list implied . and ..

       –author
              with -l, print the author of each file
 Manual page ls(1) line 1 (press h for help or q to quit)

I have listed part of this one since it is the foundation of learning the cli. You can also search the Internet for individual commands with man [command] and you should find a link to the official Unix man pages describing the command and lots of other sites with examples of usage.

These are five commands every beginner should know. I have purposely left out more examples and options for the commands, since in my opinion learning is best done by doing . Open a cli and type man followed by the command. This will give the best overview of the command and all the available options. Some commands have shortened help files built in also. Type the command followed by -h or in some cases –help and a shortened version of the most used options will appear. If more help is needed simply do an Internet search with Linux [command] examples and sometimes the distro you are using will narrow the results.

Remember if it isn’t brand spanking new as in today, odds are in your favor of finding answers on the web. I have to end this by saying once again this is definitely not an inclusive list. These are five commands I feel are beneficial to new Linux users. What commands would you recommend?

~Jraz