Magento 2 Disable Input Field of system.xml file

If you want to restrict the Magento admin users to update any specific input field then you can disable that field.

Side note, if you add any system configuration values from app/etc/env.php or app/etc/config.php then also that field can not be updatable from the Magento admin. Today we are not discussing how to set value from env.php or config.php but we will discuss how you can disable it using the system.xml

Steps to disable system configuration field programmatically in Magento 2
Step 1: Open system.xml file and add/update field as following.
We are using “frontend_model” to disable the input field.

<field id="gateway_url" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Gateway URL</label>
      <frontend_model>Dhairvi\ExtensionDemo\Block\Adminhmlt\System\Config\Disable</frontend_model>
</field>

Step 2: Create a file as per the location you have added in frontend_model in system.xml field.
Here we are creating file Disable.php under \app\code\Dhairvi\ExtensionDemo\Block\Adminhtml\System\Config\

<?php
namespace Dhairvi\ExtensionDemo\Block\Adminhtml\System\Config

use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Config\Block\System\Config\Form\Field;

class Disable extends Field
{
    protected function _getElementHtml(AbstractElement $element)
    {
        $element->setDisabled('disabled');
        return $element->getElementHtml();
    }
}

Clear Magento caches and that’s it.