Useful bash and friends commands

Useful bash and friends commands

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:

  1. ssh
  2. scp
  3. aliases
  4. less
  5. grep

1. ssh

1.1 Remote actions

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.

ssh -i key.pem user:hostname

-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.

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.

ssh hostname1

1.2 ssh tunneling

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.

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:

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.

2. scp

scp provides a remote copy functionality:

scp -r /dir hostname1:~/

-r is needed to copy a folder.
hostname1 is the host defined in the configuration file ~/.ssh/config.

It runs on ssh therefore we would need to setup ssh to autodetect the identity file as described in 1.1).

Without the config file we would have had done the following:

scp -i key.pem -r /dir [email protected]:~/

3. Aliases

To create aliases, we need to use the alias command. For example if we want to create a command to ssh and scp:

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 .bash_aliases.

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 .bash_aliases:

# 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

4. less

For watching a file, we can use less.

less +F /var/log/myapp/app.log

5. grep

Lastly 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:

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.

Conclusion

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!

Comments

Popular posts from this blog

Manage assets and static files with Angular CLI

Absolute layout and relative layout Xamarin forms

Prime NG data table for Angular