Posted: . At: 8:55 AM. This was 2 years ago. Post ID: 16768
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Use a pseudo-terminal over SSH to run a command on a remote machine.


It is possible with SSH to run a command on a remote machine over SSH. The example below shows the output from the ps utility, which runs on a remote machine over SSH. After the command is run the connection is closed. This is a handy SSH tip.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ ssh -t jason@192.168.1.2 ps -eM
jason@192.168.1.2's password:
LABEL PID TTY TIME CMD
unconfined 1 ? 00:00:01 systemd
unconfined 2 ? 00:00:00 kthreadd
unconfined 3 ? 00:00:00 rcu_gp
unconfined 4 ? 00:00:00 rcu_par_gp
unconfined 6 ? 00:00:00 kworker/0:0H-kblockd
unconfined 9 ? 00:00:00 mm_percpu_wq
unconfined 10 ? 00:00:00 ksoftirqd/0
unconfined 11 ? 00:00:00 rcu_sched
unconfined 12 ? 00:00:00 migration/0
unconfined 13 ? 00:00:00 idle_inject/0
unconfined 14 ? 00:00:00 cpuhp/0
unconfined 15 ? 00:00:00 cpuhp/1
unconfined 16 ? 00:00:00 idle_inject/1
unconfined 17 ? 00:00:00 migration/1
unconfined 18 ? 00:00:00 ksoftirqd/1
unconfined 20 ? 00:00:00 kworker/1:0H-kblockd
unconfined 21 ? 00:00:00 cpuhp/2
unconfined 22 ? 00:00:00 idle_inject/2
unconfined 23 ? 00:00:00 migration/2
unconfined 24 ? 00:00:00 ksoftirqd/2
unconfined 26 ? 00:00:00 kworker/2:0H-kblockd
unconfined 27 ? 00:00:00 cpuhp/3
unconfined 28 ? 00:00:00 idle_inject/3
unconfined 29 ? 00:00:00 migration/3
unconfined 30 ? 00:00:00 ksoftirqd/3
unconfined 32 ? 00:00:00 kworker/3:0H-kblockd
unconfined 33 ? 00:00:00 kdevtmpfs
unconfined 34 ? 00:00:00 netns
unconfined 35 ? 00:00:00 rcu_tasks_kthre
unconfined 36 ? 00:00:00 kauditd
unconfined 37 ? 00:00:00 khungtaskd
unconfined 38 ? 00:00:00 oom_reaper
unconfined 39 ? 00:00:00 writeback
unconfined 40 ? 00:00:00 kcompactd0
unconfined 41 ? 00:00:00 ksmd
unconfined 42 ? 00:00:00 khugepaged
unconfined 89 ? 00:00:00 kintegrityd
unconfined 90 ? 00:00:00 kblockd
unconfined 91 ? 00:00:00 blkcg_punt_bio
unconfined 92 ? 00:00:00 tpm_dev_wq
unconfined 93 ? 00:00:00 ata_sff
unconfined 94 ? 00:00:00 md
unconfined 95 ? 00:00:00 edac-poller

You may also use this trick to back up a remote directory to a local machine over SSH.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents]
└─$ rsync -avz -e ssh jason@192.168.1.2:/home/jason/Documents/Boomsrc /home/john/Documents/Boomsrc
jason@192.168.1.2's password: 
receiving incremental file list
created directory /home/john/Documents/Boomsrc
Boomsrc/
Boomsrc/about.txt
Boomsrc/allegro.h
Boomsrc/am_map.c
Boomsrc/am_map.h
Boomsrc/bin2c.c
Boomsrc/bmp2c.c
Boomsrc/book.old
Boomsrc/boom.txt
Boomsrc/boomdeh.txt
Boomsrc/boomref.txt
Boomsrc/changes.old
Boomsrc/common.cfg
Boomsrc/copying
Boomsrc/d_deh.c
Boomsrc/d_deh.h
Boomsrc/d_englsh.h
Boomsrc/d_event.h
Boomsrc/d_french.h
Boomsrc/d_items.c
Boomsrc/d_items.h
Boomsrc/d_main.c
Boomsrc/d_main.h
Boomsrc/d_net.c
Boomsrc/d_net.h
Boomsrc/d_player.h
Boomsrc/d_textur.h
Boomsrc/d_think.h
Boomsrc/d_ticcmd.h
Boomsrc/dckboom.txt
Boomsrc/defswani.dat
Boomsrc/doom17.dat
Boomsrc/doom19.dat
Boomsrc/doomdata.h
Boomsrc/doomdef.c

It is also possible to use compression on an SSH connection to compress packets passing to and fro over a network connection.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents]
└─$ ssh -C -c aes256-ctr jason@192.168.1.2
jason@192.168.1.2's password: 
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.4.0-125-generic x86_64)
 
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
 
  System information as of Thu 22 Sep 2022 08:47:50 AEST
 
  System load:              0.0
  Usage of /:               11.1% of 915.82GB
  Memory usage:             2%
  Swap usage:               0%
  Temperature:              39.0 C
  Processes:                189
  Users logged in:          0
  IPv4 address for enp0s25: 192.168.1.2
  IPv6 address for enp0s25: fdc8:1451:5fa9:4700:d250:99ff:fe0d:ab0f
 
Save time starting to type a command or file name, then press tab to
complete Hit tab twice to bring up multiple completion options.
 
0 updates can be applied immediately.
 
New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
 
Last login: Thu Sep 22 08:18:37 2022 from 192.168.1.5

If you try and use a cipher that is not supported on the server, it will print a listing of all available ciphers to choose from.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents]
└─$ ssh -C -c aes256-cbc jason@192.168.1.2
Unable to negotiate with 192.168.1.2 port 22: no matching cipher found. Their offer: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com

2 thoughts on “Use a pseudo-terminal over SSH to run a command on a remote machine.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.