A common cause for head shaking is otitis externa, an inflammation of the external ear canal.
Your dog may moan with relief when you rub his ears, or yelp in pain from a light touch.
If there's a secondary infection, you may notice some discharge in the ears and even a nasty scent wafting from inside the ear canal.