Data security in a distributed ledger is ensured through various means, including decentralized storage, encryption, consensus mechanisms, and immutability. Even if some nodes are attacked, the integrity and security of the data can still be maintained by the other nodes.