WASHINGTON — President Obama vetoed legislation on Friday that would allow families of victims of the Sept. 11 terrorist attacks to sue the government of Saudi Arabia for any role in the plot, setting up an extraordinary confrontation with a Congress that unanimously backed the bill and has vowed to uphold it.

Mr. Obama’s long-anticipated veto of the measure, known as the Justice Against Sponsors of Terrorism Act, is the 12th of his presidency. But unless those who oppose the bill can persuade lawmakers to drop their support by next week, it will lead to the first congressional override of a veto during Mr. Obama’s presidency — a familiar experience for presidents in the waning months of their terms.

In his veto message to Congress, Mr. Obama said the legislation “undermines core U.S. interests,” upending the normal means by which the government singles out foreign nations as state sponsors of terrorism and opening American officials and military personnel to legal jeopardy. It would put United States assets at risk of seizure by private litigants overseas and “create complications” in diplomatic relations with other countries, he added.

“I have deep sympathy for the families of the victims of the terrorist attacks of Sept. 11, 2001, who have suffered grievously,” Mr. Obama wrote. But enacting the measure “would neither protect Americans from terrorist attacks nor improve the effectiveness of our response to such attacks.”