In a speech at the Islamic Society of Baltimore on Feb. 3, President Obama said he wanted to highlight the American right to freedom of religion for all, including Muslims. (Reuters)

President Obama made his first visit to a U.S. mosque as president on Wednesday and sought to repair the increasingly frayed relationship between American Muslims and their fellow citizens.

Obama’s remarks came at a time of growing fear and division in the country, a climate that in recent months has unnerved many American Muslims and surprised senior White House officials.

The president often sounded like a concerned parent, worried for the country he leads as it prepares to replace him in a presidential election marked by inflammatory and anti-Islamic rhetoric.

“Here at this mosque, twice last year, threats were made against your children,” Obama said at the Islamic Society of Baltimore. “Around the country, women wearing the hijab . . . have been targeted. We’ve seen children bullied. We’ve seen mosques vandalized.”

The president’s appearance at the simple house of worship, less than 50 miles from the White House, was also extraordinary for its contrast to a stirring address delivered in the first months of his presidency. Then, a younger Obama, speaking in Cairo, appealed to more than 1 billion Muslims worldwide for a “new beginning” with the United States.

President Obama delivers remarks Wednesday at the Islamic Society of Baltimore. (Jonathan Ernst/Reuters)

In that speech, Obama spoke of terrorism, co­lo­ni­al­ism, the U.S. invasion of Iraq and the long, damaging Israeli-Palestinian conflict. He suggested that his unique background as the first black president and son of a Muslim father could help bridge the divide between Islam and the West.

Nearly seven years later in Baltimore, Obama’s aims were far more modest. In a spare and simple speech, Obama spoke of the fears of Muslim parents and children who worry they will be targeted because of their faith.

He recounted a letter from a 13-year-old girl who wrote to him that she was scared and a mother who said her “heart cries every night thinking about how our daughter might be treated at school.” He described the worries of children who feared that they might be rounded up and forced out of the country.

Obama’s speech took on the tone of a history or civics lesson at times as he sought to allay the fears of those who do not know any Muslims or who say that Muslims are less patriotic than other Americans. A recent study by the Pew Research Center found that roughly half of the public says that at least some Muslims are anti-American. About 11 percent said that “most” or “almost all” U.S. Muslims are anti-American, according to Pew.

Many of these Americans reacted with fear and anger to the recent terrorist attacks in Paris and San Bernardino, Calif., and have flocked to support Republican presidential candidates seeking to bar Syrian refugees from entering the United States. The most prominent of those candidates, Donald Trump, has suggested a temporary ban on Muslims entering the United States.

Without mentioning Trump, Obama often seemed to be speaking directly to the working-class white voters who have flocked to his message.

The president described Muslim children who took part in the Cub Scouts and their parents who served as police officers, firefighters and soldiers. In one notable moment, he sought to describe the inside of the mosque where he was standing. “Think of your own church, or synagogue, or temple, and a mosque like this will be very familiar,” he said. “This is where families come to worship and express their love for God and each other.”

President Obama greets attendees in an overflow room after speaking at the Islamic Society of Baltimore. (Mandel Ngan/AFP/Getty Images)

Obama has labored for much of his presidency to move the United States off a war footing abroad and keep the threat of terrorism in perspective at home. The domestic reaction to the Paris and San Bernardino attacks caught much of the White House, including the president, off guard and has prompted some serious West Wing soul-searching.

White House Chief of Staff Denis McDonough, speaking recently to an audience at The Washington Post, lamented the ability of groups such as the Islamic State and al-Qaeda to transmit “unbelievably hateful propaganda onto the phone of any individual on the face of the earth,” making a “distant threat very personal and very close.”

The challenge for the White House has been to counter those hateful messages that drive recruits to extremist groups but also stir fear on the home front. After the attacks on a French satirical newspaper in January 2015 — up until then the deadliest terrorist assault in modern memory in Paris — the Obama administration rushed to organize an oft-delayed conference at the White House on countering violent extremism.

Muslim leaders from around the United States and the world flew into Washington for days of panel discussions and planning that produced few major new initiatives.

Since then, White House officials have admitted that they have struggled to blunt the appeal of Islamic State propaganda to a small but dangerous population of disaffected Muslims worldwide.

“This is the area where, in my view, I’ve failed the president most dramatically,” said McDonough, who has been one of Obama’s most trusted foreign policy advisers. “And we are bound and determined to leave the next president a much more effective infrastructure to confront that stuff.”

Republicans have responded with promises to step up U.S. airstrikes on terrorist groups in Iraq and Syria and raise barriers at home to refugees seeking shelter from the chaos. They have blasted Obama for refusing to describe the terrorist threat as emanating primarily from “Islamic extremists.”

Obama on Wednesday defended his position, insisting that those plotting attacks on the United States and its allies were betraying their faith. “Groups like ISIL are desperate for legitimacy,” he said, using an acronym for the Islamic State. “They try to portray themselves as religious leaders and holy warriors who speak for Islam. I refuse to give them legitimacy.”

Instead, he called on Muslim leaders to reject extremism and pressed Americans to resist presidential campaign rhetoric that cast Islam as being at the root of America’s terrorism problem.

“That betrays our values. It alienates Muslim Americans. It’s hurtful to those kids who are trying to go to school and are members of the Boy Scouts, and are thinking about joining our military,” he said. “That kind of mind-set helps our enemies. It helps our enemies recruit. It makes us all less safe. So let’s be clear about that.”

The president’s speech, though, wasn’t really about the terrorist threat abroad, but rather the dangers to American values on the home front. He was speaking to American Muslims and a growing number of Americans who seemed unnerved by the Islamic faith.

Throughout Wednesday’s speech, there were many distinct echoes of Obama’s 2009 Cairo address. He quoted some of the same Koranic passages: “Whoever kills an innocent, it is as if he has killed all mankind.” In both speeches, he noted that two of America’s Founding Fathers — Thomas Jefferson and John Adams — each owned copies of the Koran.

The 2009 speech in Cairo was one that Obama had hoped would begin to change the world. “Change cannot happen overnight,” Obama said then. “No single speech can eradicate years of mistrust. . . . But I am convinced that in order to move forward, we must say openly the things we hold in our hearts, and that too often are said only behind closed doors.”

In Baltimore on Wednesday, Obama’s focus was on helping American Muslims feel safer and more at home in their own country. “If you’re ever wondering whether you fit in here, let me say it as clearly as I can, as president of the United States: You fit in here — right here,” Obama told the audience in the mosque. “You’re right where you belong. You’re part of America, too.”