May 18th, 2018 - written by Kimserey with .
Since I have installed Ubuntu as a subsystem, I see myself using more and more bash. The reason being that all the VMs I spin up are Ubuntu VMs. My interactions with my servers are very basic but even for those, there are many beautiful commands which ease my interactions. Today I would like to go through the commands and tips which I use on a daily basis.
The ssh command allows us to remotly access a terminal or to execute commands remotly.
For example when we create a new EC2 instance on AWS we can download the private key. This key can be used to ssh into the vm.
1 ssh -i key.pem [email protected]
-i is used to indicate the identity file to use. You might need to change the permissions on the file before it can be used with
chmod 700 key.pem.
It is also possible, to make life easier, to setup ssh to autodetect the private key file to use based on the host we are trying to access.
We can achieve that by placing the
key.pem file under
~/.ssh and provide the proper rights
chmod 700 ~/.ssh.
Next create or modify the
~/.ssh/config file to specify the identity file and the hostname for this host.
1 2 3 4 5 6 7 8 9 Host hostname1 HostName hostname1.com IdentityFile ~/.ssh/key1.pem User user Host hostname2 HostName hostname2.com IdentityFile ~/.ssh/key2.pem User user
Now we will no longer need to specify the identity file and the hostname to ssh into the server.
1 ssh hostname1
Another useful functionality is the tunneling
-L. SSH allows to redirect traffic from a
Local port to a particular address on the server.
This is particularly useful when an address is only accessible from the server.
1 ssh hostname1 -fNL 9500:localhost:9000
-f is used to start the tunneling in the background. To terminate it we can find the process by doing
ps aux | grep 9500:localhost:9000 and then execute a
kill -9 processid to kill the ssh process.
-N is to specify that we do not want to execute any remote command.
This command will tunnel all traffic from our port 9500 to localhost:9000 on the server.
This can also be setup in the config file:
1 2 3 4 5 6 7 8 9 10 11 Host hostname1 HostName hostname1.com IdentityFile ~/.ssh/key1.pem User user # tunnel to hostname1.com Host tunnel HostName hostname1.com IdentityFile ~/.ssh/key1.pem LocalForward 9500 localhost:9000 User user
Then we will be able to do
ssh tunnel to start a tunnel. We can also use the arguments the same way with
ssh tunnel -fN.
scp provides a remote copy functionality.
We can copy from local directory to a remote destination:
1 scp -r ./dir hostname1:~/
-ris needed to copy a folder,
hostname1is the host defined in the configuration file
We can also dowload from a remote destination:
1 scp hostname1:~/myfile.txt ./dir/
It runs on ssh therefore we would need to setup ssh to autodetect the identity file as described earlier. Without the config file we would have had done the following:
1 scp -i key.pem -r ./dir [email protected]:~/
To create aliases, we need to use the
alias command. For example if we want to create a command to ssh and scp:
1 2 alias ssh-myapp='ssh hostname1' alias scp-myapp='scp -r /dir hostname1:~/'
But those are temporary, if we want to persist them, we can add them to
1 sudo vim ~/.bash_aliases
.bash_aliases is actually called from
.bashrc which is the file containing the command to run (rc: run command) before the interactive bash starts. This the line which calls
1 2 3 4 5 6 7 8 # Alias definitions. # You may want to put all your additions into a separate file like # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
For watching a file, we can use
1 less +F /var/log/myapp/app.log
grep is used to find a particular line in a text.
We seen earlier that we can pipe
| grep to find a process but we can also pipe anything which is a text format. For example we can also pipe
man which is the manual and find the definition of a partulcar argument:
1 man ssh | grep -C3 -- -N
-C prints the 3 surrounding lines of each match.
-- (double dash) is used to define the end of the options so that we can specify
-N as a string pattern. Without it, it would have been considered as an option.
And that concludes today’s post.
Today we saw some useful commands on bash mainly targeted to interact with a Linux server, copy files and create useful aliases to make work quicker. We saw how we could configure and use
ssh to login on a remote server, we also saw how we could use
scp to copy files in a secured way and lastly we saw how to save
alias to make it quicker and to not have to memorize server addresses and lastly we saw a useful
less command to watch files and
grep to find a particular line in a text input. Hope you liked this post, see you next time!