SSH commands or Secure Shell commands is a must for any advanced or even a beginner level web developer. As you step into web development and take a step ahead, the necessity and use of ssh increase proportionally. Here, I have listed some of the most commonly used ssh commands that I use in my day to day programming life.
Hold on a moment, please: Before you go ahead, please know that I expect you to know basics of Linux SSH Secure Shell. And here I shall mostly refer to Ubuntu Linux (sometimes Mac, CentOS, Windows etc). One more crucial note is that these commands aren’t properly arranged in sections. I have just put the commands which I use in my day to day life. So, please be sincere to keep that in your mind.
Frequently Used SSH (Secure Shell) Commands:
1. first command: sudo whoami
2. Create a file named “testfile”: touch testfile
3. make a directory named “testdir”: mkdir testdir
4. list or show all files and folders in a dir: ls
* For showing hidden files also run this command with a flag: ls -a
5. change directory: cd
5. back on directory: cd ..
6. go to the root directory: cd ~
7. parent working directory: pwd
Moving, copying and renaming files:
8. move “testfile” to a directory named “testdir”: mv testfile testdir/
**** mv is also used to rename files: mv old-file-name new-file-name
Or, mv /home/vivek/docs/files/resumezzz.pdf /home/vivek/docs/files/resume.pdf
Example: sudo mv readme.html readme.html-niu
*** the same command goes for renaming a directory
9. Make a copy of “testfile” with name “testfile01”: cp testfile testfile01
10. Make a copy of “testfile” to a directory named “newdir” (in the same dir): cp testfile newdir/
11. Remove a file named “testfile02”: rm testfile02
12. Make a copy of directory “testdir” with name “testdir02”: cp -R testdir/ testdir02/
*** You can use this to copy all files and folder to another directory as it’s using -R (recursively)
13. Remove a directory named “testdir” : rm -r -f testdir
14. Writing in a file by output redirection in the file name “testfile”: echo “Hello world!” >> testfile
15. Show all contents in a file name “testfile”: cat testfile
16. Edit a file named “testfile” in a folder named “testdir” in SSH: nano testdir/testfile
* Press Ctrl + O to save a file and press Ctrl + X to exit.
Searching for a file or directory:
17. Find a file or directory:
**Use sudo if needed
*To find a filename call foo do:
find / -type f -name foo
*For a directory you can do:
find / -type d -name foo
Checking memories:
18. Check used space in megabytes: df -m
19. Check free memory(RAM): free -m
Server restart and logging history:
20. Restart apache2: sudo /etc/init.d/apache2 restart
21. See who is logged on: w
22. See log of all user login: last
23. See log of a specific user login (like as root): last root
24. See history of commands: history
*To clear history: history -c
Handling Zip Files:
25. Upload a .zip file via PSFTP: put C:\xampp\htdocs\wordpress_buddypress\wordpress-4.2.2.zip
* To get full tutorial on zipping and unzipping read: http://www.servermom.org/how-to-zip-compress-and-unzip-extract-files/65/
26. Unzip a file via SSH (main putty terminal): unzip wordpress-4.2.2.zip
* You may need to use “sudo”. Also, if face – bash: unzip: command not found error message, then run: apt-get install unzip
* To get full tutorial on zipping and unzipping read: http://www.servermom.org/how-to-zip-compress-and-unzip-extract-files/65/
27. Copy all files and folders from the wordpress-4.2.2 directory after unzipping to the root directory: cp -rf wordpress-4.2.2/* .
Copy all files and folders from one dir to another: $ cp -r myfolder/* destination folder
***** Here -R or -r stands for recursively, i.e, containing it’s sub folders for files
Example: sudo cp -r pydio-core-6.0.8/* /var/www/pydio/
28. Zip or compress files: zip example.zip 1.txt 2.txt 3.txt
* You may need to use “sudo”. Also, if face – bash: unzip: command not found error message, then run: apt-get install zip
* To get full tutorial on zipping and unzipping read: http://www.servermom.org/how-to-zip-compress-and-unzip-extract-files/65/
29. Zip all files in current directory: zip example2.zip *
30. Zip an entire directory and files with name “picpic.zip” including all subdirectories in it: zip -r picpic *
***Zip a specific directory named foldername with name “compressed_filename.zip” zip -r compressed_filename.zip foldername
31. Test a zip file validity: unzip -tq picpic.zip
32. Listing all files within a zip file named “picpic.zip”: unzip -l picpic.zip
*** Extract a tar.gz file: tar xzvf latest.tar.gz
33. Extract a certain file named “1.txt” from a .zip file named “picpic.zip” : unzip picpic.zip 1.txt
34. Extract all files and folders in a .zip file named “picpic.zip” in a certain directory named “pictures”: unzip picpic.zip -d /pictures
Creating New Root User, Group and Assigning Folders:
Create a new user:
35. Create a new user with admin/root access: sudo adduser zubaer
*This command is for Debian and Debian based distributions like ubuntu.
*Then you will give new passwords for new users.
Assign user to a group:
36. Creating a group, assign a folder to the group and add user to the group:
i) Create a folder that’s going to be used by the group members: sudo mkdir /mygroupfolder
ii) Create a new group: sudo addgroup mygroup
iii) Change the ownership of the mygroupfolder to mygroup: sudo chgrp mygroup /mygroupfolder/
iv) Use changemode (chmod) command to allow group members to write to the folder “mygroupfolder”:
* You can see the folder current permission with the command: ls -l -d /mygroupfolder/
* You can see three set of characters. The first set of characters says the owners permission, the second set says the members permission and third says everyone else’s permission. You can see drwxr-xr-x which represents 751 (from left to right).
* The common permissions are 0 for no access, 4 for read-only, 5 for read & execute, 6 for read & write and 7 for full permission.
* You can see the current permission of “mygroupfolder” is 751 which means group members can only read & execute. We are going to give them full access. So, we are going to change the group permission to 771.
* Change group permission to 771: sudo chmod 771 /mygroupfolder/
* Add users to the the group (here -a is for appending the group to the current list of group of the user): sudo usermod -a -G mygroup zubaer
* Exit your current shell and log back in.
* Now if you run the command: id , you will be able the see “mygroup” in your group list. Now you can create and write any files in the mygroupfolder without using “sudo” command.
37. Delete/Remove a group:
* To delete or remove a group we need to make sure that there are no members in the group. To remove users we are going to use usermodify (usermod) command again without a flag (-a). Notice: you must include the group you want to be part of or, you will loose all other groups also!: sudo usermod -G zubaer,comicbuzz_ftp,sudo zubaer
*previously I was member of four groups zubaer,comicbuzz_ftp,sudo and mygroup. So, in the last command I have kept only three groups zubaer,comicbuzz_ftp,sudo except mygroup.
* Then logout and log back in. Then run delgroup command: sudo delgroup mygroup
Give user root access:
38. Give a user root access:
* Command: sudo visudo
* After running the command above you will be taken to a text editor session with the file that contains sudo privileges pre-loaded. We need to add our user to this file to grant our desired access rights.
* Find the part of the file that is labeled “User privilege specification”. It should look something like this:
# User privilege specification
root ALL=(ALL:ALL) ALL
* Add a new line below it replacing root with the desired username:
# User privilege specification
root ALL=(ALL:ALL) ALL
zubaer ALL=(ALL:ALL) ALL
* Now save the file with Ctrl+X and then type “Y” and Press enter.
* To know more visit: https://www.digitalocean.com/community/tutorials/how-to-add-delete-and-grant-sudo-privileges-to-users-on-a-debian-vps
39. Delete a user: sudo deluser –remove-home username
* The –remove-home option will delete the user’s home directory as well.
* And If you are logged in as root, you do not need to add the sudo before the command.
40. To select a word in putty just double click on it. And to select an entire line make a triple click and it will be copied in the clipboard. To paste it press Ctrl+Right Click and then paste.
Switch to another user within the terminal:
41. Switch to another user without logging out first is to use the su (substitute user) command. This command stands for substitute user and it allows you to enter the user you would like to change to. You can use it like this: su – newuser
41. You can grant an existing user “sudo” permission or root access by adding him to group sudo. Run Command: sudo adduser username sudo
42. Remove user “sudo” permission or root access removing him from the group sudo. Run Command: sudo deluser username sudo
* This won’t delete the user entirely. Rather it will remove him from group sudo. You can remove a user’s root permission by removing him from the admin group this way.
43. See your uid (unique identification number), gid (Group identification number), groups etc by running id command: id
* You can also see other user’s uid, gid, groups etc by running: id username
Other Useful Utility Commands:
44. See file size:
A single file: du -h /var/lib/mysql/ib_logfile0
All files in a folder: du -h /var/lib/mysql/
or, du -h /var/lib/mysql/ib_logfile*
45. See directory owner:
ls -l
or, ls -l /path/to/file
46. Get php.ini location: php -i | grep “Loaded Configuration File”
* After modifying or editing php.ini restart php5-fpm and the server
* To restart php5-fpm run: sudo service php5-fpm restart
* And then restart the server based on what server you are using (apache, nginx etc)
47. Get Server OS (Operating System) and Kernel Version:
*** Kernel info: uname -a
*** OS info: cat /etc/*release
Clear RAM:
48. Clear RAM or Flush Memory cache in linus server: sync; echo 3 > /proc/sys/vm/drop_caches
See more: http://tecadmin.net/flush-memory-cache-on-linux-server/#
Optimize MySQL with MySQLTuner:
49. MySQLTuner: optimize mysql by editing my.cnf file according to the recommendations.
See Details here: http://www.faqforge.com/linux/optimize-mysql-performance-with-mysqltuner/
50. Create symbolic link to phpmyadmin in nginx:
Your new server-block (virtualhost) folder is under var/www/example.com/html, you go to this directory and make the symlink: cd /var/www/example.com/html && sudo ln -s /usr/share/phpmyadmin phpmyadmin
Now you can access phpmyadmin at example.com/phpmyadmin
Copy file and folder without altering permissions:
51. Copy all files and folders from a folder to another folder keeping current files permissions: sudo rsync -avP ~/wordpress/ /var/www/html/
*** Important links:
*** Know about file permissions (chmod): http://www.computerhope.com/unix/uchmod.htm
*** Know about file ownership (chown): http://www.cyberciti.biz/faq/how-to-use-chmod-and-chown-command/
*** https://www.digitalocean.com/community/tutorials/how-to-import-and-export-databases-and-reset-a-root-password-in-mysql
52. Restart SSH:
sudo service ssh restart
CentOS (tested by 6.7): sudo /etc/init.d/sshd restart
53. Nano specific commands:
* Go to the end of a file: ctrl+w+v
Leave a Reply