For the first time, we report a facile approach to fabricate metal organic framework derived carbon (MOF-C) with tunable porous structure. Different from direct pyrolysis of MOFs and blind attempt in application, the in-situ ammonia treatment enables MOF-C with desired porous structure from enriched microporous structure to hierarchically mesoporous structure. Further, NH3 treated MOF-C as carbon host for sulfur loading performing as the cathode for Li–S batteries results in twice higher capacity retention than that of pristine MOF-C. Besides, different Li–S electrochemical mechanisms regarding the different porous structures of carbon are also revealed and investigated in this paper.