WASHINGTON — Accusing the Obama administration of “an illegitimate exercise of executive power,” a federal judge on Thursday rejected the government’s effort to impose new restrictions on lawyers’ access to prisoners at Guantánamo Bay, Cuba, if they were no longer actively challenging the prisoners’ detention in federal court.

In a scathing, 32-page opinion, Royce C. Lamberth, the chief judge of the United States District Court for the District of Columbia, ruled in the detainees’ favor, characterizing the government’s arguments using terms like “untenable,” “quite preposterous,” “even less persuasive,” and “does not pass the smell test.”

The judge wrote: “The court, whose duty it is to secure an individual’s liberty from unauthorized and illegal executive confinement, cannot now tell a prisoner that he must beg leave of the executive’s grace before the court will involve itself. This very notion offends the separation-of-powers principles and our constitutional scheme.”

The administration did not immediately say whether it would appeal the ruling by Judge Lamberth, a 1987 appointee of President Ronald Reagan. Lt. Col. Todd Breasseale, a Pentagon spokesman, said the government was “aware of Judge Lamberth’s opinion” and was reviewing it. Under the new policy, which the military unveiled this spring, lawyers were told they could no longer meet with clients who did not have a pending case, following procedures that had been in place since 2004.