Linux VPS cheat sheet. Linux commands for CentOS/RHEL and Ubuntu/Debian

Discussion in 'Tutorials and Guides' started by MannDude, Aug 18, 2013.

  1. MannDude

    MannDude Just a dude vpsBoard Founder Moderator

    5,036
    2,634
    Mar 8, 2013
    MannDude
    Figured this thread could be a handy resource for everyone at all skill-levels. Sometimes you just want to copy and paste a command you've used a hundred times (DD-tests, for example) or are just having a brain fart and forgot how to do something. Well... this thread is for you, my friend! Let's get started!
     
    I'm just going to start off with the absolute basics, was planning I'd do several of these and then add more to this post as people respond with additional ones.
     
    By the way, the outputs of the commands I show I've done on my Raspberry Pi running Debian on my local network.
     
     
    General Commands > The Basics > All Distributions
     
    Uptime Command:


    uptime

    Shows the uptime of a VPS, output example below:


    [email protected]:~# uptime
     16:11:59 up 12 days,  8:10,  1 user,  load average: 0.08, 0.03, 0.05

    Free memory Command


    free -m

    Shows the available free memory on your Linux system. Read here for more: http://www.linuxatemyram.com/


    [email protected]:~# free -m
                 total       used       free     shared    buffers     cached
    Mem:           438        379         58          0         49        213
    -/+ buffers/cache:        117        321
    Swap:            0          0          0

    Processes and resources used command:


    top

    Realtime process and resource usage. You can see your load average, uptime, and processes that are consuming system resources. To exit 'top', just hit 'Q'. You can CTRL + M to sort the processes according to their memory usage from highest to lowest, which can come in handy in figuring out what is using up your RAM. (Thanks [member=wcypierre] for that tip)


    [email protected]:~# top

    top - 16:16:59 up 12 days,  8:15,  1 user,  load average: 0.00, 0.01, 0.05
    Tasks:  65 total,   1 running,  64 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.7 us,  0.7 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem:    448776 total,   389568 used,    59208 free,    50916 buffers
    KiB Swap:        0 total,        0 used,        0 free,   218484 cached

      PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                                                                  
    17744 root      20   0  3112 1304  924 R   1.0  0.3   0:00.27 top                                                                                                                                      
     2081 mysql     20   0  294m  47m 5320 S   0.3 10.8  75:16.16 mysqld                                                                                                                                   
        1 root      20   0  2100  676  576 S   0.0  0.2   0:37.37 init                                                                                                                                     
        2 root      20   0     0    0    0 S   0.0  0.0   0:00.10 kthreadd                                                                                                                                 
        3 root      20   0     0    0    0 S   0.0  0.0   0:07.65 ksoftirqd/0                                                                                                                              
        4 root      20   0     0    0    0 S   0.0  0.0   0:00.01 kworker/0:0                                                                                                                              
        5 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/0:0H                                                                                                                             
        6 root      20   0     0    0    0 S   0.0  0.0   2:02.08 kworker/u:0                                                                                                                              
        7 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/u:0H                                                                                                                             
        8 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 khelper                                                                                                                                  
        9 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                                                
       10 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 netns                                                                                                                                    
       12 root      20   0     0    0    0 S   0.0  0.0   0:00.25 bdi-default                                                                                                                              
       13 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kblockd                                                                                                                                  
       14 root      20   0     0    0    0 S   0.0  0.0   0:00.22 khubd                                                                                                                                    
       15 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 rpciod                                                                                                                                   
       16 root      20   0     0    0    0 S   0.0  0.0   0:00.66 khungtaskd                                                                                                                               
       17 root      20   0     0    0    0 S   0.0  0.0   0:08.23 kswapd0                                                                                                                                  
       18 root      20   0     0    0    0 S   0.0  0.0   0:00.00 fsnotify_mark                                                                                                                            
       19 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 nfsiod                                                                                                                                   
       20 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 crypto                                                                                                                                   
       27 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kthrotld                                                                                                                                 
       28 root       1 -19     0    0    0 S   0.0  0.0   0:00.00 VCHIQ-0                                                                                                                                  
       29 root       1 -19     0    0    0 S   0.0  0.0   0:00.00 VCHIQr-0                                                                                                                                 
       30 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 VCHIQs-0                                                                                                                                 
       31 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 iscsi_eh                                                                                                                                 
       32 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 dwc_otg                                                                                                                                  
       33 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 DWC Notificatio                                                                                                                          
       35 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 deferwq                                                                                                                                  
       36 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kworker/u:2                                                                                                                              
       37 root      20   0     0    0    0 S   0.0  0.0   6:29.95 mmcqd/0                                                                                                                                  
       38 root       0 -20     0    0    0 S   0.0  0.0   0:04.70 kworker/0:1H                                                                                                                             
       39 root      20   0     0    0    0 S   0.0  0.0   0:22.44 jbd2/mmcblk0p2-                                                                                                                          
       40 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 ext4-dio-unwrit                                                                                                                          
      153 root      20   0  2744  980  608 S   0.0  0.2   0:00.32 udevd                                                                                                                                    
      235 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 bcm2708_spi.0                                                                                                                            
     1371 root      20   0  4852 2280  568 S   0.0  0.5   0:00.23 dhclient                                                                                                                                 
     1574 root      20   0 28060 1576 1064 S   0.0  0.4   0:03.93 rsyslogd                                                                                                                                 
     1609 root      20   0  2248  844  672 S   0.0  0.2   0:05.02 cron                                                                                                                                     
     1717 root      20   0  1712  556  460 S   0.0  0.1   0:00.13 mysqld_safe                                                                                                                              
     1899 root      20   0  6168 1040  620 S   0.0  0.2   0:00.12 sshd                                                                                                                                     
     2082 root      20   0  1804  608  532 S   0.0  0.1   0:00.01 logger                                                                                                                                   
     2174 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2175 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2176 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2177 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2178 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2179 root      20   0  2200  708  588 S   0.0  0.2   0:00.01 getty                                                                                                                                    
     2804 root      20   0 36296 3520 1252 S   0.0  0.8   1:40.49 php5-fpm

    Check disk space used and available command:


    df -h

    This will check available space on your filesystem. Your output will likely look different depending on the configuration of your server.


    [email protected]:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    rootfs          7.3G  3.7G  3.6G  51% /
    /dev/root       7.3G  3.7G  3.6G  51% /
    devtmpfs        212M     0  212M   0% /dev
    tmpfs            44M  196K   44M   1% /run
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs            88M     0   88M   0% /run/shm


    Change Directory Command:


    cd /path/to/directory

    This will change the location of the directory your browsing. In the example above, simply change the path to the directory in which you need to change to. For example, if you needed to move to '/var/www', you would type 'cd /var/www'


    [email protected]:~# cd /var/www
    [email protected]:/var/www#

    List directory content command:


    ls

    Now that you've moved to the directory of your choice, you may want to get a list of the content from within that directory. Simply enter the 'ls' command. You should see something like:


    [email protected]:/var/www# ls
    bill_murray.jpg  check.php  damncat.jpg  index.php  logo.png  logs  phpinfo.php  phpmyadmin  test  vpsboard
    Make Directory command:


    mkdir ./mynewdirectory

    The command above will create the directory 'mynewdirectory' in whatever directory you are currently in. Maybe you want to go deeper than that, so use the following the command:


    mkdir -p ./mynewdirectory/test/testing

    The command above not only creates the directory but also all that follows it.
     
     
    Move a file command:


    mv test.txt ./testdirectory

    The above command will the file 'test.txt' to the directory 'testdirectory'. Pretty straight forward.
     
     

    Change password command:


    passwd

    This will change the password for the user you are logged in as. For example, if you are logged in as the root password it's very good practice to change your password immediately. You will be prompted to type in your password, and asked to retype it. Your password will not show in the screen. Output will be similar to below:


    [email protected]:~# passwd
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully


    Check the CPU information command:


    cat /proc/cpuinfo

    This command will tell you information about the CPU your provider uses for the VPS node you are on. Thank you for submitting this one.


    [email protected]:~$ cat /proc/cpuinfo
    processor    : 0
    vendor_id    : GenuineIntel
    cpu family    : 6
    model        : 42
    model name    : Intel(R) Xeon(R) CPU E31230 @ 3.20GHz
    stepping    : 7
    cpu MHz        : 3192.748
    cache size    : 4096 KB
    fdiv_bug    : no
    hlt_bug        : no
    f00f_bug    : no
    coma_bug    : no
    fpu        : yes
    fpu_exception    : yes
    cpuid level    : 13
    wp        : yes
    flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt aes xsave avx hypervisor lahf_lm
    bogomips    : 6385.49
    clflush size    : 64
    cache_alignment    : 64
    address sizes    : 40 bits physical, 48 bits virtual



    Check memory information command:


    cat /proc/meminfo

     Likely not as used as often as the check CPU info command above, but can come in handy. Thank you for submitting this one.


    [email protected]:~$ cat /proc/meminfo
    MemTotal: 254828 kB
    MemFree: 9012 kB
    Buffers: 45764 kB
    Cached: 140528 kB
    SwapCached: 1036 kB
    Active: 139832 kB
    Inactive: 88016 kB
    Active(anon): 19448 kB
    Inactive(anon): 22164 kB
    Active(file): 120384 kB
    Inactive(file): 65852 kB
    Unevictable: 0 kB
    Mlocked: 0 kB
    HighTotal: 0 kB
    HighFree: 0 kB
    LowTotal: 254828 kB
    LowFree: 9012 kB
    SwapTotal: 496632 kB
    SwapFree: 490712 kB
    Dirty: 216 kB
    Writeback: 0 kB
    AnonPages: 40804 kB
    Mapped: 11764 kB
    Shmem: 56 kB
    Slab: 12616 kB
    SReclaimable: 8924 kB
    SUnreclaim: 3692 kB
    KernelStack: 640 kB
    PageTables: 700 kB
    NFS_Unstable: 0 kB
    Bounce: 0 kB
    WritebackTmp: 0 kB
    CommitLimit: 624044 kB
    Committed_AS: 229688 kB
    VmallocTotal: 765952 kB
    VmallocUsed: 5628 kB
    VmallocChunk: 752772 kB
    HardwareCorrupted: 0 kB
    HugePages_Total: 0
    HugePages_Free: 0
    HugePages_Rsvd: 0
    HugePages_Surp: 0
    Hugepagesize: 4096 kB
    DirectMap4k: 12280 kB
    DirectMap4M: 249856 kB

    Stop/Start/Restart (most) services:


    service [service name] [stop/start/restart/reload/status]
    OR


    /etc/init.d/[service name] [stop/start/restart/reload/status]

    The examples below will use both methods to start/stop/status Nginx and MySQL as it's what is running on my Raspberry Pi right now.


    [email protected]:~# service nginx restart
    Restarting nginx: nginx.

    Code:
    [email protected]:~# service nginx status
    [ ok ] nginx is running.
    
    Code:
    [email protected]:~# /etc/init.d/nginx stop
    Stopping nginx: nginx.
    
    Code:
    [email protected]:~# /etc/init.d/nginx start
    Starting nginx: nginx.
    
    Code:
    [email protected]:~# /etc/init.d/mysql status
    [info] /usr/bin/mysqladmin Ver 8.42 Distrib 5.5.31, for debian-linux-gnu on armv7l
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Server version	5.5.31-0+wheezy1
    Protocol version	10
    Connection	Localhost via UNIX socket
    UNIX socket	/var/run/mysqld/mysqld.sock
    Uptime:	12 days 16 hours 14 min 34 sec
    Threads: 1 Questions: 24062 Slow queries: 0 Opens: 1532 Flush tables: 1 Open tables: 293 Queries per second avg: 0.021.
    
    Thanks [member=wcypierre] for sharing.




    Change File Permissions command:


    chmod 755 file-or-directory
    Need to change the file or folder permissions? Do it quickly via SSH! The example below shows me simply changing the permissions of the directory 'testdir' to 755. Thanks [member=wcypierre] for sharing.


    [email protected]:/var/www# chmod 755 testdir
    [email protected]:/var/www#



     
     
     
     
    Please contribute to this thread!
     
    If you wish to contribute, I'll add your responses to this original posting so all commands are in one post for easy retrieval and use. I'll link to your profile or response to give credit where it is due.
     
    Last edited by a moderator: Aug 19, 2013
    wdq likes this.
  2. Epidrive

    Epidrive New Member Verified Provider

    277
    44
    May 16, 2013
    cat /proc/cpuinfo - cpu info


    cat /proc/meminfo - mem info
     
    Last edited by a moderator: Aug 18, 2013
    MannDude likes this.
  3. wcypierre

    wcypierre New Member

    88
    21
    Aug 4, 2013
    1. at the Top command, press CTRL + M to sort the processes according to their memory usage(from highest to lowest)

    2. service <service_name> <action: start/restart/stop/reload> i.e service nginx stop

    3. chmod <permissions> <file/folder>
     
    MannDude likes this.
  4. 365Networks

    365Networks New Member

    121
    38
    May 15, 2013
    How to create a tar.gz:

    Code:
    tar -pczf name-of-archive.tar.gz /path/to/dir
     
    MannDude likes this.
  5. wcypierre

    wcypierre New Member

    88
    21
    Aug 4, 2013
    history - to show the list of commands that you've typed
     
    MannDude likes this.
  6. Chronic

    Chronic New Member

    129
    16
    May 16, 2013
    clear - clear the terminal display
     
    Last edited by a moderator: Aug 18, 2013
    MannDude likes this.
  7. H_Heisenberg

    H_Heisenberg New Member

    53
    18
    Jun 18, 2013
    cat /etc/issue

    Easy way to find out which Linux Distribution is installed on the VPS/server/machine.


    Remove/disable/add/enable services in RHEL based OSs (CentOS/SL/Fedora):


    Add/enable & disable:
    chkconfig <servicenamehere> <option>

    Options:
    off - disable
    on - enable/add

    E.g: chkconfig httpd off (will disable httpd as a server so it won't startup with the OS)


    Remove services totally:
    chkconfig --del <servicename>

    E.g: chkconfig --del httpd (will delete httpd completely from the service list)



    Copy files:


    cp <path/to/file> <path/to/destination>

    For folders with files use:
    cp -r <path/to/file> <path/to/destination>

    -r is for recursive

    There is way more that is just simply and applies for all Distros but I'm too lazy :p.

    Oh: "ifconfig" is good, too.
     
    Last edited by a moderator: Aug 18, 2013
    MannDude likes this.
  8. WebSearchingPro

    WebSearchingPro VPS Peddler Verified Provider

    493
    143
    May 15, 2013
    uname -a

    Check what linux kernel + architecture is running

    setenforce 0 | 1

    Disables and Enables SELinux till the system reboots (good for quick tests)

    restorecon -R /path/to/dir/

    Fixes issues relating to SELinux and file permissions

    setsebool -P module_name=0|1

    Enable or disable certain SELinux modules.

    semanage port -a -t http_port_t-p tcp #

    Allows using a port other than 8080|80 on a SELinux installation.

    rpm --nosignature -i


    Quickly add repo to RHEL distro.


     


    cat /etc/passwd


    cat /etc/group


    Check users and groups on the system


     


    su [username]


    Quickly switch accounts


     


    ls -l


    Check file ownership quickly.


     


    curl -i


    Check if apache/nginx/varnish is running on that particular url.
     
    Last edited by a moderator: Aug 18, 2013
    Chatahooch and MannDude like this.
  9. drmike

    drmike 100% Tier-1 Gogent

    8,573
    2,717
    May 13, 2013
    Get process that is running info:

    ps -aux | grep ______   [where ___ = whatever you are looking for]

    ex: ps -aux | grep apache

    Stopping a process that is abandoned or crashed:

    kill -9 [process #]

    That process number comes from finding the process above.
     
    Chatahooch and MannDude like this.
  10. D. Strout

    D. Strout Resident IPv6 Proponent

    1,180
    365
    Apr 17, 2013
    A few more:

    nano [filename] - opens the nano editor to edit the file. After editing, Ctrl + O saves the file, and Ctrl + X exits.

    For testing:

    dd bs=1M count=1024 if=/dev/zero of=test conv=fdatasync - copy 1GB of test stuff to the hard drive for disk speed test

    wget cachefly.cachefly.net/100mb.test - Download a 100MB test file from Cachefly's CDN for network speed tests.
     
    MannDude likes this.
  11. GIANT_CRAB

    GIANT_CRAB New Member

    525
    270
    May 21, 2013
    I can't believe you guys missed out the yum command.

    Its the most powerful command ever.

    This commands does everything magically for you.

    yum install - Install a software via yum repo

    yum update - Update all your softwares to the latest via yum repo

    yum remove - Remove softwares you do not want or not longer need via yum repo

    Btw, MannDude, its GNU/Linux, Linux is just a kernel. It wouldn't function without GNU.

    4D3Ims2.jpg
    lel
     
    Last edited by a moderator: Aug 18, 2013
    MannDude likes this.
  12. Magiobiwan

    Magiobiwan Insert Witty Statement Here Verified Provider

    374
    112
    May 15, 2013
    Don't forget htop, the BETTER top.
     
  13. drmike

    drmike 100% Tier-1 Gogent

    8,573
    2,717
    May 13, 2013
    Yes, more htop :)  Picked htop up from Crunchbang a few releases ago and now prefer it :)
     
  14. patz

    patz New Member

    13
    1
    Jun 4, 2013
    Get logged in users:


    who
    Get current user:


    who am i
    It's the who command with 2 arguments, so who is it, or who mom loves all have the same results.

    Get logged in users and what they are doing:


    w
    Get users that logged in over time, their ip, last log in time and log in duration.

    Code:
    last
     
    MannDude likes this.
  15. 365Networks

    365Networks New Member

    121
    38
    May 15, 2013
    iptables drop an IP address:

    Code:
    iptables -A INPUT -s ADDRESSHERE -j DROP
    
     
    Last edited by a moderator: Aug 19, 2013
    MannDude likes this.
  16. jhadley

    jhadley New Member Verified Provider

    151
    25
    May 18, 2013
    You forgot the all-important

    Code:
    dd
     
    MannDude likes this.
  17. MannDude

    MannDude Just a dude vpsBoard Founder Moderator

    5,036
    2,634
    Mar 8, 2013
    MannDude
    Thanks for all the contributions, will be adding them to the original post later!
     
  18. shawn_ky

    shawn_ky Member

    135
    8
    Jun 20, 2013
    MannDude likes this.
  19. acd

    acd New Member

    176
    71
    May 16, 2013
    More pgrep, please:

    pgrep processname (see manpage for more filtering options, and there are a lot of them)

    see full info for grepped processes:

    ps -fp $(pgrep -d, processname)
     
    drmike and MannDude like this.
  20. mitgib

    mitgib New Member Verified Provider

    284
    219
    May 15, 2013
    Some common commands I use that are not already listed above

    lsof - list open files

    lsof -i:port ex lsof -i:80 to find if the web server is listening 

    lsof -pPID - what does PID have open, great for finding IO abusers after finding them in iotop

    iotop - simple top-like I/O monitor

    iotop -d 3 -oPk
     
    drmike and MannDude like this.