* President was abroad at time of attack, PM safe
* Al Shabaab claims strike, warns of more attacks
* Bomber infiltrated govt side this month - Shabaab (Adds govt, Shabaab comments)
By Abdi Sheikh
MOGADISHU, Jan 29 (Reuters) - An Islamist militant persuaded Somali government officials he had defected, then walked up to the president's palace compound on Tuesday and blew himself up near the gates, killing at least two soldiers, witnesses and rebels said.
Al Qaeda-linked al Shabaab rebels said the early morning suicide attack was the start of a new campaign against the country's Western-backed government and its leaders.
Palace officials said Somali President Hassan Sheikh Mohamud was abroad at the time and Prime Minister Abdi Farah Shirdon Saaid, whose house was near the site of the explosion, was not injured.
The attack was a reminder of the threat still posed by al Shabaab, even after African peacekeepers pushed it out of the capital last year and forced it to retreat to the south.
It was the first explosion reported this year in Mogadishu, where the fragile government is struggling to impose any sense of order more than two decades after the fall of dictator Siad Barre tipped the country into chaos.
Government officials said the attacker, Ali Abdi Hared, told them he had defected from the rebels and went on to join the country's security services on Jan. 14.
Hared frequently visited the palace guards and was given an official pass to the palace, officials and guards at the scene said.
The government had been "rehabilitating and investigating" Hared, Somalia's information minister, Abdullahi Ilmooge, told Reuters. "The bomber was a national security soldier. The bomber had defected from al Shabaab," he added.
"A RENEWED CAMPAIGN"
When Hared walked up on Tuesday morning, the guards said they carried out a routine check and found he was wearing an explosive jacket.
The guards tried to prevent him from detonating his device, but it went off. "The man blew up himself near a wall between the Ethiopian embassy and the Somali PM's residence," Ahmed Ali, a Somali soldier at the presidential palace told Reuters.
"One guard died there and then. Another died of his wounds. They were all the guards of the prime minister," he added.
Al Shabaab - which wants to impose its strict version of Islamic law, or Sharia - said it killed seven soldiers in the attack and warned there would be more to come.
"The operation is part of a renewed campaign of attacks against Western puppets in Somalia, reminding them that the Mujahideen will get into them, whatever security precautions they take," the movement said in a statement.
Al Shabaab pulled back to southern Somalia after quitting Mogadishu. In late September Kenyan troops forced it to withdraw from the port of Kismayu, its last major urban stronghold in the Horn of Africa nation.
On Jan. 17 the group said it had executed a French captive after a French commando mission to rescue him failed.
Somalia's president and prime minister were elected last year in the country's first national vote since Siad Barre's overthrow in 1991. (Additional reporting by Feisal Omar; Writing by James Macharia; Editing by Andrew Heavens)