Photo Dr. Vivek H. Murthy Credit Charles Dharapak/Associated Press
WASHINGTON — The Senate on Monday confirmed President Obama’s nominee for surgeon general, ending a long fight over gun control and politics that had left the country without a permanent top doctor for a year and a half.

In an early-evening vote, Dr. Vivek H. Murthy, an internal medicine physician, was approved 51 to 43.

The confirmation was one of the last acts of business for the Democratic-controlled Senate. Had it not moved, the nomination would have died, making it unlikely the president would have been able to get anyone approved to the post for the remainder of his time in the White House.

Democrats said there was no compelling reason to delay the Murthy nomination, especially as the nation’s public health system remains on edge after the Ebola outbreak in West Africa and a number of recent scares at American hospitals. In a statement applauding the vote, Mr. Obama directly cited the threat from Ebola and said that Dr. Murthy’s confirmation would help make the country safer.

“Vivek’s confirmation makes us better positioned to save lives around the world and protect the American people here at home,” the president said.

But Republicans saw Dr. Murthy as a politically connected supporter of Mr. Obama’s who would use his position to push for stricter gun control. Though the surgeon general’s office does not set or carry out gun policy, Dr. Murthy’s support for banning the sale of certain weapons and ammunition, as well as for longer waiting periods for firearms sales, drew the ire of gun rights advocates like the National Rifle Association.

The association said it would include the vote in its calculation to determine how senators fare on its annual scorecard, an attempt to dissuade lawmakers from voting yes. Earlier this year as many as 10 Democrats were considering voting against Dr. Murthy. Their wavering forced the White House to ask the majority leader, Senator Harry Reid, to delay a vote in March.

But in the end, the political immunity of lame duck status was too much to overcome — even for the N.R.A. Senators Mark Begich of Alaska, Mary L. Landrieu of Louisiana and Mark Pryor of Arkansas, all Democrats who lost in November, voted for Dr. Murthy.

Other Democrats from conservative-leaning states who voted yes included Senators Jon Tester of Montana and Claire McCaskill of Missouri. Just one Republican, Mark S. Kirk of Illinois, voted for the nomination.

Democrats who voted no were Joe Manchin III of West Virginia, Heidi Heitkamp of North Dakota and Joe Donnelly of Indiana.

None of what Dr. Murthy said on guns was out of step with the American public opinion, surveys show. He favored more restrictions on who can own guns and how they can be purchased, views he said stemmed from his experience seeing the effects of gun violence firsthand as a physician in the emergency room.

Senator Richard Blumenthal, Democrat of Connecticut, said that Dr. Murthy was being punished for telling the truth.

“The simple fact is gun violence impacts far too many people,” he said, running down a list of statistics about gun deaths. “Pointing out these facts and asking whether there are strategies we could apply to bring that number down is exactly what a person tasked to keep Americans healthy ought to be doing.”

Senator John Barrasso, Republican of Wyoming and a surgeon, said Dr. Murthy was more advocate than physician.

“Americans want the same thing from a surgeon general that they all want from their own doctors,” he said. “People want honest and straightforward advice about medical dangers like cancer, like heart attacks, like stroke. They don’t want an inexperienced, unqualified political appointee.”

In addition to his stance on guns, Dr. Murthy’s background troubled Republicans and some Democrats. He helped found a group called Doctors for Obama that supported the president’s 2008 campaign and later fought for the passage of the Affordable Care Act under the new name Doctors for America.

Mr. Manchin, the West Virginia Democrat, said he believed that Dr. Murthy’s close political association to the president would be difficult for people to see past. “I truly do not believe that he will be able to do the job that needs to be done in such difficult and challenging times because of his political involvement,” Mr. Manchin said.

Dr. Murthy teaches at Brigham and Women’s Hospital and Harvard Medical School.