CONFRONTING THE CALIPHATE | This is part of an occasional series about the rise of the Islamic State militant group, its implications for the Middle East, and efforts by the U.S. government and others to undermine it.

A protester holding the Libyan flag participates in a political demonstration in Misurata, where militias have been preparing an offensive against the Islamic State in nearby Sirte. (Javier Manzano/For The Washington Post)

At first, the Islamic State fighters confined themselves to charity work, holding contests for Koranic recitation and distributing money to help locals marry. In the initial months after they appeared in Sirte, the home town of former Libyan dictator Moammar Gaddafi, early this year, the fighters were mostly seen directing traffic or settling local disputes. They even sold oil to residents below cost.

“The majority of residents saw them as good people,” said one activist from Sirte, an arid seaside city less than 100 miles along the coast from here. But gradually, the militants revealed a more severe form of rule, harsher than other Islamist groups that had preceded them.

Sirte today is a subdued, fearful place, residents say, where the streets are deserted after dusk. Militants have closed banks and schools. Music and smoking are outlawed, and violations of minor rules are met with fines or beatings. More ominous, Islamic State fighters have shown levels of violence previously unseen in Libya.

“We woke up one morning and you could feel something was wrong. Something was different,” said Mohamed, who like others from Sirte agreed to be identified only by his first name. “That’s when they started hunting down security forces.”

Mohamed and his family fled Sirte this summer after the Islamic State kidnapped his brother, a coast guard official, and subjected him to a brutal public execution. Soldiers and police are not the only targets. A video released in February showed militants beheading 21 Egyptian Christians, mostly migrant workers, on a beach near Sirte.

Since early this year, militants fighting under the Islamic State’s black banner have consolidated their grip on Sirte. With the struggle against the Islamic State intensifying in Iraq and Syria, the United States and its allies are watching carefully to see whether the group’s smaller operations, including affiliates in Egypt and Yemen, can take hold. A significant test is unfolding in Sirte — across the Mediterranean from Europe’s southern shores — where the Islamic State has established its strongest franchise yet.

Local and Western officials describe the Islamic State, also known as ISIS or Daesh, as a hybrid operation in Libya, led partly by foreign fighters and benefiting from the experience the organization has gained in its expansion across Iraq and Syria.

But it remains at heart a local movement, one that has capitalized on divisions among existing Islamist groups and drawn strength from the unresolved struggles that have pushed Libya into a complex civil war.

The country is broken in every way imaginable, as rival governments in the capital, Tripoli, and the eastern city of Tobruk have vied for resources and legitimacy. On Thursday, a U.N. envoy released the names of politicians who he hopes will form a new national unity government after months of negotiations. If the formation of a unity government succeeds, Libya’s future leaders will face a host of problems. The country is nearly bankrupt. Officials are unable to contain a deadly human smuggling trade. Powerful militias battle each other.

Today, militants are setting up an Islamic court in Sirte and have reportedly demanded that anyone who has worked in traditional banks make amends for working in an un-Islamic financial system. At the local university, the group is altering or doing away with university curriculums deemed un-Islamic, such as law, psychology and social studies.

“They’re right, and everyone else is wrong,” said one Sirte resident, who gave his name only as Waleed.

Back to Libya

In 2012, Libyan jihadists in Syria established the Battar Brigade. After returning to Libya in 2014, some of those fighters allied themselves with the Islamic State’s self-proclaimed caliph, Abu Bakr al-Baghdadi.

Around the same time, the Islamic State’s leadership dispatched several senior militants to Libya, among them a former Iraqi police officer and Baghdadi deputy named Wisam al Zubaidi, or Abu Nabil al-Anbari.

As they did in Iraq and Syria, Islamic State leaders in Libya sought to exploit divisions among Islamist groups to attract militants to their own side. In Derna, a city east of Benghazi, that strategy foundered, and the Islamic State was pushed out of most of the city by a rival Islamist group, with support from residents who rejected the Islamic State’s hard-line ways.

The city of Misurata is still rebuilding after a devastating siege by the forces of former Libyan leader Moammar Gaddafi in 2011. (Javier Manzano/For The Washington Post)

Despite its proximity to Sirte, the Islamic States’ main area of control, Misurata is one of the most stable cities in Libya. (Javier Manzano/For The Washington Post)

In Sirte, the Islamic State found a more welcoming environment. Favored under Gaddafi’s oppressive 40-year rule, many residents of Sirte felt marginalized in the post-revolution era, prompting some to turn to Islamists.

While Libyan and U.S. officials have a limited understanding of the group’s links with leaders in Iraq and Syria, they think the central Islamic State has delegated control of day-to-day operations to local commanders. According to Libyan officials, a top leader in Sirte is Hassan al-Karami. a young preacher.

The Obama administration contends that Zubaidi, instrumental in the Islamic State’s expansion in central Iraq before he left for Libya, remains the group’s overall leader in Libya. Like Baghdadi, Zubaidi once mingled with other militants in a U.S. prison in Iraq.

Officials also think the parent group has provided expertise in governance, control of locals and propaganda. The video showing the killings of the Egyptian Christians, with its familiar style and fonts, was distributed through the Islamic State’s official media channels.

Libyans say that a wide range of foreign fighters, including Tunisians, Yemenis, Sudanese and Egyptians, have arrived in Sirte. In an attempt to secure local support, they said, foreigners have remained mostly out of public view.

Libyan fighters, they say, are recruited from disgruntled local tribes, including Gaddafi’s, as well as opponents of the authorities in Tripoli and Tobruk, and Islamist sympathizers from eastern Libya.

“The extremists don’t have an ideology,” said Hanan Shalouf, an academic from the coastal city of Misurata who was elected to parliament in 2014. “People join out of a desire for revenge and because they need somewhere to belong to.”

Officials in Misurata say there are as many as 1,200 fighters in nearby Sirte.

The Islamic State continues to try to expand across the country. The militants have launched attacks on foreign targets in Tripoli and on oil facilities elsewhere in the country, but U.S. officials say there is little evidence the group is plotting operations outside the country, despite some rhetoric about crossing the sea and attacking Rome.

Until such plans are detected, the Obama administration remains unlikely to launch significant military action in Libya. In addition to several failed attempts to train Libyan forces, Washington has confined its post-2011 military role to a series of Special Forces raids and airstrikes against al-Qaeda and other militants.

“Fluid, transactional, opportunistic — just remember those three words,” one senior U.S. official said of the Islamic State’s Libyan branch.

Rather than revealing a lasting affinity for the foreign-led Islamic State’s goals, Libyan fighters’ adoption of the group’s cause may be a temporary marriage of convenience, motivated by an array of tribal and regional rivalries that emerged after 2011. But, the official said, “it’s harder to defeat because it is so fluid.”

While the Islamic State has capitalized on growing discontent with the country’s lack of order, Western officials believe the group may fail to reproduce in Libya its earlier successes. Libya lacks the sectarian divisions that have been instrumental in the group’s gains in Iraq and Syria. And the country’s most important tribes have not embraced the Islamic State’s extremist cause.

The uprising in District 3

A failed uprising in Sirte in August illustrated the tenuous nature of the Islamic State’s power base in Libya — and also the fact that outside forces are yet unwilling to deal a decisive blow to the militants’ creeping expansion.

A group of underground opposition fighters launched the rebellion in Sirte on Aug. 10 after militants shot and killed Khalid al-Ferjani, a defiant local cleric. During two days of fighting, the Islamic State rained artillery on District 3, a section of the city that is home to families of tribesmen who joined the revolt.

By the time the Islamic State, with help from sympathizers in the neighborhood, had put an end to the uprising, at least 30 people were dead, officials in Misurata said. Militants were reported to have strung up corpses in public view and even beheaded some rebels in retaliation. Afterward, some families smuggled corpses out of Sirte in livestock trucks to give their dead proper burials.

Dozens of Misurata residents pray after attending a protest on Tripoli Street — a principal artery in the city and along which some of the worst battles of 2011 occurred. (Javier Manzano/For The Washington Post)

The Islamic State cleared District 3 house by house, searching for weapons or contraband. When residents were allowed to return, they found that the militants had scrawled messages on their houses: either a mark indicating they had passed inspection or a more ominous sign ordering them to “come see the Islamic State.”

U.S. officials likened the crushing of the revolt to what has occurred in eastern Syria, where the Islamic State’s brutal answer to tribal uprisings appears to have deterred further rebellions. Despite appeals for help from desperate Sirte residents who called in to Libyan television stations, no one came to the rebels’ aid.

One Sirte resistance fighter, who spoke on the condition of anonymity out of concern for his safety, acknowledged that the rebels had moved too quickly and had lacked weapons and fighting power. “The resistance movement is over,” he said by phone.

Some Misurata officials express hope that an eventual assault on the Islamic State in Sirte will include forces not just from Misurata, but also from across Libya, suggesting that fear of the militants’ spreading black banner could overtake regional and political divisions.

“We need to take on this group while the seed is still small and branches are breakable,” said a military commander in Misurata. “By the grace of God, this will be the battle that unites Libyans.”

Read more from the series:

Life in the ‘Islamic State’

An American family saved a son from joining the Islamic State. Now he might go to prison.

In a propaganda war against ISIS, the U.S. tried to play by the enemy’s rules

Why the Islamic State leaves tech companies torn between free speech and security

The hidden hand behind the Islamic State militants? Saddam Hussein’s.

‘Jihadi John’: Islamic State killer is identified as Londoner Mohammed Emwazi