Skip to main content

Basic Linux Commands


Here are explained some basic linux commands and command line syntax.
To run some of the commands presented here, You may be logged in as root user. Root is the highest and most privileged user account on Linux system. 



    Navigating through directory structure
Navigating throught filesystem diretory structure is easy; You can do this with "cd" command. With "pwd" You can check Your current position, and "ls" lists the current directory ("-l" switch provides You long format and extra info explained below):

LinuxBox#pwd /root LinuxBox# LinuxBox#cd /home LinuxBox#ls user john user2 barney LinuxBox# LinuxBox#ls -l -rw------- 1 bshotts bshotts 576 Apr 9 2011 my_file.txt drwxr-xr-x 6 bshotts bshotts 1024 Oct 14 2011 www -rw-rw-r-- 1 bshotts bshotts 276480 Oct 12 21:44 web_site.tar -rw------- 1 bshotts bshotts 5743 Feb 19 2011 Phonebook.txt ---------- ------- ------- -------- ------------ ------------- | | | | | | | | | | | File Name | | | | | | | | | +--- Modification Time | | | | | | | +------------- Size (in bytes) | | | | | +----------------------- Group | | | +-------------------------------- Owner | +---------------------------------------------- File Permissions LinuxBox#
( If want to go up one folder in a directory structure simple type "cd ..", and if wondering how to access You user home folder, You can do so with "cd ~" command.) 

    Making new directory
Make a new directory with "mkdir", and removing it with "rmdir"

LinuxBox#mkdir my_dir LinuxBox#ls user john user2 barney my_dir LinuxBox#rmdir my_dir LinuxBox#ls user john user2 barney LinuxBox#


    Copying, moving and deleting files
Copy file with "cp" to another location. Move it with "mv", and delete a file with "rm" command: 

LinuxBox#cp my_file.txt /home/ LinuxBox#cd /home LinuxBox#ls user john user2 barney my_file.txt LinuxBox#rm my_file.txt LinuxBox#ls user john user2 barney LinuxBox#


    Creating a shortcut
Creating a shortcut also called symbolic link or symlink on linux is done with ln. Syntax is simple; ln -s target_file name_of_shortcut:

LinuxBox#ln -s test_script.sh /home/testko/test_link LinuxBox#cd /home/testko LinuxBox#ls -l total 8 lrwxrwxrwx 1 root root 14 Apr 25 19:29 test_link -> test_script.sh -rwx------ 1 root root 827 Apr 17 15:53 test_script.sh LinuxBox#
( With "ls -l" we can see that our symbolic link is created and pointing with an arrow to a file test_script.sh )


    Displaying file content
Content can be displayed with "cat" command, "head" displays only the beggining, and "tail" command displays only the end of the file: 

LinuxBox#cat Phonebook.txt 1 John 091 654 84756 2 Barney 091 786 69786 3 Drago 091 234 5676 4 Sara 091 564 4985 5 John 091 654 84756 6 Barney 091 786 69786 7 Drago 091 234 5676 8 Sara 091 564 4985 9 John 091 654 84756 10 Barney 091 786 69786 11 Drago 091 234 5676 12 Sara 091 564 4985 13 John 091 654 84756 14 Barney 091 786 69786 15 Drago 091 234 5676 16 Sara 091 564 4985 LinuxBox#head Phonebook.txt 1 John 091 654 84756 2 Barney 091 786 69786 3 Drago 091 234 5676 4 Sara 091 564 4985 LinuxBox#tail Phonebook.txt 13 John 091 654 84756 14 Barney 091 786 69786 15 Drago 091 234 5676 16 Sara 091 564 4985 LinuxBox#


    Displaying disk usage
Display disk usage of each mountpoint on the system in human readable form (switch "-h"): 

LinuxBox#df -h Filesystem Size Used Avail Use% Mounted on /dev/hda2 78G 7.6G 71G 89% / tmpfs 252M 0 252M 0% /lib/init/rw tmpfs 252M 0 252M 0% /dev/shm /dev/hda1 464M 37M 403M 9% /boot /dev/hda3 8.3G 429M 7.5G 6% /var nfs6:/home 520G 461G 60G 89% /home mirrors:/mirrors 1.4T 1.1T 233G 83% /mirrors /dev/mapper/big-phat 77G 36G 42G 46% /space mailstore:/var/mail 13.9G 8.8G 7.7G 58% /var/mail LinuxBox#fdisk -l Disk /dev/sda: 82.3 GB, 82347195904 bytes 255 heads, 63 sectors/track, 10011 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0009f41c
( If want to see a total space of a hard drive, we can use "fdisk -l" )

    Check memory usage
Display the status of total and used system memory: 

LinuxBox#free -m total used free shared buffers cached Mem: 750 625 125 0 35 335 -/+ buffers/cache: 254 496 Swap: 956 0 956 LinuxBox#


    Display running processes
Displaying processes on Linux can be done with "ps" command. Switch "-e" gives You all running processes with process id or pid. To display real-time view of a running processes use "top". To quit from top press q, forhelp press h. To find a process called apache for example, we can use "pgrep": 

LinuxBox#pgrep apache 3457 LinuxBox#
(To see all processes running by user1 use "ps -u user1" command) 

If want to stop or kill specific process immediately, use "kill" with process id or pid

LinuxBox#kill 3457 LinuxBox#kill -9 3457
(To kill process and all its child processes, run kill with switch -9, "kill -9 pid") 

    System information
Linux System inromation can be displayed with uname command: 

LinuxBox#uname -a Linux LinuxBox 2.6.32-5-686 #1 SMP Mon Oct 3 04:15:24 UTC 2011 i686 GNU/Linux LinuxBox#uname -r 2.6.32-5-686
(If want to display Your Linux kernel version, use "uname -r" ) 


Changing file or folder permissions and ownerships
As already explained in the beginning, permissions of folders and files can be seen with "ls -l" command. For every file or folder, we can set readwrite or execute permissions for owner (o)group (g) and all others, world (w). In the following example, we can see that owner (O) of the my_file.txt is user rootgroup (G) is set also toroot. Also we can see that the owner of the file is allowed to read(r) and write(w). Further more, we see thatgroup(g) and world(w) have only read (r) permissions: 

LinuxBox#ls -l total 0 -rw-r--r-- 1 root root 0 Apr 24 17:38 my_file.txt | | | | | | | | | | o g w O G LinuxBox#

To change file permissions, use chmod command. For this we use numbers 4 - read, 2 - write, 1 - execute. For example if we want to set readwrite and execute permissions for the owner, we simply sum those numbers: 4+2+1=7. We do the same for the group and world. Let say we want to set only read and execute permissions, we use number 5: 4+1=5. So we get a permission number 755: 

LinuxBox#chmod 755 my_file.txt LinuxBox#ls -l total 0 -rwxr-xr-x 1 root root 0 Apr 24 17:38 my_file.txt | | | | | | | | | | o g w O G LinuxBox#

To change the owner or owner group of the file or folder use "chown owner:owner_group" command. Let's say we want to change owner and group of my_file.txt from root to testko: 
LinuxBox#ls -l total 0 -rwxr-xr-x 1 root root 0 Apr 24 17:38 my_file.txt LinuxBox#chown testko:testko my_file.txt LinuxBox#ls -l total 0 -rwxr-xr-x 1 testko testko 0 Apr 24 17:38 my_file.txt LinuxBox#


    Creating, modifying or deleting users and groups on Linux
Creating of user on Linux is done with "useradd" command. For this, we can use command "useradd -m -s /path_to_users_shell username". Using -m switch will create user folder for us automatically, and with -s we can define a shell we intended for the user. Once the user account is created, we can setup password for the user with "passwd username". We can check that user account is created by listing /home directory and displaying a newly created home folder of the user: 

LinuxBox#useradd -m -s /bin/bash testis LinuxBox#passwd testis Enter new UNIX password: ****** Retype new UNIX password: ****** passwd: password updated successfully LinuxBox#ls /home testis user1 user2 user3 LinuxBox#

If want to get rid of the account, deleting user with his home folder is done with userdel

LinuxBox#userdel -r testis LinuxBox#ls /home user1 user2 user3 LinuxBox#

To modify user, for example to change the primary group that user "testko" belongs to, use usermod

LinuxBox#usermod -g client_group testko LinuxBox#groups testko testko : client_group LinuxBox#
(To see to which group user belongs, enter "groups user") 


    Mounting filesystems and partitions
To be able to access your data in different physical storage devices, like floppies, CD-ROMs, hard drives or different partitions with different filesystems, they firstly have to be attached to the system before they can be accessed. The attaching is called mounting, and the path where they are mounted is called mount pointMount point must be already existing directory. 

Example of mounting a floppy drive to /mnt/floppy: 

LinuxBox#mount /dev/fd0 /mnt/floppy LinuxBox#

To Mount a network shared folder we can use adittional switches like -t to define filesystem type, or -o to define options like username and password which have right to access shared folder. Finally we can enter destination of network shared folder on a PC_server and local mount point on linux where we want to mount the share: 

LinuxBox#mount -t cifs -o username=user1,password=passwd1234 //PC_server/C$ /mnt/C LinuxBox#

Unmounting is done with the "umount" command, follows the name of the device or mount point: 

LinuxBox#umount /mnt/C LinuxBox#

Comments

Popular posts from this blog

Cisco three-layer hierarchical model

Because networks can be extremely complicated, with multiple protocols and diverse technologies, Cisco has developed a layered hierarchical model for designing a reliable network infrastructure. This three-layer model helps you design, implement, and maintain a scalable

Access Control Lists (extended)

Access Control List  or  ACL  is a technic of controling network traffic. It is a list of rules with which traffic flow can be manipulated - permitted or denied. By default, traffic flow from the interface with the higher security level (for example "inside" interface) to interface with the lower security level (example "outside" interface) is allowed by default, but the flow of traffic from "outside" to the "inside" interface must be explicitly permited. 

Classes of IP addresses

TCP/IP defines five classes of IP addresses: class A, B, C, D, and E. Each class has a range of valid IP addresses. The value of the first octet determines the class. IP addresses