How to Spoof System Date for Isolated Shell Sessions in Linux Testing Environments


2 views

When developing applications that depend on system time, we often need to test behavior across different dates without affecting the entire system. Traditional methods like date --set impact system-wide operations including cron jobs, log rotations, and time-sensitive services.

Linux namespaces provide the perfect solution through temporal isolation. The unshare command combined with faketime creates an effective sandbox:


# First install libfaketime
sudo apt-get install libfaketime

# Create time-shifted session
unshare --uts bash -c 'export FAKETIME="2020-01-15 12:00:00" && \
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 && \
/bin/bash --noprofile --norc'

For complex testing scenarios, Docker provides better isolation:


docker run --rm -it alpine ash -c "apk add faketime && \
export FAKETIME='+365d' && \
LD_PRELOAD=/usr/lib/libfaketime.so.1 ash"

For automated testing, integrate time manipulation directly into your test scripts:


#!/bin/bash
test_dates=("2023-01-01" "2023-06-30" "2024-12-25")

for testdate in "${test_dates[@]}"; do
    echo "Testing with date: $testdate"
    FAKETIME="$testdate" LD_PRELOAD=./libfaketime.so.1 \
    ./your_application --test-argument
done

Always validate your time manipulation is working:


# In your modified environment
echo "Current apparent date: $(date)"
python3 -c "import time; print(time.strftime('%Y-%m-%d'))"
node -e "console.log(new Date())"

When testing time-sensitive applications, changing the system-wide date with date command affects all processes and services. This can cause:

  • Cron jobs executing prematurely or delayed
  • Log timestamp inconsistencies
  • SSL certificate validation failures
  • Database replication issues

The most robust approach is using libfaketime, a library that intercepts system calls to time functions. Here's how to install and use it:

# Debian/Ubuntu
sudo apt-get install faketime

# CentOS/RHEL
sudo yum install faketime

Basic Usage Examples

Run a single command with faked time:

faketime '2022-01-01 12:00:00' date

For an entire shell session:

faketime -f '+3d' bash  # Fast-forward 3 days
faketime -f '-1y' bash  # Rewind 1 year

Create a .faketimerc file for persistent settings:

# Format: [YYYY-MM-DD] [HH:MM:SS] [offset]
2023-05-15 09:30:00 +15m  # Start at May 15 2023, then advance 15min per real minute

For containerized testing, use this docker run command:

docker run -e FAKETIME="+2d" -it ubuntu date

For developers needing custom behavior, you can implement your own time override:

// timestub.c
#include 
#include 

time_t time(time_t *tloc) {
    return 1672531200; // Fixed timestamp for Jan 1, 2023
}

// Compile with:
// gcc -shared -fPIC timestub.c -o timestub.so -ldl

// Usage:
// LD_PRELOAD=./timestub.so ./your_program
  • Some applications use direct hardware clock reads
  • Multi-threaded programs may see inconsistent timestamps
  • Always verify with strace -e clock_gettime,gettimeofday