In the Ubuntu Linux terminal, the keyboard shortcut Ctrl + D sends an EOF (End of File) signal. By default, if you press this shortcut while at an empty command prompt, the Bash shell assumes you are finished and instantly terminates the session, closing your terminal window or disconnecting your SSH connection.
Because Ctrl + D is very close to other common shortcuts (like Ctrl + C to cancel a command), it is incredibly easy to press it accidentally. Closing a remote SSH session by mistake can disrupt your workflow significantly. Fortunately, you can configure Bash to ignore accidental EOF signals.
How to Set the IGNOREEOF Variable
You can prevent the terminal from closing immediately by using a built-in shell variable called IGNOREEOF. This variable tells the shell how many consecutive EOF signals it must receive before it actually closes the session.
To test it temporarily for your current session:
- Open your Ubuntu terminal.
- Type the following command and press Enter:
export IGNOREEOF=1
Now, try pressing Ctrl + D once. The terminal will not close. Instead, it will print a warning message: Use "logout" to leave the shell. If you press Ctrl + D a second time immediately, it will close.
How to Make the Change Permanent
To ensure this protection is active every time you open a terminal or log into your server, you need to add the variable to your user’s Bash configuration file (~/.bashrc).
- Open the configuration file in a text editor like Nano by typing:
nano ~/.bashrc - Use the arrow keys to scroll to the very bottom of the file.
- Add a new line and type the following:
export IGNOREEOF=2(Setting the value to 2 means you must press Ctrl + D three times in a row to close the terminal, making accidental closures virtually impossible.) - Save the file by pressing Ctrl + O, then press Enter.
- Exit Nano by pressing Ctrl + X.
- Apply the changes immediately by typing:
source ~/.bashrc
From now on, your Ubuntu terminal is protected against accidental closures from errant keyboard shortcuts. When you genuinely want to close the terminal, you can either press Ctrl + D three times or simply type the exit command and press Enter.