President Barack Obama has branded the killing of the US ambassador to Libya and three other Americans as an "outrageous attack".

He vowed to track down the perpetrators, while ordering a tightening of diplomatic security worldwide.

Christopher Stevens and the embassy staff were killed in an  attack on the US consulate and a safe house refuge in Benghazi overnight.

US government officials said the attack may have been planned in advance.

They said there were indications that members of a militant faction calling itself Ansar al Sharia - which translates as Supporters of Islamic Law - may have been involved.

Washington ordered the evacuation of all US personnel from Benghazi to Tripoli and was reducing staffing in the capital to emergency levels.

Gunmen assaulted the Benghazi compound, clashing with Libyan security forces, who withdrew under heavy fire amid a protest over a film they said insulted the Prophet Muhammad

Two US personnel were killed at the safe house and two others died at the main consular building. A Libyan official said up to 17 people were wounded.

Mr Stevens, a 21-year veteran of the foreign service, was one of the first US officials on the ground in Benghazi during the uprising against Muammar Gaddafi last year.

An Arabic and French speaker, he served as Deputy Chief of Mission in Tripoli between 2007 and 2009, in the waning years of Gaddafi's brutal rule in the oil-rich country.

As the country dissolved into civil war, he was appointed the US envoy to the Transitional National Council, which was coordinating the revolt against Gaddafi, and returned aboard a Greek cargo freighter that docked in Benghazi in April, 2011.

Sean Smith, a foreign service information management officer, was identified as one of the diplomats killed.

The names of the two others were withheld while the US government notified their families.

President Obama said: "Right now, the American people have the families of those we lost in our thoughts and prayers.

"They exemplified America's commitment to freedom, justice, and partnership with nations and people around the globe, and stand in stark contrast to those who callously took their lives.

"I have directed my administration to provide all necessary resources to support the security of our personnel in Libya, and to increase security at our diplomatic posts around the globe.

"While the United States rejects efforts to denigrate the religious beliefs of others, we must all unequivocally oppose the kind of senseless violence that took the lives of these public servants."

US Secretary of State Hillary Clinton also condemned the attack as the work of a "small and savage group" but said US-Libyan ties would not suffer.

Separately, Chairman of the US Joint Chiefs of Staff General Martin Dempsey called on Florida pastor Terry Jones to withdraw his support for the film at the centre of the protests.

US military officials are concerned that the film could inflame tensions in Afghanistan, where 74,000 US troops are stationed.

The Taliban called on Afghans to prepare for a fight against Americans and urged insurgents to "take revenge" on US soldiers over the film.

"He told me he had seen the film and that the film was pornographic ... and very, very bad. He asked me not to support it," Pastor Jones told Reuters.

He agreed to "re-evaluate" his plans to show the film. "If the film is indeed pornographic, then, of course, as a Christian pastor I cannot support that type of film and could not show it," he said.

The violent protests in Libya came hours after thousands of demonstrators gathered outside the US embassy in the Egyptian capital Cairo to express their anger at the same film.

At one point some of the protesters entered the embassy compound and tore down the US flag.

Afghanistan bans YouTube over film

Afghanistan has banned YouTube so Afghans would not be able to watch the film, the Ministry of Communications said.

"We have been told to shut down YouTube to the Afghan public until the video is taken down," said Aimal Marjan, general director of Information Technology at the ministry.

In the past, material and actions deemed insulting to Islam have sparked deadly riots in Afghanistan.

Afghan President Hamid Karzai sharply condemned the film in a statement, calling its making a "devilish act", saying he was certain those involved in its production represented a very small minority.

For many Muslims it is blasphemous to depict the Prophet and the incident had echoes of the publication in a Danish newspaper of cartoons that touched off riots in the Middle East, Africa and Asia in 2006 in which at least 50 people died.

YouTube, which is owned by Google, said it would not remove the film clip but it has blocked access to it in Egypt and Libya.

"This video - which is widely available on the Web - is clearly within our guidelines and so will stay on YouTube," Google said in a statement.

"However, given the very difficult situation in Libya and Egypt, we have temporarily restricted access in both countries. Our hearts are with the families of the people murdered in yesterday's attack in Libya".

Meanwhile, Tunisian police fired teargas and rubber bullets into the air to disperse a protest over the film near the US Embassy in the capital Tunis.

Police chased the protesters away while Tunisian army soldiers guarded the embassy building.

There were no immediate reports of casualties.