If you use Linux, you definitely use aliases, which are shortcuts in the terminal.
These aliases are very convenient and speed up the job of the System Engineer. A system administrator who uses aliases (and functions) will always be faster and more efficient than a system administrator who writes the same commands by hand every time.
Obviously as soon as you connect to a remote server with the SSH connection you cannot use the aliases you created in bashrc on your machine. In order to use these aliases, you would have to copy the bashrc file to the remote machine.
In this article, we are going to see how to use SSHRC to carry our aliases with us when we connect to a remote machine in order to speed up our work.
Table of Contents
![Sshrc](https://supporthost.com/wp-content/uploads/2022/03/sshrc-1024x538.png)
Alias bashrc Linux and Mac
I want to start with a brief introduction on aliases.
If you use Linux, but also if you are using a Mac, chances are you use the terminal a lot.
For example, you are likely to use the command often:
ls -alh
On Linux, in your user’s home folder you will find a file called .bashrc
![Sshrc File Bashrc Linux](https://supporthost.com/wp-content/uploads/2022/05/sshrc-file-bashrc-linux.png)
This file contains some directives, we can add our own.
For example, by opening the file with a text editor such as “vi” we can add aliases.
In the case of Mac, by default, instead of bash, the terminal uses zsh, so the file to modify will be .zshrc. The syntax remains the same, as does the function.
For example, I can add this line to the file:
alias l = 'ls -lah'
At this point, every time you type “l” in the terminal it is as if you were typing the command in full: ls -lah
![Sshrc Local Alias Linux](https://supporthost.com/wp-content/uploads/2022/05/sshrc-local-alias-linux.png)
It goes without saying that if you have commands that you use frequently, having an alias speeds up your work since you have to type fewer characters.
Note that after editing the .bashrc file or the .zshrc file you must close and reopen the terminal in order to use your alias.
Now that we’ve seen how aliases can come in handy, let’s see how to carry them around when we connect to a remote server.
SSHRC: what it is and how we can use it
It was originally created by Russell Stewart (Russell91) who later abandoned the project, which is now maintained by Chris Down (cdown) on github. Find all the details here.
SSHRC is a shell script that you can easily install on your Mac or Linux machine that allows you to carry your aliases and functions with you to use them on a remote server.
![Sshrc Terminal](https://supporthost.com/wp-content/uploads/2022/05/sshrc-terminal.png)
Basically when you connect using the sshrc command instead of ssh, this script looks for the .sshrc file in your user’s home folder and takes all the aliases with it.
Normally to connect via SSH to a remote server you would use the command:
ssh [email protected]
To use SSHRC you will need to use:
sshrc [email protected]
Using this command, once connected to the server, you will have all your aliases available.
We will then see how to use SSHRC using the SSH command, in practice we will see how to use SSHRC as the default method to connect to a remote server.
How to install SSHRC
In the github repository that I linked in the previous section you will find the installation instructions.
I like to install it by hand, let’s see how.
First we download the sshrc file on our machine:
wget https://raw.githubusercontent.com/cdown/sshrc/master/sshrc
Let’s set the permissions to make it executable:
chmod + x sshrc
And we move it to the / usr / local / bin folder whether we are on Linux or Mac:
sudo mv sshrc / usr / local / bin
Now that we’ve installed SSHRC and made it executable, let’s create the .sshrc file.
We then move to our user’s home and create the file:
touch .sshrc
Now let’s edit the file by adding all the aliases we need, we can do the test with the alias I indicated before:
alias l = 'ls -lah'
We can now connect using SSHRC and carry our aliases with us.
SSH connection with SSHRC
As I mentioned before, connecting with SSHRC is very easy, in practice it is like using the ssh command:
sshrc [email protected]
Once you’re connected to the server, just type in one of your aliases to make sure it works.
Set SSHRC as the default method
If you think this system is good and you want to use it all the time, you might as well set it as default. This way every time you connect using SSH you will be effectively using SSHRC.
Doing this is very easy, we just have to set an alias in our .bashrc or .zshrc file like this:
alias ssh = 'sshrc'
This way every time you use the ssh command you will be effectively using sshrc.
Note: if you have any aliases that use the ssh command you should put this alias before the others.
If for some reason you decide that for a specific connection you want to use the ssh command normally just insert a backslash before the command. Inserting a backslash serves not to use an alias if present:
\ssh [email protected]
Use the .sshrc file
Everyone has different needs, so everyone will need their own custom .sshrc file.
![Sshrc Programming](https://supporthost.com/wp-content/uploads/2022/05/sshrc-programming.png)
If you find yourself using the same command several times it might be worth creating an alias.
If you need to run a script you may decide to create a function.
In other cases it may be useful to create aliases with a security function.
Let’s see in detail.
Use aliases for protection
The first aliases I created I created them to avoid errors:
alias mv='mv -i' alias cp='cp -i' alias ln='ln -i' alias rm='rm -i'
This way before removing or moving a file and creating a problem, the server asks me for confirmation, and I am able to avoid an error for an oversight.
Create custom aliases
As we have seen before we can create custom aliases, which speed up us in typing the commands we use most frequently:
alias l='ls -lah' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' alias vi="vim" alias tailf='tail -f' alias head='head -50' alias fixperms='find ./* -type d -exec chmod --preserve-root 0755 {} + && find . -type f -exec chmod --preserve-root 0644 {} +'
These are some of the aliases I have set in my file.
Set functions
We can also create real scripts, very useful for speeding up everyday work.
sh(){ echo -n "Enter IP: "; read ip; ssh root@$ip }
For example, this function asks us for a parameter, which once entered is used as the IP address for the SSH connection.
A very useful function in my opinion is this:
intro () { #esegui varie operazioni echo "benvenuto Ivan"; } intro
Basically, we create a function and call it immediately after. In this specific case, it echoes a simple welcome message.
I use such a function to see the software versions on the server, the load and disk usage.
Example of .sshrc file
Let’s look at an example file, which could be useful as a starting point for creating your file.
# Clear alias c="clear" alias cl="clear" alias ckear="clear" alias clr="clear" # Change Directories alias ..="cd .." alias ...="cd ../.." alias ....="cd ../../.." alias .....="cd ../../../.." alias ......="cd ../../../../.." # Automatically do an ls after each cd cd() { if [ -n "$1" ]; then builtin cd "$@" && ls --group-directories-first -al else builtin cd ~ && ls --group-directories-first -al fi } #============================================================= # cd & ls in one :- when you cd to a directory the ls command # automatically lists it's contents however you have configred # ls to do so: #------------------------------------------------------------- cl() { if [ -d "$1" ]; then cd "$1" ls -la --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F else echo "bash: cl: '$1': Directory not found" fi } #============================================================= # cd & ls & more in the same command. Pipes the output to more #for use with directories with many files in their contents: #------------------------------------------------------------- # cd and ls & more in one cm() { if [ -d "$1" ]; then cd "$1" ls | more else echo "bash: cl: '$1': Directory not found" fi } # Estimate file space usage to maximum depth alias du1="du -d 1" # History commands alias h="history" alias h1="history 10" alias h2="history 20" alias h3="history 30" alias hgrep='history | grep' # List commands alias l='ls -lAh' alias ls="ls -a" alias la="ls -a" alias ll="ls -al" # Ping Commands alias pg="ping google.com -c 5" alias pf="ping facebook.com -c 5" alias ping="ping -c 5" alias fastping="ping -c 100 -s.2" # Confirmation alias rm='rm -Iv --preserve-root' alias mv='mv -iv' alias cp='cp -iv' alias ln='ln -i' alias chown='chown --preserve-root' alias chmod='chmod --preserve-root' alias chgrp='chgrp --preserve-root' # Create a new directory and enter it function mkd() { mkdir -p "$@" && cd "$@" } # Install & Update utilties alias sai="sudo apt install" alias sai="sudo apt-get install" alias sau="sudo apt update" alias sau="sudo apt-get update" alias update="sudo apt update" alias update="yum update" alias updatey="yum -y update" # System state alias reboot="sudo /sbin/reboot" alias poweroff="sudo /sbin/poweroff" alias halt="sudo /sbin/halt" alias shutdown="sudo /sbin/shutdown" alias flighton='sudo rfkill block all' alias flightoff='sudo rfkill unblock all' alias snr='sudo service network-manager restart' # Show open ports alias ports='sudo netstat -tulanp' # Free and Used alias meminfo="free -m -l -t" # Get top process eating memory alias psmem="ps auxf | sort -nr -k 4" alias psmem10="ps auxf | sort -nr -k 4 | head -10" # Get top process eating cpu alias pscpu="ps auxf | sort -nr -k 3" alias pscpu10="ps auxf | sort -nr -k 3 | head -10" # Get details of a process alias paux='ps aux | grep' # Get server cpu info alias cpuinfo="lscpu" # Resume wget by default alias wget="wget -c" # Grabs the disk usage in the current directory alias usage='du -ch | grep total' # Gets the total disk usage on your machine alias totalusage='df -hl --total | grep total' # Shows the individual partition usages without the temporary memory values alias partusage='df -hlT --exclude-type=tmpfs --exclude-type=devtmpfs' # Gives you what is using the most space. Both directories and files. Varies on current directory alias most='du -hsx * | sort -rh | head -10' #functions function extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xjf $1 ;; *.tar.gz) tar xzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xf $1 ;; *.tbz2) tar xjf $1 ;; *.tgz) tar xzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *) echo "'$1' cannot be extracted via extract()" ;; esac else echo "'$1' is not a valid file" fi } #trash function trash(){ if [ -z "$*" ] ; then echo "Usage: trash filename" else DATE=$( date +%F ) [ -d "${HOME}/.Trash/${DATE}" ] || mkdir -p ${HOME}/.Trash/${DATE} for FILE in $@ ; do mv "${FILE}" "${HOME}/.Trash/${DATE}" echo "${FILE} trashed!" done fi } # find - exclude svn, git, mercurial, repo, eclipse and intellij idea directories # usage : f [directory] [find-options] f(){ dir=$1 shift find $dir -type d \( -name .svn -o -name .git -o -name .hg -o -name .repo -o -name .metadata -o -name .idea \) -prune -o $@ -print } pause() { if $(killall -STOP "$1"); then echo "[+] $1 successfully paused!" else echo "[-] Failed to pause $1" fi } unpause() { if $(killall -CONT "$1"); then echo "[+] $1 successfully unpaused!" else echo "[-] Failed to unpause $1" fi }
Conclusions
In this article we have seen what SSHRC is and how we can use it to speed up our work as systems engineers.
Were you already familiar with this system? If yes, how do you use it? If no, do you think it could be useful to you? How would you use it?
Let me know in a comment!
Leave a Reply