Top 50 Most Useful and Frequently Used SSH Commands

Secure Shell or SSH is a well-known term to whom that manages UNIX/Linux based servers. It provides safe and secure remote login to the servers and allows running numerous useful SSH commands. It is basically a command line tool in UNIX type of system, e.g. command prompt in windows. You can use Terminal in Mac and PuTTY in Windows to access servers via SSH. Here is a list of most frequently used SSH commands that may become useful when you need.

Useful SSH Commands for UNIX/Linux Based Systems

Useful SSH commands for directory and file browsing and handling:
cd – to change between directories
cd ~/ – to go to logged in user’s home directory
cd .. – to go up one level in the directory tree
cd - – to go to the last directory you were in
cd /home/username/public_html/some/sample/directory/ – go to a directory ( a common pattern in cPanel based system)
cd /var/www/vhosts/domain.com/htdocs/some/sample/directory/ – go to a directory ( a common pattern in Plesk based system)
pwd – to check where you are currently i.e. present working directory
ls – list files and directories
ls -l – list files and directories with information
ls -al – list all files (i.e. including hidden and dot files) and directories with information
ls -alh – list all files and directories with information and filesize
ls *.ext – to list files of specific extension (i.e. ls *.php)
cp /source/location/filename.ext /destination/location/filename.ext – copy file from one location to another
cp /source/location/filename.ext /destination/location/differentname.ext – copy file from one location to another with a different name
cp -r /source/directory/path/ /destination/directory/path/ – copy a directory with all files and directories in it
mv /source/location/filename.ext /destination/location/filename.ext – move file from one location to another
mv /source/location/filename.ext /destination/location/differentname.ext – move file from one location to another and then rename it
rm filename.ext – remove a file
rm * – remove all files
rm *.ext – remove all files based on extension
rm -rf – remove all files and directories recursively
mkdir directory_name – create a new directory
mkdir /existing/directory/new_directory – create a new directory within an existing directory
touch new_filename.ext – create a new file

Copy publicly available file from one server to another
wget http://www.source-domain.come/file/path/file.tar.gz – This command will use cURL method and get the file over public bandwidth

Copy a file securely from one server to another:
Log in to the destination server using the root user, and then run the following command.
cat ~/.ssh/id_rsa.pub – if it says no such file or directory exists, then run the following command.
ssh-keygen – It will prompt for the file name, just press enter. Then it will prompt for a password, just press enter twice. Your SSH public and private key pair are ready now. Now run the first command, i.e. cat ~/.ssh/id_rsa.pub again and copy the key.
Now login to the source server using root user, and then run the following commands.
nano ~/.ssh/authorized_keys OR vi ~/.ssh/authorized_keys (for the later command need to press “insert” key [for PuTTY] or “i” key [for mac/Linux terminal]) and paste the key that you have copied earlier. Now save the file (Ctrl + X for “nano” OR “esc” followed by “:wq” and “enter” for “vi”). Now your destination server can securely log in to the source server. Run the following command now to copy file securely:
scp [email protected]:/path/in/source/server/filename.ext /path/in/destination/server/filename.ext – this will copy the file from one server to another securely (the IP used here is the source server IP)

Useful SSH commands to search and find directory and files:
find . -name filename.ext -print – search for a file starting within the current directory
find . -name "filenamepart*" – search for a file using wildcard pattern matching
grep header index.php – search for text within a file
grep -r -H "search string" * – look for the text in all files
which git OR whereis git – to find a file in the whole server

Useful SSH commands to set directory and file permission:
chmod 644 sample.php – set the file permission to 644
chmod 755 config/ – set the directory permission to 755
find . -type f -exec chmod 0644 {} \; – You can set all file permission to 644 within a directory by running this command.
find . -type d -exec chmod 0755 {} \; – You can set all directory permission to 755 within a directory by running this command.
chown user:user sample.php – This command can be used to change the ownership of a file with group and user permission.
chown group:user public/ – Run this command to change the ownership of a directory with group and user permission.

Decompress files:
tar -xzf archivename.tar.gz – This command unarchives the tarball files.
gunzip compressed.gz – This command unzips the compressed files. The “unzip” utility might not be available on the server. You can install the utility by running the following comman in the Debian type of systems sudo apt-get install unzip.

MySQL optimization and dump commands:
mysqlcheck -hhostname -uusername -ppassword --auto-repair -o database_name – This command is used to optimize the tables of a database. The password can be entered separately while running this command by not providing the -p attribute.
mysqldump -hhostname -uusername -ppassword database_name | gzip > database_name.sql.gz – This command dumps a database export in a compressed SQL format. The password can be entered separately while running this command by not providing the -p attribute.
gunzip database_name.sql.gz and then mysql -hhostname -uusername -ppassword another_database_name < database_name.sql – import an SQL file to a database. The password can be entered separately while running this command by not providing the -p attribute.

Below are some links to the articles describing useful SSH commands to setup RSYNC and GIT mechanism.

Setup RSYNC between tow servers:
Please follow the instructions in this link – How to setup rsync between two servers

Setup GIT repository system:
Please follow the instructions in this link – GIT repository with version control system

Leave a Reply

Your email address will not be published. Required fields are marked *