Yet while Mr. Murphy is comfortably ahead, Mr. Northam is locked in a competitive race. A former chairman of the Republican National Committee and a Washington establishment fixture, Mr. Gillespie has turned to more hard-line tactics in an effort to win over both swing voters and Mr. Trump’s supporters.
While Virginia is increasingly Democratic — Mr. Obama carried it twice — voter turnout plummets when it holds its governor’s election the year after a presidential vote. And that often results in an electorate that is older and whiter than what is reflected in White House races.
Mr. Obama pointedly invoked this trend.
“Sometimes y’all get a little sleepy, get a little complacent,” he said of Democrats in off-year elections, warning that they would have no right to complain about their elected officials if they “slept through” Election Day.
Speaking directly to young Virginians, the 56-year-old former president, wearing a suit but no tie, tried his hand at millennial vernacular: “I think it’s great that you hashtag and meme, but I need you to vote,” Mr. Obama said.
While avoiding any direct discussion of his successor, Mr. Obama offered his harshest assessment of the Trump era since leaving the White House.
“Instead of our politics reflecting our values, we’ve got politics infecting our communities,” he said.
As they spoke at the rally, Mr. Northam and the Democratic governor he hopes to succeed, Terry McAuliffe, were, however, glad to link Mr. Trump and Mr. Gillespie.
“He’s nothing more than a Washington lobbyist who’s now become Donald Trump’s chief lobbyist,” said Mr. Northam, adding that “we cannot accept that this is the new normal.”
Mr. McAuliffe, who is thought to be considering a White House bid, was even more succinct about Mr. Gillespie’s attempt to straddle between Trump loyalists and Trump skeptics. “Ed Gillespie is treating the president of his party like a communicable disease,” the governor said.
If Mr. Obama demurred from a frontal assault that could provoke the Twitter-happy president, he was eager to depict Mr. Gillespie as a Trump stand-in.
Mr. Gillespie until recently had been linking Mr. Northam to MS-13 by pointing out that the Democrat opposed a measure in the State Senate that would have banned sanctuary cities in Virginia. (No localities in the state have tried to become sanctuary cities.)
Noting that Mr. Northam, an Army veteran, is a child neurologist, Mr. Obama said that it “strains credulity” to think that the lieutenant governor is “suddenly cozying up to street gangs.”
But Mr. Obama went well beyond mockery, accusing Mr. Gillespie of “fanning anti-immigrant sentiment” and all but calling him a hypocrite for airing the ad after he spent years warning other Republicans to stay away from such a divisive issue.
“He’s gone on record in the past condemning the very same kind of rhetoric that he’s using right now,” Mr. Obama said, careful to avoid mentioning Mr. Gillespie by name.
The Republican’s campaign, perhaps wanting to avoid a quarrel with a former president still popular here, offered a muted response to Mr. Obama’s broadsides.
“It’s no surprise President Obama would level Lieutenant Governor Northam’s attacks against Ed at a Northam campaign event,” said David Abrams, Mr. Gillespie’s spokesman.
Mr. Obama was far more restrained discussing Mr. Gillespie’s criticism of Mr. Northam for supporting the removal of Virginia’s Confederate statues.
Speaking in a former capital of the Confederacy, Richmond, a city whose most stately boulevard is lined with monuments to the leaders of the Lost Cause, the former president did not directly mention the statues.
“We shouldn’t use the most painful parts of our history just to score political points,” Mr. Obama said.
He did, though, draw laughs by noting that he is a distant relative of the Confederate president, Jefferson Davis, who is chiseled in marble a few miles away.
Democrats hoped Mr. Obama’s appearance would rouse African-Americans in this city, where about half the population is black, and across central Virginia.
Mr. Obama, Democratic Party officials said, may be one of the few figures famous enough to draw attention away from Mr. Trump, whose near-daily pronouncements and provocations have overshadowed this race.
Yet while Mr. Obama’s return to the campaign trail was sure to draw ample news coverage — 18 cameras lined a riser in the back of the hall — it was less clear whether he could transfer his loyal following to Mr. Northam.
Democrats suffered grievous down-ballot losses during Mr. Obama’s presidency in off-year elections, including the governorships of Virginia and New Jersey in 2009, when he similarly sought to rally African-American voters. Those losses presaged the Tea Party-fueled Republican sweep in 2010, a year that saw Republicans seize statehouses across the country to take control of the decennial redistricting process.
In New Jersey, Mr. Obama sought to frame the race between Mr. Murphy and Ms. Guadagno as an opportunity to right the course in a country that he saw as faltering in its role as an example to the world.
“The world counts on America having its act together,” he said.
“You can’t take this election, or any election, for granted,” he added, and then paused before continuing, “I don’t know if you all noticed that.”
The audience reacted with laughter followed by muted sighs.
When it was his turn to speak, Mr. Murphy was more blunt in criticizing Mr. Trump’s record. He alluded to Mr. Trump’s decision to end a program started by Mr. Obama that protected young undocumented immigrants from deportation and said, “Dreamers, and we’ve got 22,000 in this state, are every bit as American as our four kids and are being shown the door.”
Mr. Murphy said that in the Trump era, “governors will never have mattered more,” and he promised that he would stand up to the president.