Entra ID Linux Terminal Usage with Headless Chrome
In this article
Using Microsoft Entra ID (formerly Azure Active Directory) from a Linux terminal via headless Chrome allows you to automate authentication flows, perform web scraping on protected resources, or run automated tests against applications requiring Single Sign-On (SSO). Because Entra ID relies heavily on JavaScript and complex redirect chains for authentication, a simple CURL request is usually insufficient. By utilizing a headless browser like Chrome controlled via Puppeteer or Playwright, you can simulate a real user session, handle Multi-Factor Authentication (MFA) prompts, and capture the resulting session cookies or tokens needed for further terminal-based operations.
What You'll Need
- A Linux environment (Ubuntu, Debian, or CentOS recommended).
- Node.js and npm installed on the system.
- Google Chrome or Chromium browser installed.
- An Entra ID account with the necessary permissions to access the target resource.
- A code editor (like VS Code or Vim) to write the automation script.
- Xvfb (X Virtual Frame Buffer) if you are running on a server without a physical display.
Step-by-Step
- Install Chrome and Dependencies: Ensure Google Chrome is installed on your Linux machine. If you are on a headless server, install the necessary libraries to support Chrome's execution. Run
sudo apt-get update && sudo apt-get install -y google-chrome-stableon Ubuntu/Debian. - Initialize the Project: Create a new directory for your project and initialize a Node.js environment. Run
mkdir entra-headless && cd entra-headless && npm init -y. - Install Puppeteer: Puppeteer is the industry standard for controlling headless Chrome. Install it by running
npm install puppeteer. This will download a compatible version of Chromium by default. - Configure the Headless Script: Create a file named
auth.js. In this script, you must launch the browser with specific flags to avoid detection and ensure stability in a Linux environment. Use the--no-sandboxand--disable-setuid-sandboxflags, as these are often required for Chrome to run as root or in containerized Linux environments. - Implement the Login Flow: Write the logic to navigate to the Entra ID login URL. Use
page.goto('your-entra-login-url'). Usepage.waitForSelectorto identify the email input field, type the username usingpage.type, and click the 'Next' button. - Handle the Password and MFA: Repeat the selector and type process for the password field. If your account has MFA enabled, you will need to implement a pause in the script (using a prompt or a timer) to manually enter the code on a mirrored display, or use a library that can interface with your MFA provider's API.
- Capture Session Data: Once the authentication is successful and the browser redirects to the landing page, use
page.cookies()to extract the session cookies. These cookies can then be exported to a JSON file or passed directly to a terminal-based tool likecurlorwget. - Execute the Script: Run the script from your terminal using
node auth.js. If you are using a purely headless environment without X11, ensure you have wrapped the command inxvfb-runif the browser fails to launch.
Common Mistakes
- Ignoring Sandbox Issues: Attempting to run Chrome without the
--no-sandboxflag on many Linux distributions will result in a crash immediately upon launch. - Hardcoding Credentials: Placing usernames and passwords directly in the
auth.jsfile is a security risk. Use environment variables (viaprocess.env) to handle sensitive data. - Overlooking Dynamic Selectors: Entra ID often updates its UI. Relying on strict CSS selectors can cause scripts to break. Use more robust selectors like
[type="email"]or ARIA labels. - Forgetting User-Agent Strings: Some Entra ID conditional access policies block requests that identify as "HeadlessChrome". Always set a realistic User-Agent string using
page.setUserAgent().
Tips
- Use Persistent Contexts: Instead of logging in every time, use
userDataDirin Puppeteer. This saves the session and cookies to a local folder, allowing you to skip the login process in subsequent runs until the session expires. - Slow Down Actions: Use the
slowMooption in the Puppeteer launch configuration. This adds a small delay between actions, making the automation look more human and reducing the chance of being flagged by security bots. - Debug with Headful Mode: While developing the script, set
headless: false. This allows you to see the browser window and identify exactly where the script is failing during the Entra ID redirect chain.
Troubleshooting
If the browser fails to launch, check the system logs for missing shared libraries. Chrome requires several .so files that may not be present in minimal Linux installations. Installing libnss3, libatk1.0-0, and libgbm1 usually resolves launch crashes.
If you encounter a "This browser is not secure" error from Entra ID, it means the conditional access policies have detected the headless environment. To fix this, ensure you have disabled the automationControlled flag using page.evaluateOnNewDocument to hide the navigator.webdriver property.
If the script hangs during the redirect, increase the timeout setting in page.goto and page.waitForSelector. Entra ID authentication involves multiple redirects across different domains, which can be slow on some network configurations.
Frequently Asked Questions
Can I use this for automated API calls?
Yes, the primary goal is usually to capture the session cookie or Bearer token, which you can then include in the headers of your API requests via the terminal.
Does this work with Multi-Factor Authentication (MFA)?
Yes, but MFA requires a manual step or a programmatic way to retrieve the code. You can use a "headful" window for the MFA step and then switch to headless mode for the remaining tasks.
Is Puppeteer the only option for Linux?
No, Playwright is a strong alternative that often handles browser contexts and multiple tabs more efficiently than Puppeteer.
Will this trigger security alerts in Entra ID?
It can. If the login originates from an unusual IP address or a known headless user agent, Entra ID may flag the sign-in as risky and require additional verification.
How do I keep the session alive?
By saving the cookies to a local file and loading them back into the browser instance at the start of your next session, avoiding the need for a full login flow.