Posted: . At: 9:15 AM. This was 2 years ago. Post ID: 7514
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.

Useful SSH tricks for downloading files from an SSH server to your machine.

This is how to connect to a machine with an SSH server installed and download a file from that machine to your local computer. The SFTP command allows this to be accomplished with ease.

Administrator@WIN-EM8GK0ROU41 ~
$ sftp -P 443 homer@192.168.100.8:/home/homer/Documents/my2.c
LMDE MATE Editionhomer@192.168.100.8's password:
Connected to 192.168.100.8.
Fetching /home/homer/Documents/my2.c to my2.c
/home/homer/Documents/my2.c

Here is another example that is using wildcards.

Administrator@WIN-EM8GK0ROU41 ~
$ sftp -P 443 homer@192.168.100.8:/home/homer/Documents/*.c
LMDE MATE Editionhomer@192.168.100.8's password:
Connected to 192.168.100.8.
Fetching /home/homer/Documents/hello.c to hello.c
/home/homer/Documents/hello.c                                                                                                                      100%   62     0.1KB/s   00:00
Fetching /home/homer/Documents/multicol.c to multicol.c
/home/homer/Documents/multicol.c                                                                                                                   100%   10KB  10.3KB/s   00:01
Fetching /home/homer/Documents/my.c to my.c
/home/homer/Documents/my.c                                                                                                                         100%  566     0.6KB/s   00:00
Fetching /home/homer/Documents/my2.c to my2.c
/home/homer/Documents/my2.c                                                                                                                        100% 1420     1.4KB/s   00:00
Fetching /home/homer/Documents/randimg.c to randimg.c
/home/homer/Documents/randimg.c                                                                                                                    100%   15KB  14.9KB/s   00:01
Fetching /home/homer/Documents/rot13.c to rot13.c
/home/homer/Documents/rot13.c                                                                                                                      100%  296     0.3KB/s   00:00
Fetching /home/homer/Documents/self.c to self.c
/home/homer/Documents/self.c                                                                                                                       100%  210     0.2KB/s   00:00
Fetching /home/homer/Documents/time.c to time.c
/home/homer/Documents/time.c                                                                                                                       100%  802     0.8KB/s   00:01
Fetching /home/homer/Documents/tune.c to tune.c
/home/homer/Documents/tune.c                                                                                                                       100% 1321     1.3KB/s   00:00
Fetching /home/homer/Documents/x11.c to x11.c
/home/homer/Documents/x11.c

Use the lastlog command to view all logins on the server machine.

homer@deusexmachina ~ $ lastlog
Username         Port     From             Latest
root                                       **Never logged in**
daemon                                     **Never logged in**
bin                                        **Never logged in**
sys                                        **Never logged in**
sync                                       **Never logged in**
games                                      **Never logged in**
man                                        **Never logged in**
lp                                         **Never logged in**
mail                                       **Never logged in**
news                                       **Never logged in**
uucp                                       **Never logged in**
proxy                                      **Never logged in**
www-data                                   **Never logged in**
backup                                     **Never logged in**
list                                       **Never logged in**
irc                                        **Never logged in**
gnats                                      **Never logged in**
nobody                                     **Never logged in**
libuuid                                    **Never logged in**
avahi-autoipd                              **Never logged in**
messagebus                                 **Never logged in**
usbmux                                     **Never logged in**
ntp                                        **Never logged in**
avahi                                      **Never logged in**
colord                                     **Never logged in**
dnsmasq                                    **Never logged in**
pulse                                      **Never logged in**
rtkit                                      **Never logged in**
saned                                      **Never logged in**
davfs2                                     **Never logged in**
mdm                                        **Never logged in**
hplip                                      **Never logged in**
homer            pts/1    103.1.7.222      Mon Jul 14 10:38:15 +1000 2014
sshd                                       **Never logged in**

This is another way to get this information. The w command is a way to list all active logins.

homer@deusexmachina ~ $ w
 10:40:55 up  1:06,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
homer    tty8     :0               19:36     ?     1:19   0.01s /bin/sh /etc/xdg/xfce4/xinitrc -- /etc/X11/xinit/xserverrc
homer    pts/0    :0.0             09:46   54:39   0.11s  0.11s bash
homer    pts/1    103.1.7.222      10:38    7.00s  0.13s  0.00s w

Finally; this command will list all active logins.

homer@deusexmachina ~ $ finger -lmps
Login: homer                            Name: John Cartwright
Directory: /home/homer                  Shell: /bin/bash
On since Mon Jul 14 19:36 (EST) on tty8 from :0 (messages off)
On since Mon Jul 14 09:46 (EST) on pts/0 from :0.0
   56 minutes idle
On since Mon Jul 14 10:38 (EST) on pts/1 from 103.1.7.222
No mail.

To login to a specific port on an SSH server; use this command.

ssh user@10.1.1.1 -p 443

This will log in to the SSH server using port 443. This is very useful if you want to use this port to allow SSH access through a firewall that blocks port 22.

The netstat utility may also be used to list all SSH connections, this is a sample from Kali Linux.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ netstat 
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 172.23.15.240:41958     192.168.1.2:ssh         ESTABLISHED
tcp        0      0 172.23.15.240:53456     76.76.21.22:https       TIME_WAIT  
tcp        0      0 172.23.15.240:47400     76.76.21.61:https       TIME_WAIT  
tcp        0      0 172.23.15.240:52190     76.76.21.164:https      ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         SEQPACKET  CONNECTED     17228    @00001
unix  3      [ ]         SEQPACKET  CONNECTED     17230    @00002
unix  3      [ ]         STREAM     CONNECTED     17702    
unix  3      [ ]         STREAM     CONNECTED     17703    
unix  2      [ ]         SEQPACKET  CONNECTED     17573    
unix  3      [ ]         STREAM     CONNECTED     14       
unix  3      [ ]         STREAM     CONNECTED     15       
unix  3      [ ]         STREAM     CONNECTED     17582    
unix  3      [ ]         STREAM     CONNECTED     18       
unix  3      [ ]         STREAM     CONNECTED     17301    /tmp/.X11-unix/X0

This shows the local and destination addresses of the SSH connection.

Leave a Comment

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