RICHMOND, Va.—Former Gov. Bob McDonnell was sentenced to two years in federal prison Tuesday after being convicted of accepting $177,000 in loans and lavish gifts in exchange for helping a wealthy donor’s business.

The two-year sentence was shorter than the decade requested by prosecutors, who said a lengthy prison term would help restore a cynical public’s faith in the political system. But the penalty was stiffer than the community-service sentence requested by Mr. McDonnell, who asked to serve his time working with a faith-based...