SSH
, but also more competently navigate the network.ssh
tricks is useful to any system administrator, network engineer, or security specialist.SSH
server. localhost:~$ ssh -v -p 22 -C neo@remoteserver
-v
: debug output is especially useful when analyzing authentication problems. Can be used several times to display additional information.- p 22
: port for connecting to a remote SSH server. 22 is not necessary to specify, because this is the default value, but if the protocol is on some other port, then we specify it with the help of the -p
parameter. The listening port is specified in the sshd_config
file in the format Port 2222
.-C
: compression for connection. If you have a slow channel or you view a lot of text, it can speed up the connection.neo@
: the line before the symbol @ denotes the username for authentication on the remote server. If you do not specify it, the default will be the username of the account you are currently logged in to (~ $ whoami). The user can also be specified with the -l
option.remoteserver
: the name of the host to which ssh
connected, this can be a fully qualified domain name, IP address or any host in the local hosts file. To connect to a host that supports both IPv4 and IPv6, you can add the -4
or -6
option to the command line for proper resolution.remoteserver
.sshd_config
file, there is also a client configuration file for the ssh
command. The default is ~/.ssh/config
, but it can be defined as a parameter for the -F
option. Host * Port 2222 Host remoteserver HostName remoteserver.thematrix.io User neo Port 2112 IdentityFile /home/test/.ssh/remoteserver.private_key
localhost:~$ scp mypic.png neo@remoteserver:/media/data/mypic_2.png
scp
from the command line. Here the port parameter is -P
, and not -p
, as in the ssh client! You will forget, but do not worry, everyone will forget.ftp
, many of the commands are similar in sftp
. You can push , put, and ls as your heart desires. sftp neo@remoteserver
localhost:~$ ssh -D 8888 user@remoteserver localhost:~$ netstat -pan | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN 23880/ssh
localhost:~$ ssh -D 0.0.0.0:8888 user@remoteserver
localhost:~$ google-chrome --proxy-server="socks5://192.168.1.10:8888"
localhost:~$ proxychains rdesktop $RemoteWindowsServer
Hint: if using remote desktop from linux to windows? Try the FreeRDP client. This is a more modern implementation than rdesktop
, with much smoother interaction.
localhost:~$ ssh -L 9999:127.0.0.1:80 user@remoteserver
-L
parameter. It can be thought of as the local listening side. Thus, in the example above, port 9999 is tapped on the localhost side and forwarded via port 80 to remoteserver. Please note that 127.0.0.1 refers to localhost on a remote server! localhost:~$ ssh -L 0.0.0.0:9999:127.0.0.1:80 user@remoteserver
localhost:~$ ssh -L 0.0.0.0:9999:10.10.10.10:80 user@remoteserver
localhost:~$ ssh -v -R 0.0.0.0:1999:127.0.0.1:902 192.168.1.100 user@remoteserver
localhost:~$ ssh -v -R 0.0.0.0:1999 192.168.1.100 user@remoteserver
netstat
what other interfaces the listening port is connected to. Although we specified 0.0.0.0 in the examples, but if the value of GatewayPorts in sshd_config is set to no , then the listener will be bound to localhost (127.0.0.1) only.Safety warning
Please note that when opening tunnels and socks-proxies, internal network resources may be accessed by unreliable networks (for example, the Internet!). This can be a serious security threat, so make sure you understand what the listener is and what it has access to.
SYN
.ssh
, iptables
, tun interfaces
and routing.sshd_config
. Since we are making changes to the interfaces of both the remote and client systems, we need root rights on both sides . PermitRootLogin yes PermitTunnel yes
localhost:~# ssh -v -w any root@remoteserver
# ip a
). The next step is to add IP addresses to the tunnel interfaces. localhost:~# ip addr add 10.10.10.2/32 peer 10.10.10.10 dev tun0 localhost:~# ip tun0 up
remoteserver:~# ip addr add 10.10.10.10/32 peer 10.10.10.2 dev tun0 remoteserver:~# ip tun0 up
route -n
and ping 10.10.10.10
). localhost:~# route add -net 10.10.10.0 netmask 255.255.255.0 dev tun0
ip_forward
and iptables
. remoteserver:~# echo 1 > /proc/sys/net/ipv4/ip_forward remoteserver:~# iptables -t nat -A POSTROUTING -s 10.10.10.2 -o enp7s0 -j MASQUERADE
ping
to determine the cause. Since we are playing at level 3, our icmp packages will go through this tunnel.~/.ssh/authorized_keys
on the remote server. localhost:~$ ssh-copy-id user@remoteserver
ssh
command can be linked to other commands for a normal user-friendly interface. Just add the command you want to run on the remote host as the last parameter in quotes. localhost:~$ ssh remoteserver "cat /var/log/nginx/access.log" | grep badstuff.php
grep
is executed on the local system after the log has been downloaded via the ssh channel. If the file is large, it is more convenient to run grep
on the remote side simply by enclosing both commands in double quotes.ssh-copy-id
from example 7. localhost:~$ cat ~/.ssh/id_rsa.pub | ssh remoteserver 'cat >> .ssh/authorized_keys'
:~$ ssh root@remoteserver 'tcpdump -c 1000 -nn -w - not port 22' | wireshark -k -i -
bzip2
(this is the -j parameter in the tar
command) and then extracts the bzip2
stream on the other side, creating a duplicate folder on the remote server. localhost:~$ tar -cvj /datafolder | ssh remoteserver "tar -xj -C /datafolder"
localhost:~$ ssh -X remoteserver vmware
X11Forwarding yes
in the sshd_config
file is required.rsync
is much more convenient than scp
if periodic backup of a directory, a large number of files or very large files is required. There is a recovery function after the failure of the transfer and copy only the modified files, which saves traffic and time.gzip
(-z) compression and archive mode (-a), which includes recursive copying. :~$ rsync -az /home/testuser/data remoteserver:backup/
torsocks
. The following command will proxy ssh-proxy through Tor. localhost:~$ torsocks ssh myuntracableuser@remoteserver
chmod 400 my-ec2-ssh-key.pem
). Keep the key in a safe place or place it in your ~/.ssh/
folder. localhost:~$ ssh -i ~/.ssh/my-ec2-key.pem ubuntu@my-ec2-public
~/.ssh/config
file is ideal for automatically configuring key usage when connected to the ec2 host. Host my-ec2-public Hostname ec2???.compute-1.amazonaws.com User ubuntu IdentityFile ~/.ssh/my-ec2-key.pem
vim
lovers, this advice will save some time. With vim
files are edited for scp with one command. This method simply creates the file locally in /tmp
, and then copies it back as soon as we save it from vim
. localhost:~$ vim scp://user@remoteserver//etc/hosts
scp
. After the host we have a double //
. This is a link to an absolute path. One slash will be the path relative to the users
home folder. **warning** (netrw) cannot determine method (format: protocol://[user@]hostname[:port]/[path])
sshfs
, the ssh
file system client, we can connect a local directory to a remote location with all file interactions in an encrypted ssh
session. localhost:~$ apt install sshfs
sshfs
package, and then simply accept the remote location to our system. localhost:~$ sshfs user@remoteserver:/media/data ~/data/
ssh
second connection using ssh
or scp
establishes a new session with additional authentication. The ControlPath
option allows you to use an existing session for all subsequent connections. This will significantly speed up the process: the effect is noticeable even in the local network, and even more so when connected to remote resources. Host remoteserver HostName remoteserver.example.org ControlMaster auto ControlPath ~/.ssh/control/%r@%h:%p ControlPersist 10m
ssh
session. The last option means that even after logging out of the console, the existing session will remain open for 10 minutes, so during this time you can reconnect over the existing socket. See the ssh_config man
help for more information.ssh
and vlc
(Video Lan Client) are not always aware of this convenient option, when you really need to watch videos over the network. In the settings File | Open Network Stream vlc
programs can be entered as sftp://
. If a password is required, a prompt will appear. sftp://remoteserver//media/uploads/myvideo.mkv
ssh
initially has a two-factor authentication function, which means a password and an SSH key. The advantage of a hardware token or Google Authenticator application is that it is usually a different physical device. localhost:~$ ssh -J host1,host2,host3 user@host4.internal
ssh host1
, then user@host1:~$ ssh host2
and so on. The -J parameter cleverly uses redirection so that localhost establishes a session with the next host in the chain. Thus, in the example above, our localhost is authenticated to host4. That is, our localhost keys are used, and the session from localhost to host4 is fully encrypted.sshd_config
file using the Port ## configuration parameter.iptables
, you can also easily block attempts to connect to a port when a certain threshold is reached.The easiest way to do this is to use OSSEC , since it not only blocks SSH, but performs a bunch of other intrusion detection measures based on the host name (HIDS).ssh
intended to change the port forwarding on the fly within an existing session ssh
. Imagine such a scenario. You are deep in the net; you may have jumped through half a dozen hosts and you need a local port on the workstation that is redirected to the Microsoft SMB of the old Windows 2003 system (does anyone remember ms08-67?).enter
, try typing in console ~C
. This is a control sequence in a session that allows you to make changes to an existing connection. localhost:~$ ~C ssh> -h Commands: -L[bind_address:]port:host:hostport Request local forward -R[bind_address:]port:host:hostport Request remote forward -D[bind_address:]port Request dynamic forward -KL[bind_address:]port Cancel local forward -KR[bind_address:]port Cancel remote forward -KD[bind_address:]port Cancel dynamic forward ssh> -L 1445:remote-win2k3:445 Forwarding port.
msfconsole
, (, ).ssh
; ( man ssh
, man ssh_config
, man sshd_config
).ssh
, .Source: https://habr.com/ru/post/435546/
All Articles