New Saudi King Salman is taking the throne amid an unprecedented period of instability and terrorist threats. (Reuters)

At 3 a.m. on a cold desert night earlier this month, four Islamic State militants carrying guns, grenades and cash slipped into Saudi Arabia here through a hole in the new heavy fencing that separates this country from Iraq.

They were immediately spotted by Saudi border guards in a state-of-the-art control room 35 miles away, appearing first as blips on radar, then as ghostly white figures on night-vision cameras scanning the desolate desert landscape.

Heavily armed troops were dispatched to confront them. When the battle ended, the four intruders — all Saudi citizens — and three Saudi soldiers were dead, including the local base commander, who was killed when a militant pretending to surrender detonated a suicide vest.

“Thanks to God and our new systems, we are ready for whatever they try,” said the new commander, Ali Mohammed Assiri, whose troops have now been issued orders to shoot on sight anyone breaching the border. “If you are not willing to defend the country, you don’t deserve to live in it.”

Except for Syria and Iraq, where the Islamic State controls territory, no country is more directly threatened by Islamist militants than Saudi Arabia, which the extremists regard as a traitor to Islam for Riyadh’s close associations with the United States and the West.

President Obama will visit Saudi Arabia Tuesday and meet its new King Salman, ending his planned three-day trip to India early. (Reuters)

No king of Saudi Arabia has ascended the throne amid more regional turmoil than King Salman, who was crowned Friday upon the death of his brother King Abdullah.

With war raging in Syria and tensions with Iran increasing, Saudi Arabia is threatened by a disintegration of the national government in Yemen across its southern border and by the Islamic State militants who are dominating the Iraqi desert just over its northern border.

Salman indirectly mentioned the threat of rising violence and regional instability on Friday in his first speech to the Saudi people, saying that “the Arab and Islamic nation is in dire need today to be united and maintain solidarity.”

Militants have staged four attacks inside the kingdom in the past six months, resulting in the deaths of eight civilians, 11 police or border guards and 13 militants, according to Saudi officials.

As in the recent attacks in Paris on the Charlie Hebdo satirical newspaper and a kosher supermarket, most of the Saudi attacks have been carried out by homegrown radicals influenced or trained by the Islamic State, al-Qaeda or other extremist groups. Saudi authorities said that they have arrested 293 people in connection with the incidents and that 260 of them are Saudi nationals.

Saudi officials are anticipating more attacks, either by some of the 2,200 or so Saudi citizens they say have gone to fight with the Islamic State in Syria or Iraq, or by others who infiltrate Saudi Arabia’s borders, especially the nearly 600-mile frontier with Iraq, which runs mainly through empty desert.

“They are targeting Saudi Arabia, and they want to have a very big terrorist act in this country,” said Gen. Mansour al-Turki, spokesman for the Interior Ministry.

The Saudi government has responded by sharply beefing up border security and by creating new laws that give the government broad power to arrest anyone who joins, or even praises, the radical groups — which has led to complaints from human rights groups that the laws are being unfairly used against activists who merely criticize the government.

Officials have also made it illegal for imams in the country’s 85,000 mosques to give sermons sympathizing with religious extremists. The Ministry of Islamic Affairs has launched the al-Sakinah Campaign for Dialogue, an anti-radicalization program that includes a Web site offering anonymous counseling.

“We are also educating the imams to tell people that what ISIS is saying is against Islam,” said Tawfeeq al-Sediry, Saudi Arabia’s deputy minister of Islamic affairs. “They represent violence. We represent the real Islam.”

Saudi officials said they have also tightened controls on charities suspected of channeling money to radicals. Wealthy Saudi individuals are widely believed to be a significant source of funding for the Islamic State and al-Qaeda.

Critics point to that funding as evidence that many Saudis quietly support the Islamic State, seeing it as a Sunni Muslim force fighting to protect other Sunnis, especially in Syria and Iraq, where Shiite Muslims control the government with the support of Saudi Arabia’s chief rival, Iran.

“In the Middle East, it’s nothing new: You create your own terrorists, then pretend you are fighting them,” said Ali al-Ahmed, a Saudi activist who runs the Institute for Gulf Affairs in Washington. “The Saudis didn’t even invent it, but they’re good at it.”

Ahmed said the Saudi government is “playing both sides” to give “the appearance that they are the good guys.”

“They get a lot of political traction out of it,” Ahmed said. “To the Americans, they are the guardians of safety, and no matter how horrible they are on human rights, the way they treat women and all that, they are the ones who are keeping things under control. Really, they are very clever.”

Awadh al-Badi, a researcher and scholar at the King Faisal Center for Research and Islamic Studies in Riyadh, rejects those assertions. He says the Islamic State is offering arguments that attract some idealistic young Muslims.

Islamic State leaders say they want to establish a vast Islamic caliphate or “khalifa,” including taking control of the holy cities of Mecca and Medina, which are in Saudi Arabia.

“For many young people, the idea of khalifa is the idea of the great Islam,” Badi said. “The idea itself is attractive to many people who aspire to see a return of Islamic dignity and influence.”

One Western diplomat based in Riyadh said Saudi cooperation in the fight against Islamist militants has been unwavering. He noted that Saudi Arabia has joined the U.S.-led military coalition against the Islamic State and sent fighter jets to bomb militant targets — including one F-16 piloted by King Salman’s son Prince Khaled.

“We have had to fight the perception that the Saudis are not doing enough,” the diplomat said, speaking on the condition of anonymity because of the sensitivity of the subject. “From here, we think they are doing plenty. They tell us, ‘We’ve been fighting these guys and their ilk for 12 years, so don’t tell us how to fight these people.’”

The kingdom faced a wave of al-Qaeda attacks in the mid-2000s, but officials were able to stop them with a fierce crackdown that resulted in the jailing of thousands of suspected militants.

The decree issued last spring making it illegal to belong to or publicly support the Islamic State and other radical groups has slowed the flow of Saudis joining the militants, said Turki, the Interior Ministry spokesman, because it allows police to arrest anyone trying to go as well as those returning.

The most visible sign of Saudi Arabia’s response to the rising militant threat is the extensive new system of fences, ditches, razor wire and berms along the border with Iraq, which stretches from the border with Kuwait in the east to Jordan in the west.

King Abdullah inaugurated the barrier system in September after six years of construction on the project, which was initially conceived as a defense against the sectarian chaos in Iraq but is now primarily a defense against the Islamic State.

Saudi officials have also embarked on a multiyear project to similarly fortify all the thousands of miles of land borders, with Jordan, Qatar, the United Arab Emirates, Oman and especially Yemen — where the government was recently toppled by rebels aligned with Iran, Saudi Arabia’s main rival for power in the region.

Sometimes jokingly referred to as the “Great Wall,” the Iraq border defense system involves two high fences topped with concertina wire, backed by deep ditches and tall sand berms designed to make it impossible for any vehicle to cross.

The physical barriers are reinforced by technological ones, with 40 radar towers, each 125 feet high, that constantly sweep a radius of nearly 25 miles looking for any movement. Each tower is fitted with two cameras — one for daytime, one for night — that can zoom in on objects up to 12 miles away.

At the border’s main control room, at the border guard base in Arar, a town of about 100,000 people in the empty desert nearly 600 miles northwest of Riyadh, operators sit at computer monitors watching 24 hours a day.

When radar spots something moving in a suspicious place, the operators use a mouse to swivel the camera in its direction. Most of the time it’s nothing dangerous — a camel or a shepherd’s dog — but in the pre-dawn hours of Jan. 5, it was four heavily armed militants.

Not far from a remote and desolate border crossing, which is only open during the hajj pilgrimage season, the four militants slipped through a hole left by a construction crew working on the fence.

Turki said the men were trying to get to Arar, where police arrested three Saudis and four Syrians suspected of plotting with the infiltrators. The four militants were carrying nearly $20,000 in cash, four suicide vests, six grenades, five assault rifles and pistols and two silencers.

“Even if they got to Arar, were they waiting for the people to rise with them? I doubt it,” said Badi, the King Faisal Center scholar. “They send messages: We are capable of coming to you. We will target you. Terrorism will come to you.

“It’s the same idea as the attacks in France,” he said. “They are not planning to take over France, but they send a message that they can do this.”