Outgoing Gen. John R. Allen speaks of progress made during his tenure. 'This is what winning looks like,' Allen says. Dunford stresses continuity.

KABUL, Afghanistan — Gen. Joseph F. Dunford Jr. took over Sunday as the newest and probably last U.S. commander in Afghanistan, charged with ending America's longest war even as insurgents continue to challenge the U.S.-backed Afghan government.

Dunford, a four-star Marine officer, arrives as the U.S.-led NATO coalition has closed three-quarters of its 800 bases and as it watches to see whether the Afghan security forces it trained can keep the Taliban insurgency at bay.

A ceremony inside the coalition's heavily guarded compound in Kabul marked the end of the 19-month tenure of Gen. John R. Allen, whose command was marred by a rash of deadly "insider" attacks by Afghan forces against their U.S. and NATO trainers and by strained relations with Afghan President Hamid Karzai.

But in remarks tinged with emotion Sunday, Allen pointed to significant progress, including the growth of the Afghan security forces, an increase in Afghan-led military operations, a sharp reduction in civilian casualties and the withdrawal of about 35,000 U.S. troops.

"This is victory," Allen said. "This is what winning looks like, and we should not shrink from using those words."

Allen was cleared of wrongdoing last month in a Pentagon inquiry into emails he exchanged with a woman who was linked to the sex scandal that forced the resignation of CIA Director David H. Petraeus. Allen has been nominated to lead North Atlantic Treaty Organization forces in Europe.

By replacing Allen with Dunford, the respected but low-key assistant commandant of the Marine Corps, President Obama hopes to repair ties with Karzai so they can cement a long-term security deal that could see several thousand U.S. troops remain in Afghanistan beyond the withdrawal of combat forces next year.

Embracing Allen at the ceremony, Dunford stressed continuity in the mission.

"What's not changed is the will of this coalition," he said. "What's not changed is the growing capability of our Afghan partners."

Obama is expected to spell out plans for the troop withdrawal and a post-2014 U.S. military presence in Afghanistan as early as his State of the Union message Tuesday. Although White House officials have said it's possible that no U.S. troops would remain, Pentagon officials are calling for a residual force that would focus on counter-terrorism and supporting Afghan forces.

Dunford will have a key seat at the table as U.S. officials try to work out the security agreement, which will hinge on earning assurances from Afghan leaders that they won't release prisoners currently in U.S. custody and will guarantee U.S. troops immunity from prosecution in Afghan courts. The failure to reach an immunity guarantee was a main reason no U.S. troops remained in Iraq after the war ended there.

About 65,000 U.S. troops remain in Afghanistan, down from a high of 100,000. Despite flagging U.S. support for the war, military commanders contend that removing the remaining troops precipitously could cause Afghan security forces to collapse.

In his Senate confirmation hearing in November, Dunford offered no prescriptions for troop levels but cautioned against withdrawing too quickly, saying it could destabilize the region.

U.S. officials recently estimated that a residual American force could number from 6,000 to 9,000 troops — fewer than the 15,000 senior military commanders had wanted. Experts say that Dunford will be charged with figuring out how such a force could achieve U.S. strategic aims.

"A major challenge will be identifying a mission that those troops can perform that's useful and doable with that small number," said Stephen Biddle, a defense analyst and professor at George Washington University.

Even as the war winds down, challenges remain. The insider attacks that killed 61 NATO troops in 2012 have dissipated, but only after U.S. troops scaled back joint operations with Afghan forces, hampering training efforts. By next year, Afghan forces are expected to be in the lead of all security operations, but the Taliban, though weakened, retains the ability to attack in Kabul and other strategic areas.

Experts say that Dunford, who earned the nickname "Fighting Joe" when he led a charge from Kuwait into Baghdad during the 2003 invasion of Iraq, could clash with a second-term Obama Cabinet, whose members — including Secretary of State John F. Kerry and, if he's confirmed, Chuck Hagel as Defense secretary — have not been strong supporters of a large long-term U.S. presence in Afghanistan.

"It's going to be extremely difficult for a commanding general who's not going to have many partners in the administration," said Thomas Donnelly, a military expert at the American Enterprise Institute, a conservative Washington think tank.