An incoming Harvard University freshman was booted back to his home in Lebanon when he tried to enter the US — and blamed his deportation on his pals’ anti-American social media posts.

Ismail B. Ajjawi, 17, arrived at Boston Logan Airport on Friday night, and spent eight hours there before immigration officials sent him packing, he said in a statement issued to the Harvard Crimson, the university’s student paper.

During his time there, the teen said, immigration officers questioned him about his religious practices in his home country, before asking that he unlock his phone and laptop for a search.

Once the search was completed, Ajjawi said, an officer grilled him about his friends’ online posts.

“When I asked every time to have my phone back so I could tell them about the situation, the officer refused and told me to sit back in [my] position and not move at all,” he wrote.

“After the 5 hours ended, she called me into a room, and she started screaming at me. She said that she found people posting political points of view that oppose the US on my friend[s] list.”

Ajjawi, who is Palestinian and lives in Lebanon, insisted he’d never made any political posts and shouldn’t be penalized for his friends’ actions.

“I responded that I have no business with such posts and that I didn’t like, [s]hare or comment on them and told her that I shouldn’t be held responsible for what others post,” he wrote. “I have no single post on my timeline discussing politics.”

But it was no use — the officer canceled the student’s visa and told him he’d be deported.

A State Department official declined to comment on the particulars of Ajjawi’s case — but confirmed to the Crimson that Customs and Border Protection found him “inadmissible” to the country “based on information discovered during the CBP inspection.”

“Applicants must demonstrate they are admissible into the US by overcoming ALL grounds of inadmissibility including health-related grounds, criminality, security reasons, public charge, labor certification, illegal entrants and immigration violations, documentation requirements, and miscellaneous grounds,” spokesman Michael S. McCarthy said in a statement.

A CBP spokeswoman told the Washington Post that all applicants for admission to the US are subject to complete inspections upon arrival — and that no one can enter until the examining officer is fully satisfied. The applicant bears the burden of proof for admission into the country, according to the spokesperson.

The Crimson called the incident “rare among Harvard undergraduates.”

“The University is working closely with the student’s family and appropriate authorities to resolve this matter so that he can join his classmates in the coming days,” Harvard spokesman Jonathan L. Swain told the Washington Post.

Two Harvard students and two scholars were barred from entry in January 2017 when the Trump administration enacted a travel ban on seven majority-Muslim nations, Swain said.

In Ajjawi’s case, he went home to Lebanon — but said he’s been in touch with a lawyer and hopes to be on campus by Sept. 3, the first day of classes.