NS codoped carbon nanorods (NS-CNRs) were prepared using crab shell as template and polyphenylene sulfide (PPS) as both the C and S precursor, followed by carbonization in NH3. The as-obtained NS-CNRs had a diameter of ∼50 nm, length of several micrometers, and N and S contents of 12.5 at.% and 3.7 at.%, respectively, which can serve as anodes for both lithium-ion batteries (LIBs) and sodium ion batteries (SIBs). When serving as an anode of LIB, the NS-CNRs delivered gravimetric capacities of 2154 mAh g−1 at current densities of 0.1 A g−1 and 625 mAh g−1 at current densities of 5.0 A g−1 for 1000 cycles. When serving as an anode of SIB, the NS-CNRs delivered gravimetric capacities of 303 mAh g−1 at current densities of 0.1 A g−1 and 230 mAh g−1 at current densities of 1.0 A g−1 for 3000 cycles. The excellent electrochemical performance of NS-CNRs could be ascribed to the one-dimensional nanometer structure and high level of heteroatom doping. We expect that the obtained NS-CNRs would benefit for the future development of the doped carbon materials for lithium ion batteries and other extended applications such as supercapacitor, catalyst and hydrogen storage.