The White House gave troops stationed at the southern border the OK to use lethal force if necessary — a move that legal experts warn may violate the Posse Comitatus Act, which bars the military from being used for civilian law enforcement.

The “cabinet order” was signed by White House chief of staff John Kelly — not President Trump — and authorizes “Department of Defense military personnel” to “perform those military protective activities that the Secretary of Defense determines are reasonably necessary” to protect border agents, the Military Times reported.

That includes “a show or use of force (including lethal force, where necessary), crowd control, temporary detention and cursory search.”

There are about 5,900 active-duty troops and 2,100 National Guard forces deployed to the Mexican border.

Trump had said earlier this month that the troops could “fight back” if the Central American asylum-seekers heading to the US border hurled rocks their way.

“They want to throw rocks at our military, our military fights back. I told them to consider it a rifle. When they throw rocks like what they did to the Mexican military and police, I say consider it a rifle,” the commander-in-chief said on Nov. 1.

He later walked back the comment that rocks are the same as rifles after widespread condemnation.

Some of the actions described in Kelly’s late Tuesday order, including crowd control and detention, could violate the 1878 Posse Comitatus Act.

The Congressional Research Service determined “case law indicates that ‘execution of the law’ in violation of the Posse Comitatus Act occurs (a) when the Armed Forces perform tasks assigned to an organ of civil government, or (b) when the Armed Forces perform tasks assigned to them solely for purposes of civilian government,” the website reported.

But the law also allows the president “to use military force to suppress insurrection or to enforce federal authority,” the service found.

Prior to the midterms, the president employed harsh anti-immigrant rhetoric to describe the “caravans” of migrants — many of them women and children — fleeing their homelands.

In an effort to fire up his nationalist base, he repeatedly called the caravan an “invasion” of the US, claiming without evidence that “Middle Easterners,” terrorists and hordes of violent gang members had infiltrated the group.

He ordered the troop deployment, and ominously warned that the active-duty troops would stop the asylum-seekers, though their actual role turned out to be building camps and stringing barbed wire along the border.

Trump barely mentioned the caravans after the midterms, and the Pentagon announced that the border operation, briefly dubbed “Operation Patriot Freedom” before the brass scrapped that moniker, would be wound down and that the troops would be home by Christmas.

Some of the migrants have massed at the border city of Tijuana, while others remain hundreds of miles away from the US.

Experts eyed the order warily.

Posse Comitatus is “always looming in the background. You never invoke it as such because it is such a background principle,” William Banks, author of “Soldiers on the Home Front: The Domestic Role of the American Military” and former director of the Institute for National Security and Counterterrorism at Syracuse University, told the website.

Kelly said in the directive that the move was necessary because “credible evidence and intelligence” suggest that the migrants in Tijuana “may prompt incidents of violence and disorder” that could threaten Border Patrol personnel.

But the White House could still find itself in legal hot water if the authorities in the memo are determined to be counter to the law, Banks said.