Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 11-30-2012, 11:31 AM   #1
LQ Newbie
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
SSH X forwarding in program using "system" command

I've run into a nuance of ssh that I'd like to solve. Here's the setup:

I'm running a GUI program that launches commands via the "system" function. One such command is 'ssh -X account@remote "./startup"'. Inside the 'startup' script, I set the display variable to localhost:10.0. The desired result is that the X window of the remote program shows up locally. It works when in a terminal:

1. ssh -X account@remote <enter>

Setting DISPLAY explictly is redundant here because the .bashrc is run.

2. ssh -X account@remote "./startup"

Setting DISPLAY explicitly in script needed because it doesn't get set.

In the program, the call 'system("ssh -X account@remote \"./startup\"")' yields a 'Can't open display: localhost:10.0'. Here's the kicker though, it works if ANOTHER terminal is already sshed into that machine. This probably has something to do with the .Xauthority file.

My question is how can I make this work WITHOUT having to ssh into the machine from another terminal? Any other approaches to this are welcome.

Test script on remote machine (replace gcalctool with any X windows program)
#! /bin/bash

export DISPLAY=localhost:10.0

Program to execute command:
#include <stdlib.h>
int main(int argc,char *argv[])
    system("ssh -X account@remote \"./startup\"");
    return 0;
BTW, using mostly CentOS 5, and one machine is Fedora Core 2 (I know, but it's got special hardware)

Last edited by JayKemper; 11-30-2012 at 11:33 AM. Reason: Added distribution info
Old 11-30-2012, 01:04 PM   #2
LQ Newbie
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I found the error...

I use an alias for ssh=ssh -X. When running the 'system' call, it doesn't use the alias. I had to explicitly use the -X for the X forwarding to work.

There is no way anyone could have guessed that from the information I provided. Lesson learned - 'system' does NOT run your .bashrc script!
Old 12-01-2012, 02:46 PM   #3
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
Instead of putting the -X into an alias, I suggest to define it in your ~/.ssh/config file which will always be used:
Host foobar
    hostname remote
    user account

Host *
    ForwardAgent yes
    ForwardX11 yes
    ForwardX11Trusted yes
    Compression yes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh / sshd config: how enable "tunneling with port forwarding on the network"? frenchn00b Linux - Server 1 08-30-2009 01:57 PM
Supplying "password" to "ssh" command paragkalra Linux - Networking 1 07-31-2008 05:19 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
list file system linux supports "please give the command or system call" varun_shrivastava Linux - General 4 01-09-2007 07:28 AM
C -communicating with system("ftp"); using "program|ftp>>myfifo" probably hansschmucker Programming 1 03-23-2005 01:39 PM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration