The conservative majority on the Supreme Court seemed to agree Wednesday that President Trump has the authority to ban immigrants from certain majority-Muslim countries if he thinks that it is necessary to protect the country.

Lower courts have struck down each of the three iterations of the president’s travel ban proclamation, the first of which was issued just a week after he took office in January 2017. But the conservative-leaning Supreme Court may be Trump’s best hope, and it gave the administration a boost by allowing the ban to go into effect in December while considering the challenges to it.

Solicitor General Noel J. Francisco told the justices that the president was well within his power to issue the proclamation and that it came after a thorough, worldwide review of the vetting procedures of countries.

The initiative applies only to a “tiny” number of countries, Francisco told the justices, and permits the vast majority of travelers to enter, including those from Muslim-majority countries.

Chief Justice John G. Roberts Jr. was most active in advancing the notion that the president is privy to national security information that courts are ill-prepared to second-guess.

After being caught in Trump’s travel ban, a Syrian family finds a place to call home

As the nation marks President Trump’s 100 days, see how these Syrian refugees once caught in the travel ban are finding their new home.

But Justice Anthony M. Kennedy, who always seems to occupy the pivotal position when conservative and liberal justices disagree, asked questions that mostly seemed to support the president’s authority.

The court is considering the third iteration of Trump’s travel ban, issued last fall, which barred various travelers from eight countries, six of them with Muslim majorities. They are Syria, Libya, Iran, Yemen, Chad, Somalia, North Korea and Venezuela. But restrictions on North Korea and Venezuela are not part of the challenge. Chad was removed from the list earlier this month.

The first questions for the government came from the two liberal justices, Ruth Bader Ginsburg and Sonia Sotomayor, who had noted their dissent from the high court’s earlier order to allow the ban to go into effect while challenges continued.

Sotomayor suggested that Congress had already taken steps to ensure national security by implementing a heightened vetting process for travelers from other countries.

“Where does the president get the authority to do more than Congress has already decided is adequate?” she asked.

Francisco pointed to the removal of Chad from the list as evidence that the review process was working as anticipated by encouraging more cooperation from countries to help screen out those who might intend to harm the United States.

The challengers are led by the state of Hawaii, which said its citizens and educational institutions have suffered because of the ban.

Former Obama administration acting solicitor general Neal K. Katyal, representing Hawaii, said that Trump had taken an “iron wrecking ball” to the law Congress had implemented to govern immigration and keep the nation safe.

“No president in 100 years” has tried to issue such a broad-based immigration ban, Katyal said, adding that it was based on Trump’s animosity toward Muslims.

But conservative justices Samuel A. Alito Jr. and Neil M. Gorsuch, along with Roberts, peppered Katyal with questions on how the president had exceeded his lawful authority, given that Congress had granted the executive branch broad latitude to bar people’s entry to the U.S.

Katyal argued that while the president had great power in immigration decisions, he could not effectively rewrite the law. The ban, he said, also violated the Constitution.

But some of the justices seemed skeptical that the ban did so. Alito noted that the law said if Trump found the entry of “any aliens” to be detrimental to U.S. interests, he could bar them. How, he asked, did the travel ban not fall “squarely” within that power?

Alito also said the ban only affected 8 percent of the world’s Muslim population. “A reasonable observer would not think this was a Muslim ban,” Alito said.

Roberts posited a hypothetical: If the intelligence agencies told the president that 20 Syrian nationals planned to enter the U.S. with biological weapons, could the president ban the entry of Syrians to stop them? Katyal conceded that he could, because in that instance — unlike this one — there was a true emergency.

“We’re so far from the hypothetical that we’ll concede the hypothetical,” Katyal said.

Justice Elena Kagan similarly asked Francisco about a hypothetical anti-Semitic candidate for president who once elected put in place a proclamation blocking entry for citizens of Israel. Could the courts intervene in such a situation, she asked.

“This is an out-of-the-box kind of president in my hypothetical,” Kagan added, prompting laughter from the courtroom.

Francisco called it a “tough hypothetical,” but said such a president could impose the measure if it came at the recommendation of staff who had identified a genuine national security problem. In contrast, Francisco said Trump’s travel ban is an “easy case” because it came after multiagency review and on the advice of Cabinet officials. He conceded that if Cabinet officials knew the president was ordering a ban based on religious animus, because he told them as much, they would be “duty bound” to resign or refuse to comply with his order to come up with a justification.

“Is everything the president said exactly that?” Kagan asked. Trump had, on the campaign trail and afterward, suggested that he favored a Muslim ban, though the Justice Department argues that his travel ban is not that.

The justices are reviewing a unanimous ruling from a three-judge panel of the U.S. Court of Appeals for the 9th Circuit in San Francisco. That panel said the third version of the travel ban suffered from the deficiencies of the first two — that Trump had again exceeded his lawful authority and that he had not made a legally sufficient finding that entry of those blocked would be “detrimental to the interests of the United States.”

The U.S. Court of Appeals for the 4th Circuit struck down the ban on the constitutional question. The 9-to-4 decision took a deep dive into Trump’s statements and tweets since he became president, and concluded that the proclamation, like the first two, was motivated not by national security concerns but by antipathy toward Muslims.

However, Judge William B. Traxler Jr. switched sides, saying the administration’s work between the second travel ban and the third cured its problems for him.

The third version must be judged on the basis of the “context of the investigation and analysis that the agencies acting on the president’s behalf have completed, the consultation that has taken place between the president and his advisers, and the logical conclusions and rationale for the proclamation that are documented therein,” Traxler wrote.

The case is Trump v. Hawaii.