In a second "green-on-blue" attack on Friday, in the south, an Afghan security force member turned his weapon on other international service personnel, wounding two American soldiers, according to NATO and Afghan officials.

The two American service members who died were part of a Special Operations team working with local police in Farah Province in the west of Afghanistan, according to a NATO official who spoke on condition of anonymity because he was not authorized to comment on the attack.

The Americans belonged to United States Forces-Afghanistan, a command separate from the main NATO force.

Special Operations forces are working closely with Afghan forces on the Afghan Local Police initiative, a group trained and financed by the United States and viewed as an important stopgap to secure remote corners of Afghanistan as international troops withdraw. Another American Special Forces service member was wounded and an Afghan police recruit was killed in the shooting, said Aqa Noor Kentoz, the police chief of Farah province.

Mr. Kentoz identified the attacker. "When the training finished, the ALP soldier Mohammad Ismail turned his weapon toward our allies and killed two of them," Mr. Kentoz said.

In the second attack, in Kandahar Province in the south, a member of the Afghan security forces shot at NATO service members. Nobody was killed by the gunman, but some soldiers were wounded, said Maj. Martyn Crighton, a spokesman for the NATO-led International Security Assistance Force in Kabul. Major Crighton did not give the nationality of those NATO service members who were injured. But another coalition official, who spoke anonymously because he was not authorized to comment on the nationality of those injured in the attack, said the shooting wounded two American troops.

In both cases, in Farah and Kandahar, the shootings were carried out by individual attackers, and both were shot and killed, said Major Crighton.

The shootings were the latest in a spate of attacks by Afghan forces on their coalition counterparts.

These assaults have intensified in recent years in Afghanistan, where the military calls them "green-on-blue" attacks.

With the two episodes on Friday morning, there have now been at least 31 such attacks in Afghanistan so far this year, including 21 that have resulted in fatalities.

All told, the deaths of at least 39 NATO troops during the first eight months of 2012 have been attributed to these shootings.

Those numbers for deaths and attacks already surpass the figures for green-on-blue attacks for all of 2011.

The increase in the rate of this kind of violence has prompted Afghan and NATO military leaders — who are worried about the impact on morale and the propaganda boost the attacks give to the insurgents — to investigate the circumstances surrounding each one.

Defense Secretary Leon E. Panetta this week suggested that the Taliban were at least partly behind the increase in the violence.

"The reality is the Taliban has not been able to regain any territory lost, and so they're resorting to these kinds of attacks to create havoc," Mr. Panetta said.

But the precise reasons for many of the shootings have been difficult to ascertain. In fact, inside NATO, analyses and investigations conducted of recent green-on-blue episodes have found that only about one out of every 10 such attacks are clearly the result of insurgent activity.

Major Crighton said that the attacks on Friday were not carried out by Taliban infiltrators wearing Afghan uniforms, but were members of the Afghan security forces, according to the preliminary investigation.

"Operational reporting confirms that both cases involved members of the Afghan national security forces," he said.

Abdul Rahman Zowandi, a spokesman for the Farah governor's office, said the attacker in Farah, Mohammad Ismail, was a 60-year-old man who had been hired only two weeks earlier to train as a local police officer.

Mr. Zowandi said the attacker opened fire on the American soldiers as they were training his local police unit in the Bala Bolok district of Farah province. He said the local police unit had been established only two weeks ago, and the police officers had all been hired from the local district.

In a statement on the Farah shooting, United States Forces — Afghanistan said: "Two U.S. Forces-Afghanistan service members died this morning as a result of an insider threat attack in Farah Province."

It said: "Officials are investigating the incident to determine the facts, and as more information becomes available it will be released as appropriate."

An employee of The New York Times and Habib Zahori contributed reporting from Kabul