-
first command:
sudo whoami
-
Create a file named "testfile":
touch testfile
-
make a directory named "testdir":
mkdir testdir
-
list or show all files and folders in a dir: ls
- For showing hidden files also run this command with a flag:
ls -a
- For showing hidden files also run this command with a flag:
-
change directory:
cd
-
back on directory:
cd ..
-
go to the root directory:
cd ~
-
parent working directory:
pwd
-
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
- mv is also used to rename files:
-
Make a copy of "testfile" with name "testfile01":
cp testfile testfile01
-
Make a copy of "testfile" to a directory named "newdir" (in same dir):
cp testfile newdir/
-
Remove a file named "testfile02":
rm testfile02
-
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)
-
Remove a directory named "testdir" :
rm -r -f testdir
-
Writing in a file by output redirection in file name "testfile":
echo "Hello world!" >> testfile
-
Show all contents in a file name "testfile":
cat testfile
-
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.
-
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
-
-
Check used space in megabytes:
df -m
- Check disk space in MB/GB:
df -h
- Check disk space in MB/GB:
-
Check free memory(RAM):
free -m
- Check free memory(RAM) in MB/GB:
free -h
- Check free memory(RAM) in MB/GB:
-
Restart apache2:
sudo /etc/init.d/apache2 restart
-
See who is logged on:
w
-
See log of all user login:
last
-
See log of a specific user login (like as root):
last root
-
See history of commands:
history
*To clear history:history -c
-
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/ -
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/
-
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/
- Copy all files and folders from one dir to another:
-
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/
- You may need to use "sudo". Also, if face – bash: unzip: command not found error message, then run:
-
Zip all files in current directory:
zip example2.zip *
-
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
-
Test a zip file validity:
unzip -tq picpic.zip
-
Listing all files within a zip file named "picpic.zip":
unzip -l picpic.zip
- Extract a tar.gz file:
tar xzvf latest.tar.gz
- Extract a tar.gz file:
-
Extract a certain file named "1.txt" from a .zip file named "picpic.zip" :
unzip picpic.zip 1.txt
-
Extract all files and folders in a .zip file named "picpic.zip" in a certain directory named "pictures":
unzip picpic.zip -d /pictures
-
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. -
Creating a group, assign a folder to the group and add user to the group:
- Create a folder that's going to be used by the group members:
sudo mkdir /mygroupfolder
- Create a new group:
sudo addgroup mygroup
- Change the ownership of the mygroupfolder to mygroup:
sudo chgrp mygroup /mygroupfolder/
- 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.
- You can see the folder current permission with the command:
- Create a folder that's going to be used by the group members:
-
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
- 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!:
-
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
-
-
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.
-
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 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
-
You can grant an existing user "sudo" permission or root access by adding him to group sudo. Run Command:
sudo adduser username sudo
-
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.
-
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
-
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*
-
See directory owner:
ls -l
or,ls -l /path/to/file
-
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)
-
Get Server OS (Operating System) and Kernel Version: *** Kernel info:
uname -a
*** OS info:cat /etc/*release
-
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/# -
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/
-
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 all files and folders from a folder to another folder keeping current files permissions:
sudo rsync -avP ~/wordpress/ /var/www/html/
-
Restart SSH: sudo service ssh restart CentOS (tested by 6.7):
sudo /etc/init.d/sshd restart
-
Nano specific commands:
- Go to the end of a file:
ctrl+w+v
- Go to the end of a file:
-
Setting up a cronjob using crontab: https://vexxhost.com/resources/tutorials/how-to-use-cron-jobs-for-automation-on-ubuntu-14-04/
-
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
-
Flush DNS Cache Mac OS Seira 10.12:
sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;say flushed
- 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/
- Import/Export Database and Rest MySQL Root Pass: https://www.digitalocean.com/community/tutorials/how-to-import-and-export-databases-and-reset-a-root-password-in-mysql
-
Add user to the www-data group (nginx) so that you can write via FTP/SFTP with www-data:www-data permission:
sudo usermod -a -G www-data zubaer
sudo chgrp -R www-data /var/www/html
sudo chmod -R g+w /var/www/html
Last active
January 19, 2024 14:47
-
-
Save zubaer-ahammed/7ab1bd2e1c070c242368d8861589c246 to your computer and use it in GitHub Desktop.
Frequently Needed SSH Commands ( Mostly Linux & Mac + PUTTY for windows)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment