CHARLESTON, S.C. — Joseph C. Meek Jr., a friend of Dylann S. Roof’s who spent time with him in the weeks before nine people were killed at the Emanuel African Methodist Episcopal Church here, was sentenced Tuesday to 27 months in prison for hampering and misleading the federal authorities in the aftermath of Mr. Roof’s racist massacre.

The punishment, handed down by Judge Richard M. Gergel of Federal District Court, was at the low end of the sentencing guidelines, which called for Mr. Meek to spend from 27 to 33 months in prison. The months that he spent in a county jail after his arrest will count toward his federal sentence.

Before the sentence was announced, a tearful Mr. Meek said he was not sure whether he would survive prison, and he apologized to family members of Mr. Roof’s victims, some of whom had gathered for the hearing. “I’m really sorry a lot of innocent lives were taken,” said Mr. Meek, who had previously expressed remorse in handwritten letters in which he asked for forgiveness.

But Judge Gergel, speaking at a hearing that lasted more than two hours, said Mr. Meek’s crimes warranted prison. “The danger he exposed to the community is extraordinary,” he said.