No it is not necessary that you should replace all the 4 shocks together at once, but you can replace them in pairs like both rear shocks at once or both front struts together. This is because the new shocks will be able to reduce the road bumps better than the old ones, and when you will replace them in a pair, it will provide better stability and performance on rough terrains. The replacement of all four shocks at once depends on the wear. Sometimes, when you mix the old and new ones together, they can have a undesirable effect on wear, leading to more damage. So it is crucial to check the condition fo wear first and then determine whether all four needs to be replaced together or not.