(Maybe) Useful commands
Some of the commands that I use daily 😄. Hope those may speed you a little up on your Linux work!
All of the commands below assume you're using the bash shell v4.
!$
("bang dollar")
Use !$
("bang dollar") to refer to the last argument on the previous command:
For more techniques, please refer to Directory Navigation
Reserve search
Quickly search previous commands, Ctrl + R
(R = reverse) and Type the keyword
. If you want older commands, keep pressing Ctrl + R
$ echo 1
1
$ echo 2
2
$ echo 3
3
# Ctrl + R (MacOS)
(reverse-i-search)`echo': echo 3
# Ctrl + R again`
(reverse-i-search)`echo': echo 2
# Ctrl + C
# Even `ho` keyword still works
(reverse-i-search)`ho': echo 3
Check open ports
Telnet port without telnet
or nc
command. For example, if you want to check the host 1.2.3.4 port 80:
$ echo > /dev/tcp/1.2.3.4/80
# hang means you cannot access that port
$ echo > /dev/tcp/8.8.8.8/53
$ # return immediately means ok
SSH tunnel
Forward a port from remote hosts
We want to connect serverX port 80 from our laptop (client)
$ ssh jump-host -L 8080:serverX:80
# connection to our laptop port 8080
# will be forwarded to serverX port 80
Forward our port to a remote server
We want our laptop port 1234 to be accessible on jump-host
$ ssh jump-host -R 1234:jump-host:5678
# connection to jump-host port 5678
# will be forwarded to our laptop port 1234
Simple web service with nc
Sometimes we want to receive the headers and payloads from another service for further analysis. For example, the payload using the webhook method from the notification services.
while true ; do
echo -e "HTTP/1.1 200 OK\n\n $(date)" | nc -l -p 1500;
done
So in the notification service, we can just set the webhook value to our http://our-server:1500
Subshell command as files
This is called Process Substitution
<(any command here)
In Linux, when a disk file is opened, it's assigned a number called a file descriptor. Process Substitution acts like a file by running a command and connecting its output to a file descriptor. This makes the output appear as a file to programs. You can view the file descriptor with echo:
$ echo <(ls)
/dev/fd/63
The standard input, standard output, and standard error are denoted by the file descriptors 0, 1, and 2.