How to Simulate System Dates and Times for Commands in Linux Using faketime

Testing time-dependent software logic—such as SSL certificate expiration, licence key validation, automated backup rotations, and scheduled batch jobs—poses a significant challenge on development and production systems. Changing the host system clock using timedatectl or date -s requires superuser privileges, disrupts running daemons, invalidates Kerberos tickets, and can corrupt system logs and databases.

The faketime utility (powered by the underlying libfaketime library) solves this problem by intercepting system calls related to time retrieval (such as time(), gettimeofday(), and clock_gettime()) via the dynamic linker (LD_PRELOAD). This allows you to launch individual commands, interactive subshells, or background processes under an arbitrary system date and time without modifying the operating system clock or affecting other running applications.

Installing faketime on Linux

The faketime package is readily available in standard package repositories across major Linux distributions:

On Debian, Ubuntu, and Linux Mint:

sudo apt update
sudo apt install -y faketime

On Fedora, Rocky Linux, and RHEL:

sudo dnf install -y faketime

On Arch Linux:

sudo pacman -S libfaketime

Basic Syntax and Clock Freezing

The simplest way to use faketime is to specify a static date string followed by the target command. When launched with a static date format, faketime sets the initial time for the process:

faketime '2028-12-25 08:00:00' date

Output:

Mon 25 Dec 08:00:00 UTC 2028

Meanwhile, inspecting the system clock directly confirms the host remains unaffected:

date

By default, unless configured otherwise, the clock ticks normally forward from the specified starting timestamp. If you require the time to remain strictly frozen at the exact second across multiple checks, prepend an @ symbol to the timestamp string:

faketime '@2030-01-01 00:00:00' /bin/bash -c "date; sleep 3; date"

Both date executions will output the exact same frozen second.

Applying Relative Time Offsets

In automated testing pipelines and Continuous Integration (CI) workflows, calculating absolute calendar dates can be brittle. faketime natively supports relative offset strings relative to the current host time:

  • Advance into the future: Prepend + followed by the interval:
    # Advance time by 10 days
    faketime '+10d' date
    
    # Advance time by 2 years and 3 months
    faketime '+2y 3m' date
    
    # Advance time by 48 hours
    faketime '+48h' date
  • Rewind into the past: Prepend - followed by the interval:
    # Rewind time by 90 days to test archive retention policies
    faketime '-90d' date
    
    # Rewind time by 1 year
    faketime '-1y' date

Controlling Clock Speed and Rate Scaling

Some automated tests require simulating prolonged durations (such as testing a 24-hour cache invalidation or token refresh cycle) without waiting 24 physical hours. faketime allows you to scale the perceived speed of time using the -f flag or the rate multiplier prefix:

# Speed up clock progression by 60x (1 real second = 1 fake minute)
faketime -f '+0d x60' /bin/bash -c "for i in {1..5}; do date; sleep 1; done"

Conversely, time can be slowed down to inspect high-frequency timestamp collisions or sub-second race conditions by passing fractional values such as x0.1.

Spawning an Interactive Simulated Shell

If you are debugging complex environments or running multiple utility scripts, passing each command through faketime can become tedious. Instead, spawn an entire interactive bash or zsh subshell wrapped in the simulated environment:

faketime '2035-06-15 14:30:00' bash

Every process, script, and terminal command executed within this nested shell inherits the fake time environment. To return to standard real-time operation, type:

exit

Testing TLS/SSL Certificate Expiration

A practical use case for sysadmins is verifying how client applications handle expired TLS certificates. To simulate validating a certificate after its expiration date:

# Launch curl simulated 2 years in the future against a secure endpoint
faketime '+2y' curl -I https://example.com

Curl will immediately abort the TLS handshake, raising an explicit SSL certificate problem: certificate has expired warning, validating your error-handling and failover logic before production deployments.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.