KABUL, Afghanistan (www.nytimes.com)— For the first time since Vietnam, a United States Army general was killed in an overseas conflict on Tuesday when an Afghan soldier opened fire on senior American officers at a military training academy.

Maj. Gen. Harold J. Greene Credit U.S. Army

Maj. Gen. Harold J. Greene Credit U.S. Army

The slain officer, Maj. Gen. Harold J. Greene, was the highest-ranking member of the NATO-led coalition killed in the Afghanistan war, and his death punctuated the problems vexing the Americans as they try to wind down the 13-year-old conflict, contending with a political crisis that has threatened to splinter the Afghan government and leave it unable to fend off the Taliban.

The general was among a group of senior American and Afghan officers making a routine visit to Afghanistan’s premier military academy on the outskirts of Kabul when an Afghan soldier sprayed the officers with bullets from the window of a nearby building, hitting at least 15 before he was killed.

Though American officials said General Greene was not believed to have been specifically targeted, his violent death at the hands of an Afghan soldier, not an insurgent, was a reminder of the dangers faced by even the highest-ranking, and best protected, officers in Afghanistan.

Driving home the threat, an Afghan police officer opened fire on American soldiers visiting the governor of Paktia Province in eastern Afghanistan soon after the shooting at the military academy, Afghan and coalition officials said. The policeman was fatally shot; none of the Americans were wounded.

There was no indication that either of the attackers were members of the Taliban, or that their acts were coordinated. The insurgents did not claim the attackers as their own, instead hailing them as hero soldiers. American officials said they had no reason to suspect the gunman at the military academy was anything but an ordinary Afghan soldier whose motivations remained a mystery.

But scores of these so-called insider attacks have plagued the American military in recent years, and Afghan and American commanders believe the vast majority have been carried out by Afghan soldiers and police alienated and angered by the protracted war in their country, and the corrupt and ineffectual government that the United States has left in place. Few of the attacks are believed to have been results of coordinated Taliban plots.

Rear Adm. John F. Kirby, the Pentagon spokesman, said officials believed the gunman was “a member of the Afghan national security forces,” but he had no other details about him or the circumstances of the shooting.

Still, Admiral Kirby maintained on Tuesday that the insider attack, the first in months, would not change the Obama administration’s plans to leave a residual force in Afghanistan after most American forces are withdrawn at the end of the year and the NATO combat mission here formally concludes.

Admiral Kirby emphasized the progress that Afghan forces had made in recent years, citing as examples their role in limiting violence in the presidential election in April and the June runoff vote.

Yet the shooting was a blunt reminder that discipline and vetting remain a challenge, and rogue Afghan soldiers and policemen remain a threat, despite a sharp drop in insider attacks since 2012, when the violence peaked and dozens of coalition service members were killed by Afghan counterparts.

With foreign troops having largely ceded their front-line role to Afghan forces in the past two years, training and advising Afghans is one of the few crucial roles still played here by the coalition. American soldiers largely stay out of the Taliban’s line of fire, but they must still maintain close contact with Afghan soldiers and policemen. Foreign forces have few options for protecting themselves, short of cutting off contact with the Afghans.

But that would make the training mission impossible, as General Greene, 55, most likely knew. 

He was one of the most senior officers overseeing the transition from a war led and fought by foreign troops to one conducted by Afghan forces. His specialty was logistics — he was a longtime acquisitions officer — and he had been dispatched to Afghanistan to help the Afghan military address one of its most potentially debilitating weaknesses: an inability to manage soldiers and weaponry.

Compared with the infantry grunts who did tours of duty in the Taliban-infested hinterlands of Afghanistan, General Greene had an assignment that appeared to carry far less risk. Yet on Tuesday, he became one of the more than 2,300 American service members killed in Afghanistan.

Admiral Kirby said his death reflected an inherent risk of the war, calling insider attacks “a pernicious threat and always difficult to ascertain.”

“Afghanistan is still a war zone,” he said.

The attack occurred around noon and sent shock waves through the coalition command and Afghanistan’s defense establishment, bringing nearly all other work to a halt. In the immediate aftermath, Gen. Joseph F. Dunford Jr., the commander of coalition forces, ordered all foreign advisers and trainers out of Afghan government ministries and back to their bases.

At the military academy, foreign troops appeared on edge, fearful of another attack.

Massoud Hossaini, a photographer for The Associated Press, said he had arrived at the camp’s gate ahead of other journalists, and just as coalition armored vehicles were departing. A coalition soldier manning a roof-mounted gun on one vehicle shouted for Mr. Hossaini to “get away,” then fired what was apparently a warning shot.

“I don’t know what he fired; it was fired near our car,” Mr. Hossaini said, adding that he quickly left the scene.

In images released by The A.P., the gunner appeared to be firing a warning flare.

“I haven’t seen anything like this since 2010,” when an American and a Canadian colonel and two American lieutenant colonels were killed in a suicide car bombing, said an American official in Kabul. There was “more than a little shock and numbness” at coalition headquarters.

Another coalition official compared General Greene’s death to the killings of American advisers at Afghanistan’s Interior Ministry by an Afghan government employee in 2012, an attack that led many Western officials here to question the viability of their purpose. That attack came amid a wave of anti-American violence over burnings of Qurans at Bagram Air Field, a sprawling base north of Kabul.

There was no similar incident before Tuesday’s shooting. The general and the other Afghan and American officers were standing by a water purification tank when the Afghan soldier opened fire without warning, a senior Pentagon official said.

Afghan forces responded quickly, gunning down the assailant, and the shooting was over nearly as quickly as it began, Afghan and coalition officials said.

A German brigadier general and a senior Afghan commander were among the wounded, Afghan and coalition officials said, though they declined to identify any of the Western victims by name. The identity of the gunman was not disclosed either.

Late into the night on Tuesday, many details of the shooting were unclear, and the coalition, in an official statement, would only confirm that one of its service members had been killed in what it described as “an incident” at the Marshal Fahim National Defense University in Kabul.

Until Tuesday, no officer in the American military of major general rank or higher had been reported killed by hostile action abroad since the Vietnam War.

According to the Vietnam Veterans Memorial database, Maj. Gen. John Albert B. Dillard Jr. was killed on May 12, 1970, when his helicopter was shot down. Rear Adm. Rembrandt Cecil Robinson, the Navy’s equivalent of a brigadier general, was killed on May 8, 1972, when his helicopter crashed. Five other American officers of comparable rank were killed in the war, all in air crashes, whether accidental or caused by hostile action.

Lt. Gen. Timothy L. Maude, who was the Army’s deputy chief of staff for personnel, was killed during the Sept. 11, 2001, terrorist attack on the Pentagon, according to the United States Army War College in Carlisle, Pa.