To Jeff Sessions, President Trump was the man who could do no wrong. To Trump, Sessions was the attorney general who could do no right.

On questions of immigration, police work and civil rights, the president could hardly find a more eager champion of his administration’s policies.

But on the issue that seemed to matter most to the president — protecting him and his White House from the criminal investigation into 2016 election interference by Russia — Sessions recused himself shortly after becoming the attorney general.

The president never forgave him. And on Wednesday, Sessions resigned at Trump’s request.

By the time Trump declared in September, “I don’t have an attorney general,” even his torturous relationship with Sessions had become a subject of scrutiny for special counsel Robert S. Mueller III. Specifically, Mueller has examined whether Trump’s efforts to pressure Sessions into resigning in 2017 might have amounted to an attempt to obstruct justice.

Sessions has said publicly and privately that he does not regret the recusal, believing it was the right course of action. According to a person familiar with Sessions’s thinking, he has shared the president’s frustration with the pace of the Mueller probe and would like it to be finished, but also feels that it is important for the country that the investigation continue unimpeded, so that its final results are accepted by the public.

Sessions also believes that even though he did not oversee the Russia probe, he played a positive role at the Justice Department, shielding that probe from political interference, even as his boss publicly scorned him, this person said.

As much as the shadow of the Russia probe has loomed over Sessions’s tenure as attorney general, he has sought to make his time in the job about more than that — a return, as he calls it, to the principles of pro-police, anti-illegal-immigration law enforcement.

In May, standing before a sparkling Pacific Ocean and a looming border fence, Sessions emphasized his vision for America.

“Today we’re here to send a message to the world that we are not going to let the country be overwhelmed,” Sessions said. “People are not going to caravan or otherwise stampede our border . . . If you smuggle illegal aliens across our border, then we will prosecute you. If you are smuggling a child, then we will prosecute you. And that child may be separated from you as required by law.”

While the family-separation policy pursued by the administration has been shelved, at least for now, the broader set of immigration actions pursued by Sessions became a central argument for Trump and the Republicans as they sought to retain control of Congress.

Avideh Moussavian, legislative director at the National Immigration Law Center, said Sessions “came in with a very strong anti-immigrant ideology, and a very deeply ingrained world view that is rooted in exclusion.”

Through his directive that prosecutors bring cases against anyone who crosses the border illegally, his defenses of Trump’s ban of travelers from certain majority-Muslim countries and his family separation policy, his effort to end an Obama-era program granting reprieves from deportation to people who had come here as children without documentation, and his opinions attempting to restrict asylum, Sessions has “demonstrated a willingness to radically transform our immigration system in ways that run around Congress and are sort of death by a thousand cuts, if you will,” Moussavian said.

“I think that he’s trying intentionally to turn the immigration courts into fast-track deportation machines, and to turn immigration judges into mass deportation agents, and limiting their discretion,” Moussavian said.

As the nation’s highest law enforcement official for 21 months, Sessions will be remembered for remaining loyal to a president who turned virulently against him, even as he pushed Trump’s controversial policies more aggressively than any other member of the Cabinet.

“In my view, there has never been a Republican attorney general — a conservative attorney general — who can be credited with more achievements in advancing the conservative legal policy agenda,” said Charles J. Cooper, Sessions’s longtime friend and attorney and the former assistant attorney general for the Office of Legal Counsel under President Ronald Reagan. “What he’s managed to accomplish, despite the distractions, has been nothing short of astounding.”

Cooper pointed to Sessions’s undoing of Obama-era criminal justice policies, his immigration policies and his push against drug trafficking.

“In virtually every area of legal policy, he has done exactly what conservatives like me had hoped and expected that he would do,” Cooper said. “But he’s done it more energetically, more effectively than we could have imagined.”

Former Attorney General Edwin Meese, who has known Sessions for 33 years, said he has “probably accomplished as much in two years as a lot of attorneys general have in four.”

“He restored integrity to the Department of Justice, which was in very bad shape. And he has done a great job in mending relationships with law enforcement, both in the federal government and in state and local law enforcement,” said Meese.

Civil rights activists, however, said Sessions’s actions and rhetoric demonstrated racism and antipathy to civil rights. They point to steps he took to loosen federal protections for African Americans and Latinos, along with lesbian, gay, bisexual and transgender people, including restrictions on immigration and the Justice Department’s reversal of Obama administration policies on civil rights, criminal justice, policing and voting.

“Sessions has been terrible for civil rights in this country,” said Vanita Gupta, the head of the Justice Department’s civil rights division in the Obama administration who is now the chief executive of the Leadership Conference on Civil and Human Rights.

“He’s turned back the clock from trying to re-create mass incarceration policies, to his view of the voting rights act as intrusive, to his abdication of the mission of the civil rights division on a range of issues including addressing systemic police misconduct, voting rights and LGBTQ rights,” Gupta said.

After a rocky confirmation battle amid charges that he was racially insensitive, Sessions, who had been a senator from Alabama, took the helm of the Justice Department in February 2017 with the full support of Trump, whom he supported for president in early 2016 before any other senator.

“It is with great pride — very great pride — that I say these words to you right now: Attorney General Jeff Sessions, welcome to the White House,” Trump said during Sessions’s swearing-in ceremony in the Oval Office on Feb. 9, 2017. “He’s a man of integrity, a man of principle and a man of total, utter resolve.”

Those feelings sharply changed just a few weeks later.

Trump became enraged when Sessions held a news conference on March 2 announcing that, upon the advice of ethics lawyers and others at the Justice Department, he was recusing himself from the investigation into possible conspiracy between Russian officials and the Trump administration during the 2016 presidential election. The bond between the two men was shattered.

For the rest of Sessions’s tenure, Trump berated and humiliated his attorney general on Twitter and in interviews. He publicly called him “beleaguered” and “very weak.” In September, Trump told reporters he was “very disappointed in Jeff. Very disappointed.”

Through it all, Sessions continued to speak effusively about the president — even while he was mocked on “Saturday Night Live” for his unwavering loyalty — and moved aggressively to reshape the Justice Department to reflect his hard-line views.

One of the first actions Sessions took in April 2017 was to order the Justice Department to conduct a sweeping review of all reform agreements — or consent decrees — with troubled police departments nationwide, saying it was necessary to ensure that the pacts didn’t work against the Trump administration’s goals of promoting officer safety and morale while fighting violent crime.

“We’ve been very pleased with Attorney General Sessions from Day One,” said Chuck Canterbury, the national president of the Fraternal Order of Police. “He’s been much more supportive of the law enforcement mission in this country. And officers around the country know it and feel it.”

Sessions’s actions regarding police investigations, along with a strong effort to reach out to local police officers whenever he traveled to give speeches, endeared him to law enforcement. He would often say to them, “we have your backs, and you have our thanks.”

“They think extraordinarily highly of him,” said Steven H. Cook, a Justice Department official at the Office of Law Enforcement Affairs. “He has worked especially hard to reverse the false narrative that law enforcement people are racist, that it’s not an honorable profession. And law enforcement appreciate that support.

“These consent decrees, while well-intended, drive a wedge between law enforcement and the community,” Cook said. “What they signal is that the United States Department of Justice has said that this department is — fill in the blank — racist, abusive. It always casts a shadow over the entire department.”

Law enforcement officials in local communities also point to the aggressive approach Sessions has taken to fighting crime, both with his rhetoric and a string of new initiatives, particularly focused on the country’s opioid crisis. The Justice Department, for example, has tripled its prosecutions of fentanyl cases and last year brought the first cases charging Chinese nationals with selling large quantities of the drug to Americans.

Sessions proposed a change to national drug policy by limiting the amount of opioids that companies can manufacture each year. He created a team of agents and analysts to disrupt illicit opioid sales online and started a unit to target opioid-related health-care fraud.

But civil rights leaders say they see Sessions’s actions, particularly with regard to agreements reached with police in places like Chicago, as an effort by the Justice Department to walk away from its obligation to ensure that state and local law enforcement agencies nationwide are following the Constitution.

“Sessions abandoned the Justice Department’s investigation and duty by law to remedy the findings of systemic police misconduct in the Chicago Police Department,” Gupta said. The Obama administration had opened 25 investigations into law enforcement agencies and had been enforcing 14 consent decrees, along with some other agreements before Sessions became attorney general.

Sessions also reversed the charging policy of former attorney general Eric H. Holder Jr. and in May 2017 directed his federal prosecutors to aggressively target drug traffickers and charge defendants with the most serious, provable crimes. Holder, five years ago, had directed his prosecutors to stop charging low-level, nonviolent drug offenders with offenses that impose severe mandatory minimum sentences.

Civil rights groups, some Republican lawmakers and even the Koch brothers — major donors to conservative groups and causes — criticized Sessions’s new policy, saying that he was taking the country backward after there had been a consensus in Congress about criminal justice reform.

But many prosecutors praised the measure, saying it gave them more tools to do their jobs, which they felt had been taken away in the Obama administration.

“That was huge,” said Cook, a former prosecutor in Tennessee. “It unhandcuffed prosecutors who were demoralized when they were told not to charge drug traffickers — even drug traffickers who had large quantities of controlled substances — with the actual crimes they committed.”

Sessions also methodically rolled back Obama administration positions in court cases over voting rights. In the same month that Sessions became attorney general, the Justice Department dropped its long-standing position that Texas intended to discriminate when it passed a strict voter-ID law. And, several months later, the department sided with Ohio in its controversial effort to purge thousands of people from its rolls for not voting in recent elections, a position upheld in a 5-4 decision by the Supreme Court.

“There has not been a single case brought by this administration to enforce the Voting Rights Act or to take up the crisis with voter suppression that we are seeing in the country right now, and that is deeply troubling,” said Kristen Clarke, president and executive director of the National Lawyers’ Committee for Civil Rights Under Law. “What we have seen from the Justice Department is the agency reversing course on cases involving the Texas ID law and Ohio’s voter purge program.”

Sessions’s defenders, however, point to several cases that illustrate a focus on hate-crimes prosecutions under Sessions. Since January 2017, the Justice Department has indicted 50 people in connection with hate crimes and secured convictions of 51 defendants for hate crimes, according to Justice officials.

“I will acknowledge that the civil rights division has continued to pursue important work in the hate crimes area,” Clarke said. “But when it comes to voting rights and confronting systemic police misconduct and other areas of the civil rights division, we’ve seen work come to a complete standstill.”