Log In By Customer ID

Magento1

// for security we should add custom IP
$IP = "192.168.0.1";
$customer_id = 1;
if($_SERVER['REMOTE_ADDR']==$IP){
    Mage::getSingleton ( 'customer/session' )->loginById ( $customer_id );
}

Magento2

$customer_id = 1;
$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->get('Magento\Customer\Model\Customer')->load($customer_id);
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);