There's a fair few ways of doing this, but the easiest is probably the following:
Install "pavucontrol" and "audacity" from ubuntu software centre or via terminal. (
sudo aptitude install pavucontrol audacity or
sudo yum install pavucontrol audacity).
Open up Audacity (from the menu or via a terminal with
Select "pulse*" as recording device in Audacity.
Click Record Button.
Open PulseAudio Volume Control (Search For PulseAudio Volume Control in the menu or dash).
Select Recording Tab.
Now You Should see ALSA plug-in [audacity]. If you do not see ALSA plug-in [audacity], make sure you have clicked Record Button in audacity. You should be running recording in audacity while performing the next step.
Select "Monitor from *".
Now you should be able to record the sound you hear from speaker or headphones.