WASHINGTON — A pair of luxurious waterfront compounds outside New York and Washington have for decades been a retreat for Russian diplomats, places to frolic in the water, play tennis and take lengthy steam baths.

On Thursday, Obama administration officials described the compounds differently: as beachside spy nests sometimes used by Russian intelligence operatives to have long conversations on the sand to avoid being ensnared by American electronic surveillance.

They ordered all Russians out of the compounds within 24 hours.

The move was one of a number of retaliatory measures the White House announced in response to what it called a Russian campaign to wreak havoc on the presidential election, and to what it said was systemic harassment of American officials in Russia.

Besides the shuttering of the two compounds, administration officials announced the expulsion by Sunday of 35 unnamed Russian officials — and their families — who they said were working undercover as spies.

The announcement had echoes of the tit-for-tat reprisals that were common during the Cold War, and the government of Vladimir V. Putin announced within hours that there would be a swift response. That seemingly ensured that the Obama administration’s final days would be consumed by escalating accusations and retaliation between Washington and Moscow.

The Obama administration offered no proof on Thursday linking the two compounds or the Russians being expelled to intelligence activities, and veterans of the spy games offered mixed assessments of the administration’s actions.

“I think these sanctions are pretty weak. It’s more perhaps symbolic,” said Steven Hall, a former senior C.I.A. official who ran Russia operations until his retirement in 2015.

But Mr. Hall said that the other measures could cause more irritation in Moscow. He said that he could not recall a previous move similar to the shuttering of the two compounds — one on Long Island and the other on the Eastern Shore of Maryland — and that expelling the 35 officers and their families “will slow down Russia’s activities in the United States.”

One former senior American official said that the group, made up of officials working at the embassy in Washington and the consulate in San Francisco, makes up about a third of the suspected Russian intelligence officers operating in the United States under diplomatic cover. It is unclear how many of the expelled Russians spent time at the two compounds.

The dismissals are believed to be the largest expulsion of Russian officials from the United States since 2001, when about 50 suspected Russian intelligence officers were forced to leave after Robert Hanssen, a senior F.B.I. official, was arrested and charged with spying for Moscow.

“I would be flabbergasted if the Russians didn’t reciprocate and expel 35 American officials from Russia,” Mr. Hall said. The C.I.A. has long posted undercover officers in Russia — and nearly every other country in the world — who pose as diplomats, businessmen or other professionals.

It is unclear whether any of the Russians being expelled played a part in the hacking that interfered with the American election. Law enforcement officials said that the White House and the State Department had come up with the number 35 and had then turned to the F.B.I. to list Russian officials in the United States who it believed were actually intelligence officers.

While the expulsions send a powerful message, they will force the F.B.I. to go back to work trying to identify a new crop of spies the Russians will almost certainly send to the United States.

After the expulsions were announced, officials described a pattern of harassment of American government employees working in Russia. In June, a uniformed officer with Russia’s Federal Security Service, called the F.S.B., attacked an employee of the United States Embassy in Moscow seemingly without provocation.

“Our diplomats have experienced an unacceptable level of harassment in Moscow by Russian security services and police over the last year,” President Obama said in a statement on Thursday.

Before this year, the most notable recent episode to become public was in 2013, when Russian officials arrested Ryan C. Fogle, an employee of the embassy in Moscow who the Russian government had discovered was a C.I.A. officer.

Mr. Fogle, officially posted in Russia as the third secretary of the embassy’s political department, was picked up on the street wearing a blond wig under a baseball cap. He was carrying a knapsack holding a compass, a Moscow street atlas and $130,000 in cash, money that Russian officials said was meant to be used to recruit a Russian security officer as a spy.

The episode’s amusing details garnered much of the attention, but Mr. Fogle’s arrest and expulsion from Russia was a public indicator that the spying activity between the United States and Russia was heating up — and in many ways had never ended. The F.S.B. took the unusual step of releasing a video showing Mr. Fogle facedown on a street as a Russian officer pinned his hands behind his back.

American law enforcement and intelligence agencies have little indication that the compounds in Maryland and New York played any role in the cyberattacks against the Democratic National Committee and other political organizations.

The Long Island retreat is a 14-acre estate in Upper Brookville, known as Norwich House. Upper Brookville’s mayor, Elliot Conway, confirmed on Friday that the federal government had ordered the house shut.

Federal agents could been seen on Friday morning in the estate’s driveway, shortly before four vehicles with diplomatic license plates drove away. A few of the people in the cars waved to reporters, in an apparent goodbye.

The Russian government has owned the property, and another one nearby in Glen Cove, since the days of the Soviet Union, and relations between the owners and local residents have at times been strained. In 1982, Reagan administration officials said Russians were using the Glen Cove mansion to conduct electronic surveillance of Long Island’s defense and technology industries.

The Glen Cove City Council responded by barring the Russians from obtaining free beach parking stickers and discounted tennis permits.

The Council’s lone dissenting voice described taking away the tennis permits as “petty” and said that “it’s a matter the professionals at the State Department should handle.”

On Thursday, White House and F.B.I. officials said it was the Glen Cove estate, Killenworth Mansion, that was being shuttered. On Friday, they confirmed that it was in fact the Upper Brookville house.

The Maryland compound is a sprawling complex of several buildings fronting the Corsica River in Centreville, including a three-story brick Georgian-style mansion that has long been a retreat for Russia’s ambassador to Washington. It has a swimming pool, a soccer field and lighted tennis courts.

Anatoly Dobrynin, who served as ambassador to Washington from the Kennedy administration to the Reagan administration, was frequently at the estate, and the Russian government held on to the property after the collapse of the Soviet Union.

Unlike on Long Island, relations between the Russians visiting the Maryland compound and local residents have generally been placid — even if the Russians did not always adopt the local folkways.

Julie Patterson, 44, who has lived on a horse farm near the compound for 12 years, said the Russians largely kept to themselves but were cordial neighbors, inviting locals to an annual Labor Day party.

She said that she believed that the property was largely used as a weekend retreat and that she often saw children, families and buses heading to the estate. She said people often held parties there and hosted a sailing club.

In 1992, a Centreville resident told a reporter for The Associated Press that the Russians did not cook crabs the local way, by throwing live crabs into a pot of boiling water.

“They stab them with a screwdriver, break the back shell off, clean them and then boil the body,” she said.

Correction:

Articles on Friday about the Obama administration’s decision to close two Russian-owned compounds in the United States misidentified one of the compounds, using information from the White House and F.B.I. officials. The administration ordered the closure of Norwich House in Upper Brookville, N.Y., owned by Russia — not the nearby Killenworth Mansion in Glen Cove, N.Y., also owned by the Russians. An accompanying picture that showed Killenworth Mansion should have been of Norwich House.

A version of this article appears in print on , on Page A1 of the New York edition with the headline: On East Coast, 2 Compounds Caught Up in History’s Echoes. Order Reprints | Today’s Paper | Subscribe