Judge Leonard Shapiro made the decision after Onyango Obama, 69, testified that he had lived in the U.S. for 50 years, been a hard worker, paid income tax and been arrested only once.
Asked about his family in the U.S., he said he has a sister and two nieces, then added, "I do have a nephew." Asked to name the nephew, he said, "Barack Obama," then added, "He's the president of the United States."
Onyango Obama, the half brother of the president's late father, testified he has lived in the U.S. since 1963, when he entered on a student visa. He had a series of immigration hearings in the 1980s and was ordered to leave the country in 1992 but remained.
During his testimony, he identified himself as Obama Okech Onyango. Court records and authorities have identified him as Onyango Obama, and no explanation was given for the discrepancy.
Obama told the judge he had led a quiet, simple life, graduating from high school in Cambridge, then attending Boston University, where he received a degree in philosophy. He said he has worked for years as a manager at a family-owned liquor store in Framingham, just west of Boston. He also said he has worked for decades to help African immigrants find housing and settle in the U.S.
The judge, while announcing his decision, cited a law that entitles immigrants who are "out of status" to become permanent residents if they arrived in the U.S. before 1972, maintained continuous residence and are of good moral character.
Obama testified he hasn't been back to Kenya since he entered the U.S. and said it would be difficult for him to return after all these years.
"Mr. Judge, America is a land of opportunities, a land of chances," he said in a thick accent.
His immigration status didn't become public until his 2011 drunken-driving arrest in Framingham. Police said after the arrest he told them, "I think I will call the White House."
Asked about the exchange by a prosecutor on Tuesday, he said he might have said that but couldn't recall.
The charge was dismissed after he completed a year of probation and 14 weeks of alcohol education classes.
The judge said he considered testimony about Obama's character, including letters from people who praised him for being a "kind and decent person," and considered the drunken-driving charge and allegations of discrepancies in what he told immigration officials 20 to 30 years ago.
"He appears to me to be a gentleman," the judge said.
Obama testified that President Obama stayed with him for three weeks in Cambridge while the president was a student at Harvard Law School.
"In our tradition, your brother's kids are your kids as well," he said after the hearing.
Onyango Obama's Cleveland-based immigration attorney, Margaret Wong, called him a "wonderful older gentleman."
"He has earned his privilege to stay in the United States. He has been here for 50 years," she said.
After the hearing, Obama quickly left the courthouse without speaking. Wong said he didn't receive any special treatment and was happy with the judge's decision.
If the government appeals, a notice must be filed within 30 days. Wong said Obama could get U.S. citizenship after five years.
There was no immediate comment Tuesday from the White House, which has said it expected the case to be handled like any other.
In the president's memoir, "Dreams from My Father," he writes about his 1988 trip to Kenya and refers to an Uncle Omar, who matches Onyango Obama's background and has the same date of birth.
Onyango Obama is the second Obama family member to be found living illegally in the United States. His sister, Zeituni Onyango, the president's aunt, was granted asylum in 2010 after her first asylum request in 2002 was rejected and she was ordered deported in 2004.
Onyango didn't leave the country and continued to live in public housing in Boston. Her status was revealed just days before Barack Obama was elected in November 2008. At the time, then-candidate Obama said he didn't know his aunt was living in the U.S. illegally and he believed laws covering the situation should be followed.