Standing guard at his frontline post, Libyan soldier Mohammed Abu Shager can see where Islamic State militants are holed up with their heavy weaponry less than a kilometre away.The militants have effectively taken over former dictator Muammar Gaddafi's home city of Sirte as they exploit a civil war between two rival governments to expand in North Africa.

"Every night they open fire on us," said Abu Shebar, who with comrades on Sirte's western outskirts holds the last position of troops belonging to one of the two warring Libyan governments, the General National Congress, which controls the capital Tripoli and most of the west of the country.

"They are only active at night," he said, pointing to the militants' position in a house just down the road blocked by sandbags. He sleeps in a shed next to his firing positions where used tank shells litter the ground.

Libya, which has descended into near anarchy since NATO warplanes helped rebels overthrow Gaddafi in a 2011 civil war, is now the third big stronghold for the Sunni Islamist group, also known as ISIS or ISIL, which declared a Caliphate to rule over all Muslims from territory it holds in Syria and Iraq.

Islamic State fighters became a major force last year in Derna, a jihadi bastion in Libya's east, and quickly spread to the biggest eastern city Benghazi, where they have conducted suicide bombings on streets divided among armed factions.

By occupying Sirte over the past four months they have claimed a major city in the centre of the country, astride the coastal highway that links the east and west.They made their presence known to the world in February by kidnapping and beheading more than 20 Egyptian Christian oil workers on a beach and posting video on the Internet.

In Libya, the group deploys locally-recruited fighters, led by envoys sent from Syria and Iraq. Those include Libyans returned from fighting on Syrian and Iraqi frontlines, steeped in the group's ethos of extreme violence and permanent warfare between those it considers true Sunni Muslims and all others.

Their gains in Libya, just across the sea from Italy, are worrying European governments and north African neighbours. But so far Western countries, which are bombing Islamic State positions in Syria and Iraq, have steered clear of that sort of intervention in Libya.

BREAKDOWN

Islamic State's expansion in Libya has been helped by a breakdown of state authority.

Neither of Libya's two warring governments exercises much formal control of territory. Both field troops that call themselves armies but are in fact loose alliances of former rebels who toppled Gaddafi, refused to disarm, and have since fallen out along tribal, political and regional lines.

Both governments pay fighters with cash from Libya's oil exports, giving them funds and incentive to fuel the war indefinitely.

Islamic State opposes both governments, exploiting local resentments and power vacuums. It took Sirte from the government based in Tripoli, which draws its support mainly from fighters from the western city of Misrata, who emerged as some of the most powerful in the country after Gaddafi's fall.

Islamic State gunmen arrived in the area in pickup trucks in February when the Misrata forces were busy 150 km to the east trying to wrestle away Libya's biggest oil port, Es Sider, from forces backing the other government, now based in the east.

With Misrata troops having spread out on front lines stretching 1,000 km, militants swiftly seized a Sirte hospital, a university, the grand Ouagadougou hall where Gaddafi once hosted African leaders and a radio station broadcasting Quranic verses.

When the Misratis returned in force to Sirte in March after failing to seize Es Sider, Islamic State had already set up checkpoints. The jihadists have since steadily widened their control. The last checkpoint held by the Misratis is now about a kilometre further from the city centre than it was when Reuters visited two months ago.

"They are now shelling the power station so we've moved back the last checkpoint for civilians," said Yuhami Ahmed, a commander of the Misrata troops based on the western outskirts near a plant that supplies the area with electricity.

The Misrata forces have surrounded Sirte and are diverting traffic on the coastal road to the desert hinterland. Anti-aircraft guns guard checkpoints.

Sirte residents who pass between the two frontlines to get petrol in suburbs under control of the Misrata forces describe hardship inside a city no longer served by the state oil firm.

"We only have power sometimes," said the owner of a cafe at a petrol station who gave his name as Salah.

Another resident fetched water in a closed restaurant used by the Misratis as a rest area. He said he had no water at home.

GADDAFI LOYALISTS

The Misrata forces compare the standoff to 2011, when Gaddafi made his last stand in Sirte while they besieged and shelled it. Gaddafi was eventually captured and lynched by rebels outside Sirte after trying to escape on the same road again blockaded by the Misratis.

Yuhami, the Misrata commander near the power plant, said their new opponent Islamic State was strong because of the backing of Gaddafi loyalists and foreigners. "They have been joined by foreigners, Sudanese, Tunisians, Egyptians, Yemenis," he said, standing in front of Toyota truck, the standard vehicle of his troops. "They have 106's," he said, referring to large calibre guns.

He and several of his men put the number of Islamic State militants in Sirte at more than hundred. So far, Islamic State has not gained territory as quickly in Libya as it did in Iraq and Syria, where it portrays itself as defenders of Sunni Islam in sectarian wars against governments led by Shi'ite Muslims.

Libyans are overwhelmingly Sunni Muslims, and their divisions tend to be tribal and regional rather than sectarian. Islamic State fighters have had to compete with rival Libyan militant groups who resent the presence of outsiders.

But Sirte, where homes were looted by Misrata rebels after Gaddafi's fall, is fertile ground. Many residents feel they were losers in the revolution and harbour resentment towards the Misrata fighters.

"Before the revolution life was so much better. We had electricity, security. Schools were always open," said Mohammed Ali, a student living in a suburb near the power plant.

"They (Islamic State) are fine. They leave you alone unless you fight them," he said.

He said he had seen Tunisians and other foreigners joining the group, and also Gaddafi loyalists. That would be a similar pattern to Iraq, where former officers from secular dictator Saddam Hussein's army have supported Islamic State.

The group has managed to stage suicide bombings on Misrata forces near the power plant and at highway checkpoints, including one on the outskirts of Misrata which frightened residents.

"We are worried about Daesh," said Ali al-Mahdy, a bookshop owner in central Misrata, using an Arabic acronym for Islamic State which the group considers derogatory. "We need to fight them.".