Skip to content

Instantly share code, notes, and snippets.

@zubaer-ahammed
Last active January 19, 2024 14:47
Show Gist options
  • Save zubaer-ahammed/7ab1bd2e1c070c242368d8861589c246 to your computer and use it in GitHub Desktop.
Save zubaer-ahammed/7ab1bd2e1c070c242368d8861589c246 to your computer and use it in GitHub Desktop.
Frequently Needed SSH Commands ( Mostly Linux & Mac + PUTTY for windows)

Useful SSH 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

  6. back on directory: cd ..

  7. go to the root directory: cd ~

  8. parent working directory: pwd

  9. 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
      • same command goes for renaming a directory
  10. Make a copy of "testfile" with name "testfile01": cp testfile testfile01

  11. Make a copy of "testfile" to a directory named "newdir" (in same dir): cp testfile newdir/

  12. Remove a file named "testfile02": rm testfile02

  13. 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)
  14. Remove a directory named "testdir" : rm -r -f testdir

  15. Writing in a file by output redirection in file name "testfile": echo "Hello world!" >> testfile

  16. Show all contents in a file name "testfile": cat testfile

  17. 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.
  18. Find a file or directory:

    • Use sudo if permission isn't enough.

    • To find a filename call foo do: find / -type f -name foo

    • For a directory you can do: find / -type d -name foo

  19. Check used space in megabytes: df -m

    • Check disk space in MB/GB: df -h
  20. Check free memory(RAM): free -m

    • Check free memory(RAM) in MB/GB: free -h
  21. Restart apache2: sudo /etc/init.d/apache2 restart

  22. See who is logged on: w

  23. See log of all user login: last

  24. See log of a specific user login (like as root): last root

  25. See history of commands: history *To clear history: history -c

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

  27. 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/
    
  28. Copy all files and folders from 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/* destinationfolder
      • 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/
  29. Zip or compress files: zip example.zip 1.txt 2.txt 3.txt

  30. Zip all files in current directory: zip example2.zip *

  31. 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
  32. Test a zip file validity: unzip -tq picpic.zip

  33. Listing all files within a zip file named "picpic.zip": unzip -l picpic.zip

    • Extract a tar.gz file: tar xzvf latest.tar.gz
  34. Extract a certain file named "1.txt" from a .zip file named "picpic.zip" : unzip picpic.zip 1.txt

  35. Extract all files and folders in a .zip file named "picpic.zip" in a certain directory named "pictures": unzip picpic.zip -d /pictures

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

  37. Creating a group, assign a folder to the group and add user to the group:

    1. Create a folder that's going to be used by the group members: sudo mkdir /mygroupfolder
    2. Create a new group: sudo addgroup mygroup
    3. Change the ownership of the mygroupfolder to mygroup: sudo chgrp mygroup /mygroupfolder/
    4. 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.
  38. Delete/Remove a group:

    1. 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
    2. 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.
    3. Then logout and log back in. Then run delgroup command: sudo delgroup mygroup
  39. Give a user root access:

    1. Command: sudo visudo

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

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

    4. 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	
      
    5. Now save the file with Ctrl+X and then type "Y" and Press enter.

    6. To know more visit: https://www.digitalocean.com/community/tutorials/how-to-add-delete-and-grant-sudo-privileges-to-users-on-a-debian-vps

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

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

  43. You can grant an existing user "sudo" permission or root access by adding him to group sudo. Run Command: sudo adduser username sudo

  44. Reomve 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.
  45. 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
  46. 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*

  47. See directory owner: ls -l or, ls -l /path/to/file

  48. 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)
  49. Get Server OS (Operating System) and Kernel Version: *** Kernel info: uname -a *** OS info: cat /etc/*release

  50. 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/#

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

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

  53. Copy all files and folders from a folder to another folder keeping current files permissions: sudo rsync -avP ~/wordpress/ /var/www/html/

  54. Restart SSH: sudo service ssh restart CentOS (tested by 6.7): sudo /etc/init.d/sshd restart

  55. Nano specific commands:

    • Go to the end of a file: ctrl+w+v
  56. Setting up a cronjob using crontab: https://vexxhost.com/resources/tutorials/how-to-use-cron-jobs-for-automation-on-ubuntu-14-04/

  57. Setting up swap memory (very useful on less powerful or low memory server): https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

  58. Flush DNS Cache Mac OS Seira 10.12: sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;say flushed

    Important links:

  59. Add user to the www-data group (nginx) so that you can write via FTP/SFTP with www-data:www-data permission:

    1. sudo usermod -a -G www-data zubaer
    2. sudo chgrp -R www-data /var/www/html
    3. sudo chmod -R g+w /var/www/html

    ref: https://www.digitalocean.com/community/questions/why-doesn-t-chown-r-root-www-data-work-on-my-wordpress-installation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment