WASHINGTON — The Senate overwhelmingly confirmed Mark T. Esper as secretary of defense on Tuesday, ending the longest period by far that the Pentagon had been without a permanent leader.

Mr. Esper, an Army infantryman who fought in the Persian Gulf war of 1991 before becoming a lobbyist for the military contractor Raytheon, replaces Jim Mattis, who resigned in December during a dispute over pulling American troops out of Syria.

In receiving the lopsided 90-to-8 Senate nod, Mr. Esper succeeded where Patrick M. Shanahan, President Trump’s original pick to replace Mr. Mattis, did not; Mr. Shanahan abruptly resigned last month, before his Senate confirmation hearing was even scheduled, after news reports revealed details of his 2011 divorce.

Mr. Esper, 55, now takes control of the country’s 1.2 million active-duty troops and one of the largest militaries in the world as the Trump administration is wrestling with the results of its so-called maximum pressure campaign of economic sanctions on Iran, which has prodded the two adversaries closer to military confrontation.