The terrorist attack by Hamas in Israel and the retaliatory strikes in Gaza had already put French authorities on edge, pushing them to jack up security at Jewish sites and ban pro-Palestinian protests.

Then, last Friday, just three days before the country was set to mark the somber anniversary of a teacher’s gruesome beheading by an Islamist extremist, an eerily similar attack hit at home as a man used a knife to kill a teacher and injure three other people at a school in northern France in what officials called an Islamist terror attack.

Since then, the mood in France has gone from worried to alarmed. The authorities raised the terrorist threat alert to its highest level, pouring even more police officers and soldiers onto the streets. Bomb scares emptied out major sites over the weekend, including the Louvre and Versailles Palace.

Officers in flak jackets with machine guns, their fingers resting on the triggers, stood sentinel on Saturday outside the school where a former student went on a stabbing spree the day before, killing Dominique Bernard, 57, a French literature teacher.

Mourners arrived bearing bouquets of white roses. Many were racked with grief, but also were wondering anxiously whether the escalating crisis in the Middle East had stoked the embers of Islamic terrorism and blown them to a small northern French city.

“We are on the other side of the world, but we are facing the consequences,” said David Milhamont, with his son Valentin, 11, who was scurried out of the attacker’s path on Friday by a hall monitor and sheltered in a classroom. “Just how far it will go, that is the question.”

The suspected attacker, Mohammed Mogushkov, 20, is in custody.

The sense of anxiety has been compounded by the ominous timing of the attack — almost three years to the day after the brutal murder of Samuel Paty, a history teacher who was beheaded by an Islamist extremist for showing caricatures of the Prophet Muhammad in his class to illustrate free speech, a killing that deeply traumatized the country.

Prescheduled ceremonies in schools across the country to honor Mr. Paty on Monday were suddenly painfully relevant.

“Islamist terrorism has struck at what it rightly regards as its greatest adversary: our schools,” President Emmanuel Macron said in a message to teachers.

“Terrorists know that there can be no Republic without schools, without patiently learning in your classrooms about critical thinking and the values of liberty, equality, fraternity and secularism that forge citizens,” he said.

The attack also came the morning after Mr. Macron reiterated the country’s unwavering support for Israel in the wake of the terrorist attacks by Hamas. French authorities have raised the possibility that there was a link between Friday’s attack and the conflict but have offered little concrete evidence.

Home to some of Europe’s largest Muslim and Jewish communities, France has been on alert since the conflict started when Hamas invaded Israel on Oct. 7. There have been nearly 200 antisemitic acts, mostly verbal threats and vandalism, and over 100 people have been arrested for such acts or for glorifying terrorism, according to the interior minister, Gérald Darmanin.

The fears of further tensions arise in a country already deeply scarred by Islamist terrorism, with two large-scale attacks occurring in 2015 and 2016, followed by a string of smaller, deadly shootings and stabbings in subsequent years, often carried out by lone assailants.

Many in France are warily accustomed to the threat. But few expected an attack in a place like Arras, a small city 50 minutes by train from Paris, with a history of quietly welcoming refugees.

“We have had no problems of racism that you see in some parts of France, and there’s no far-right presence,” said the city’s mayor, Frédéric Leturque. “Sadly, this happened in Arras. But it could have happened in any other city in France.”

The horror unfurled on Friday morning at Gambetta-Carnot, a large public school in the city’s center.

The assailant’s bloody rampage led him into the school’s inner courtyard, where many young children were waiting for the cafeteria to open. Witnesses heard him shout “God is great” in Arabic during the attack.

Many of those arriving to place their bouquets on an overflowing table near the school entrance on Saturday were students who had witnessed the attacks, and their shaken parents.

“I am scared to go back inside,” said Franck Dissaux, 11. He couldn’t fathom that Mr. Bernard, his literature teacher last year, was gone.

“Everyone loved him,” he said, his eyes brimming with tears.

Colleagues described Mr. Bernard as a devoted teacher and a passionate reader of French literature with a vast library, who often left books in their cubby holes with inscriptions. He was married with three daughters.

“I keep asking myself, ‘Why him?’” said Philippe Lourdel, a math teacher at the school.

Schools throughout Arras were put into lockdown Friday. Inside his barricaded classroom at another school, Marius Lajara, 15, said he watched the attack unfurl on social media.

“There was the feeling of war in the city,” he said after arriving by bike with his rattled parents and little sister. “I’m still in shock.”

The attack rekindled a fierce debate on immigration because Mr. Mogushkov was on the country’s security radar for radicalism and was not a French citizen.

Like Mr. Paty’s killer, Mr. Mogushkov was born in the Caucasus region of Russia and came to France at a young age with his family, which filed for asylum. But some in his family espoused a dangerous form of Islam, the authorities said.

In 2018, his father was deported because of “radical ideology,” Mr. Darmanin said. And his older brother, Mosvar, is serving time in prison after two separate convictions on terrorism charges. Mosvar had been flagged in 2016 by his school for threatening teachers and wearing the qamis — a long robe used by some Muslim men that was recently banned in schools along with a similar garment for women.

Like his older brother, Mr. Mogushkov had also been flagged by school officials, and had been under surveillance since July. The police even arrested him the day before the attack but found no evidence of a crime or of a nascent plot and soon released him.

Politicians on the right and far right have blasted the government for not deporting Mr. Mogushkov, even though, barring exceptions, French law prevents the authorities from deporting people who arrived in France under the age of 13.

“We don’t make them leave,” said Henri Leroy, who is part of a Senate committee to study the response to pressure, threats and attacks on teachers. “They stay on French soil and they are walking bombs.”

Mr. Leroy cited recent polls that show roughly half of France’s teachers feel uneasy discussing freedom of expression or the bedrock French concept of laïcité, or secularism, in class. He said government changes made since Mr. Paty’s murder — like making it easier for teachers who are threatened to get protection, or improving cooperation between police and school authorities — are not enough.

The government has vowed to speed up the deportation of nearly 200 radicalized foreigners who are in France illegally, and it wants to toughen some immigration laws.

“Schools are the Republic’s fertile ground,” Prime Minister Élisabeth Borne said on Saturday at a ceremony for an award created in Mr. Paty’s name. “You enter as a student, you leave as a citizen.”

But unlike Mr. Paty’s killer, who stalked him on the street, Mr. Mogushkov went directly into the school — adding a new layer of fear, said Sébastien Ledoux, an associate professor of history at the Picardie Jules Verne University in Amiens who has studied the effect of terrorist attacks on student life.

“That increases the sense of vulnerability,” he said, “which is what terrorists want.”