Frequently used SSH commands and their usage

Browsing, navigating and listing

cd

Change directory to a particular directory.

cd : If the command is executed without any arguments, it will take us to the user's home directory.

cd ~ : Moves you back to your home directory (the directory you are in when you first log on to your server).

cd /usr/local/apache :  leads you to the /usr/local/apache/ directory.

cd - : Move you back to the last directory you were in.

cd .. : Moves you up one level in the directory tree.

ls

list all files/directories in the current directory.

ls -a : Shows all files/directories in the current directory, includes hidden files.

ls -l : Shows an arranged listing of the current directory.

ls -al : Shows an arranged listing of the current directory, includes hidden files.

du

Shows disk usage of current directory.

du -h : Shows disk usage, per folder, in the current directory, in human readable format.

du -sh : Does not print the per folder size, only prints total space used, in human readable format.

du -la * |sort -n|tail -15: An example of a combined command:

  • du -la * : Returns the filesize of all files in the given directory.
  • sort -n : Sorts the results by numeric value.
  • tail -15 : Prints only the last 15 results, which means the 15 biggest files in a given directory.

 

Copying, Moving and deleting

cp

Command used to copy a file.

cp /home/path/filename.ext /home/newpath/newfilename.ext : make a copy of the filename.ext from the /home/path/ directory to the /home/newpath/ directory with the name newfilename.ext.

cp -R /home/path/* /home/newpath/ : make a copy of all files and directories from /home/path/ and puts them in the /home/newpath/ directory.

mv

Command used to move a file - usage is the same as cp (see above).

rm

Command used to delete files.

rm filename.ext : Deletes filename.ext, asks for confirmation before deleting.

rm -f filename.ext : Deletes filename.ext without asking for confirmation.

rm -rf /home/directory/ : Deletes the folder directory, all files and subdirectories inside, without confirmation. (Be very careful with this command).

 

Searching, finding, viewing and editing

vi

Text editor. Usage is shown here.

vi fichier.ext : Opens filename in the vi text editor.

cat

Use to print on the screen.

cat filename : Prints the content of the file filename on screen.

cat filename |more : |more spreads the content of the file on multiple pages (scroll through pages by pressing space, quit by pressing q).

tail

Same principle as cat but only prints the end of the file. Used a lot to see the last events showing in a logfile.

tail -f /path/filename.txt : To watch the file continuously, even while it is updating.

tail -100 /path/filename.txt : To print the last 100 lines of the file.

grep

Search for a string in a file.

grep string filename : Prints on screen instance where the word string has been found in filename.

grep -r string *  : Prints on screen instances where the word string has been found in the current folder and it's subfolder's files.

find

File researches

find /path/to/search/ -name filename : Searches in /path/to/search for a file named filename.

find / -name filename : Find all the files named filename anywhere on your server.

find /path/to/search -name "*string*" : Finds all the files that have 'string' in their file name.

 

Server security, process and network monitoring

last

Shows the last several connections made to the server and when the connection was made.

last -20 : Shows only the last 20 logins.

last -20 -a : Shows only the last 20 logins, with the hostname from where the connection was made.

last -20 -ai : Same as above but shows IP address rather than the hostname.

w

Shows who is currently logged in and where they are logged in from.

who

Similar as w but shows only the user, time of connection and the hostname from where the connection was made.

netstat

Shows all current network connections.

netstat -an : Shows all connections to the server, the source and destination IPs and ports.

netstat -rn : Shows routing table for all IP addresses bound to the server.

top

Shows running processes and their ressources usage in a table, in real time.

Shift + M to sort by memory usage.

Shift + P to sort by CPU usage. (Press ctrl + c to exit).

ps

List the running processes and their PID (Process ID).

ps U username : Shows the processes run by the user username.

ps aux : Shows all running system processes.

ps aux --forest : Sorts and shows all running system processes.

kill

Used to terminate a process.

kill -9 PID : Where PID stands for the process ID number.

 

Permissions and ownership

(Note: You must either be the owner of the file/directory or be logged in as root before you can run any of these commands)

chown

Used to change the ownership of a file (Two variables: USER - GROUP)

chown root filename.ext : Changes the owner of the file filename.ext to root.

chown root.apache filename.ext : Changes the owner to root and group to apache.

chown -R root /home/folder : Changes the owner of everything (folders and files alike) inside the directory /home/folder to root.

chmod

Used to change file permissions.

chmod 755 filename.ext : Changes the file permissions for filename.ext to 755.

 

Miscellaneous

ln

Creates a link between files, directories and URLs.

ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf : Now you can edit /etc/httpd.conf rather than /usr/local/apache/conf/httpd.conf.

ln -s /home/www/path domain.com : Makes the domain.com domain point to /home/www/path directory (don't forget to do another entry with http://www.domain.com!)

touch

Creates an empty file.

touch /home/path/filename.html : create an empty file called filename.html in the directory /home/path (That can be edited later by VI, for instance).

wc

Prints the amount of words in a file.

wc -l filename.ext : Prints the amount of lines in filename.ext.

 

Archiving

tar

Used in combination with arguments to create and extract .tar.gz and .tar file.

tar -vxfz filename.tar.gz : Decompresses and extracts the files contained in filename.tar.gz

tar -vxf filename.tar : Decompresses and extracts the files contained in filename.tar

tar -vcf archive.tar directory/ : Takes all files and folders in /directory/ and archives it into archive.tar

 

Apache

httpd

httpd -v : Prints the build date and version of the Apache server.

httpd -l : Lists compiled in Apache modules.

service httpd restart : Restarts Apache web server.

 

MySQL

mysql

mysqladmin processlist : Shows active MySQL connections and queries.

mysqladmin drop databasename : Drops (deletes) the database named databasename.

mysqladmin -u root -p create databasename : Creates a database named databasename.

mysql -u username -p password databasename < databasefile.sql : Restores a MySQL database into databasename from databasefile.sql.

mysqldump -u username -p password databasename > databasefile.sql : Makes a MySQL dump of the database named databasename and outputs it to databasefile.sql.

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk