See all GNU/Linux related FAQ
When you try to use the xclip command over the ssh-based session to copy and paste data, you will get the following message on screen:
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:

Error: Can’t open display: (null) when using Xclip command over the ssh-based session

  1. 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.

  2. Next log into your ssh server:
    {vivek@desktop:~}$ ssh user@server-ip-here
    {vivek@desktop:~}$ ssh vivek@ln.cbz-www-004
  3. 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

  4. 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
  5. Verify that $DISPLAY is set using the echo command or printf command:
    {vivek@server1:~}$ printf "%s\n" "$DISPLAY"
    Outputs:
    localhost:10.0
  6. Test it using the xclip command:
    {vivek@server1:~}$ command | xclip #copy
    {vivek@server1:~}$ xclip -o #paste
    {vivek@server1:~}$ date | xclip
    {vivek@server1:~}$ xclip -o

    Click to enlarge

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:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Vivek Gite is an expert IT Consultant with over 25 years of experience, specializing in Linux and open source solutions. He writes about Linux, macOS, Unix, IT, programming, infosec, and open source. Follow his work via RSS feed or email newsletter.

Category List of Unix and Linux commands
AnsibleCheck version Fedora FreeBSD Linux Ubuntu 18.04 Ubuntu macOS
Archivingz commands
Backup ManagementDebian/Ubuntu FreeBSD RHEL
Database ServerBackup MySQL server MariaDB Galera cluster MariaDB TLS/SSL MariaDB replication MySQL Server MySQL remote access
Download managerswget
Driver ManagementLinux Nvidia driver lsmod
Documentationhelp mandb man pinfo
Disk Managementdf duf ncdu pydf
File Managementcat cp less mkdir more tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04 Ubuntu 24.04
KVM VirtualizationCentOS/RHEL 7 CentOS/RHEL 8 Debian 9/10/11 Ubuntu 20.04
Linux Desktop appsChrome Chromium GIMP Skype Spotify VLC 3
LXDBackups CentOS/RHEL Debian 11 Fedora Mount dir Ubuntu 20.04 Ubuntu 22.04
Modern utilitiesbat exa
Network ManagementMonitoring tools Network services RHEL static IP Restart network interface nmcli
Network UtilitiesNetHogs dig host ip nmap ping
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 11 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Power Managementupower
Package Managerapk apt-get apt yum
Processes Managementbg chroot cron disown fg glances gtop iotop jobs killall kill pidof pstree pwdx time vtop
Searchingag egrep grep whereis which
Shell builtinscompgen echo printf
System Managementreboot shutdown
Terminal/sshsshpass tty
Text processingcut rev
Text Editor6 Text editors Save and exit vim
User Environmentexit who
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami w
User Management/etc/group /etc/passwd /etc/shadow chsh
Web ServerApache Let's Encrypt certificate Lighttpd Nginx Security Nginx
WireGuard VPNAlpine Amazon Linux CentOS 8 Debian 10 Firewall Ubuntu 20.04 qrencode
0 comments… add one

Leave a Reply

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.
close