Error: Can't open display: (null)
Here is how to fix this error by editing the ssh client and OpenSSH (SSHD server) configuration file.
Error: Can’t open display: (null) when using Xclip over the ssh-based session under Linux or Unix
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux or Unix terminal |
Category | Terminal/ssh |
OS compatibility | AIX • AlmaLinux • Alpine • Amazon Linux • Arch • BSD • CentOS • Debian • Fedora • FreeBSD • HP-UX • Linux • macOS • Mint • NetBSD • OpenBSD • openSUSE • Pop!_OS • RHEL • Rocky • Stream • SUSE • Ubuntu • Unix • WSL |
Est. reading time | 3 minutes |
The xclip is a command line interface to X selections. It works with X GUI sessions. You can copy and paste using your Linux or Unix terminal and remote/local ssh server easily. However, you need to configure a few things. Otherwise, you will see an error that read as follows:
- On your Linux or Unix desktop client, edit the ~/.ssh/config file and add ForwardX11 yes directive. For example:
{vivek@desktop:~}$ vim ~/.ssh/config
Set it as follows for all or selected host:Host * ForwardX11 yes
Save and close the file.
- Next log into your ssh server:
{vivek@desktop:~}$ ssh user@server-ip-here
{vivek@desktop:~}$ ssh vivek@ln.cbz-www-004 - Edit the /etc/ssh/sshd_config file using a text editor such as Vim or nano. For instance:
{vivek@server1:~}$ sudo vim /etc/ssh/sshd_config
Append or set X11Forwarding to yes as follows:X11Forwarding yes
Save and close the file. Restart or reload the sshd service. For example, here is how to restart ssh on Linux with systemd:
{vivek@server1:~}$ sudo systemctl restart ssh.service
- Log in using the ssh command and enable X11 forwarding when log into the remote server. On macOS, you may need to pass the -Y option if -X is not working out for you. For example:
{vivek@desktop:~}$ ssh -X vivek@ln.cbz-www-004
OR
{vivek@MacBookPro:~}$ ssh -Y vivek@ln.cbz-www-004
- Verify that $DISPLAY is set using the echo command or printf command:
{vivek@server1:~}$ printf "%s\n" "$DISPLAY"
Outputs:
localhost:10.0
- Test it using the xclip command:
{vivek@server1:~}$ command | xclip #copy
{vivek@server1:~}$ xclip -o #paste
{vivek@server1:~}$ date | xclip
{vivek@server1:~}$ xclip -o
Summing up
You must set proper X11 forwarding between your SSH client and server to fix the “Error: Can’t open display: (null)” error under Linux, macOS, and Unix-like systems. For more information, see the following manual pages using the man command:
$ man 5 ssh_config
$ man 5 sshd_config
$ man 1 ssh
See my following tutorial pages for more info about using the xclip command under Linux for CLI copy and pasting:
- How To Copy Command Output To Linux Clipboard Directly
- How do I copy a file to the clipboard in Linux?
- Use oathtool Linux command line for 2 step verification (2FA)
🥺 Was this helpful? Please add a comment to show your appreciation or feedback.