Category: Linux Toggle Comment Threads | Keyboard Shortcuts

Over 80% is running on some form of Linux – so does your Mac and you iPhone. Sometimes we have to get our hands dirty on the command line – it makes you feel like a proper hacker.

Here are some pointers I picked up on my journey.

  • Jay Versluis 12:08 pm on April 8, 2014 Permalink | Reply
    Tags: ,   

    Categories: Linux ( 40 )

    How to use FTP from the Linux Command Line 

    folder_downloadsYou can use the ftp command to talk to an FTP server from the Linux Command Line. Type ftp to see if the tool is installed. If you get a “command not found” message then go ahead and type yum install ftp to make it available on your system.

    Using it is very straightforward – but I keep forgetting how because I only do it once in a blue moon. So here’s a handy cheat sheet:

    Logging in to your FTP Server

    Assuming our site is example.com, simply type this:

    ftp example.com
    
    Connected to example (12.34.56.78).
    220 FTP-Example
    Name:

    This will connect you, but the system wants to know the username and password at the prompt. Provide those and if your login was successful you’ll see something like this:

    230 User tester logged in
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp>

    Note that you’re now at the FTP command line and no longer on the Linux command line (you can tell by the ftp> in front of the cursor). Therefore only FTP commands are now accepted, until you type “exit” or “bye” to go back to Linux.

    To see a list of available commands type help and you’ll see a list much like this:

    Commands may be abbreviated.  Commands are:
    
    !		debug		mdir		sendport	site
    $		dir		mget		put		size
    account		disconnect	mkdir		pwd		status
    append		exit		mls		quit		struct
    ascii		form		mode		quote		system
    bell		get		modtime		recv		sunique
    binary		glob		mput		reget		tenex
    bye		hash		newer		rstatus		tick
    case		help		nmap		rhelp		trace
    cd		idle		nlist		rename		type
    cdup		image		ntrans		reset		user
    chmod		lcd		open		restart		umask
    close		ls		prompt		rmdir		verbose
    cr		macdef		passive		runique		?
    delete		mdelete		proxy		send

    No need to panic: The good news is that we don’t really use a plethora of new commands, and some (like ls and mkdir) are working the same way, just the output may look a bit different.

    Let’s go through a few common scenarios now: listing and creating directories, uploading, downloading, and deleting files. Classic CRUD – FTP Style.

    If you ever need to come out of a running command, CMD-D (or CTRL-D) will do the trick.

    Listing and Switching Directories

    Your usual Linux favourites will work fine to list and switch directories:

    - ls (list directory, same as dir)
    - cd (change into directory, for example “cd mydir”)
    - cd .. (move one directory up in the tree)

    Excellent: nothing new to learn here. Result!

    Creating and Deleting Directories

    Another nice thing is that mkdir is still working to create a directory. Here’s how we create a directory called test:

    mkdir test
    
    257 "/test" - Directory successfully created

    Likewise, rmdir does a good job at deleting (empty) directories:

    rmdir test
    
    250 RMD command successful

    To delete a directory that contains files you must first remove all files (see below under Deleting Files) and then use this command.

    Downloading Files

    To download a single file we can use the get command (or recv if you can remember it better). You must type out the entire file name for this to work, and you won’t get a progress report while your file downloads:

    get testfile.tar
    
    local: testfile.tar remote: testfile.tar
    227 Entering Passive Mode (81,169,163,229,179,131)
    150 Opening BINARY mode data connection for testfile.tar (86365356 bytes)
    226 Transfer complete
    86365356 bytes received in 11 secs (7865.17 Kbytes/sec)

    This will save testfile.tar in the Linux directory that you were before you initiated the FTP session.

    To save files in a directory other than the current one, I’m afraid you’re going to have to log out, cd into the directory you want those files to go, then re-connect. I know, ultra lame – but if there is another way then it’s kept so secret that no Google search will ever unveil it.

    Sadly wildcards are no working in this operation, so you’ll always have to type out the exact file name. Lucky for us you CAN use wildcards to download multiple files with mget, like this:

    mget test*

    Now all files starting with “test” are downloaded and you’ll be prompted one by one. This will work for single files too and saves you having to type out cryptic long names. Human 1 – FTP 0. Ha!

    Uploading Files

    put and mput work just like get, but they upload local files to the current FTP directory. You can specify a local Linux path when doing this, but put and mput expect a local path to also exist on the FTP remote (and fail if they don’t). Read: messy. There probably is a way to deal with this, but life’s just too short.

    Just like get, put also needs the whole file name and cannot deal with wildcards – but mput does:

    mput test*
    
    mput testfile.tar? y
    227 Entering Passive Mode (81,169,163,229,218,225)
    150 Opening BINARY mode data connection for testfile.tar
    226 Transfer complete
    236716 bytes sent in 0.0141 secs (16825.36 Kbytes/sec)

    Deleting Files

    There’s also a delete and mdelete command which – you guessed it – removes unwanted files from the server. Same as before: no wildcards on delete, but they work fine on mdelete:

    mdelete test*
    
    mdelete testfile.tar? y
    250 DELE command successful

    Alternatives

    FTP transfers all files and passwords “in the clear” and does not work with encryption. Checkout the sftp command which will do all of this and more while using encryption on all transfers.

    Note that there is a difference between SFTP and FTPS: the latter (FTPS) is the same as FTP but with encryption added to it. SFTP isn’t really FTP at all, it’s an SSH connection that works much like rsync and scp, and uses similar syntax.

    Further Reading

     
  • Jay Versluis 7:12 pm on March 26, 2014 Permalink | Reply  
    Categories: Linux, MySQL, Plesk ( 40 )

    How to log into MySQL as root user in Plesk 

    Plesk-LogoYou may have noticed that there is no MySQL root user on servers running Plesk. That’s because Plesk renames this user into “admin” by default – for security reasons.

    The password for the admin MySQL account is the same as for the Plesk Panel admin account.

    Even so, when you try to login to MySQL – remotely or locally – you may be puzzled to find that your admin password doesn’t seem to work. Let me assure you of your sanity and your keyboard skills: it’s because Plesk encrypts the password in the database.

    It is the encrypted version that you must present to MySQL, not the clear version. For example, if your password was indeed “password”, then the following command will not grant you access to MySQL:

    mysql -u admin -ppassword

    You can check your unencrypted password by issuing the following command (on Linux servers):

    /usr/local/psa/bin/admin --show-password

    In our example, it will indeed show “password” – so why doesn’t it work? It’s because that command will unencrypted the password for us. MySQL however needs the encrypted version. Here’s how we can extract this from Plesk:

    cat /etc/psa/.psa.shadow
    
    // will show you something like
    $AES-128-CBC$w78TYgIfzDsKjOvEqkg/nQ==$O4xPUtsQe1TI3P601wQgYw==

    This will give you a weird looking output as shown above. Believe it or not, that’s your MySQL admin password!

    If you’re already logged into your server as root and want to issue a MySQL shell command, you can login to MySQL like so:

    mysql -uadmin -p`cat /etc/psa/.psa.shadow`
    
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4231837
    Server version: 5.5.36-cll-lve MySQL Community Server (GPL) by Atomicorp
    
    Copyright (c) 2000, 2014, 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.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>

    If you’re attempting a remote connection to MySQL then simply paste that cryptic looking password you got in the earlier step.

     
  • Jay Versluis 2:52 pm on March 10, 2014 Permalink | Reply
    Tags: , postfix   

    Categories: Linux ( 40 )

    The Postfix Cheat Sheet 

    mail-iconI recently had some trouble with my postfix mail service. Not knowing where to being looking for log files and restart commands, I thought this quick cheat sheet would come in handy in the future:

    Starting and Stopping

    On CentOS we can speak to postfix like this:

    postfix start
    postfix stop
    postfix reload

    The this command re-reads the configuration files. Note that there is no restart command – you have to stop and then start the service again manually.

    Log Files

    On CentOS 6.5 I could find the log files in /var/log/maillog (that’s a file not a directory). Other installations may have it in /var/log/mail. Refer to your own /etc/syslog.conf file for the exact location.

    To display log events life we can make use of tail -f

    tail -f /var/log/maillog

    Alternatively we can come through the entire log and display the tricky bits:

    egrep '(warning|error|fatal|panic):' /var/log/maillog | more

    Note that log files are rotated – what you’re looking for may be hiding in other log files (such as maillog-201xxxxx).

    Postfix in Plesk

    If you’re using Plesk then you can see the individual components of the service under Tools and Settings (or Server if you’re using Power User View) – Services Management. You can start/stop/restart each aspect from here.

    Sometimes the SMTP service appears to be offline even though it’s running. This can happen on CentOS. It’s nothing to worry about – simply restart the service from the command line as explained above and it should show up fine in Plesk.

    Screen Shot 2014-03-10 at 14.51.03

    Further Reading

    If you have any further ideas, please let me know in a comment below.

     
  • Jay Versluis 10:20 am on March 8, 2014 Permalink | Reply
    Tags: , lvextend   

    Categories: Linux ( 40 )

    How to extend instance storage on Amazon EC2 

    AWS LogoI’ve just launched an EC2 instance from my own AMI. This time however I wanted it to have more storage so I increased the size of my disk space from 10 to 100GB.

    Once the instance had launched I’ve noticed that – as before – only 10GB was actually available.

    I’ve had this with other infrastructure before, and I knew that I had to extend the volume as well as the volume and the file system for the OS to recognise the extra space.

    df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/xvde1            9.9G  2.6G  6.9G  27% /
    tmpfs                 1.8G     0  1.8G   0% /dev/shm
    

    What puzzled me was the following error message when using the lvextend command:

    lvextend -L +97G /dev/xvde
    
    Path required for Logical Volume "xvde"
    Please provide a volume group name
    Run `lvextend --help' for more information.
    

    Was I not using the command correctly? Had I miscalculated the space I had in addition? I checked again with fdisk -l:

    fdisk -l
    
    Disk /dev/xvde1: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x00000000
    
    
    Disk /dev/xvdj: 5368 MB, 5368709120 bytes
    255 heads, 63 sectors/track, 652 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: 0x00000000
    

    Nope, I was right – but lvextend wasn’t happy.

    The Solution

    Amazon had already extended the volume – all I had to do was grow the file system:

    resize2fs /dev/xvde1
    
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/xvde1 is mounted on /; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 7
    Performing an on-line resize of /dev/xvde1 to 26214400 (4k) blocks.
    The filesystem on /dev/xvde1 is now 26214400 blocks long.
    

    Let’s check it out:

    df -h
    
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/xvde1             99G  2.6G   91G   3% /
    tmpfs                 1.8G     0  1.8G   0% /dev/shm
    

    Sometimes things are actually easier than we think ;-)

     
  • Jay Versluis 11:05 pm on March 7, 2014 Permalink | Reply
    Tags:   

    Categories: Linux ( 40 )

    How to use Bind Mounting in Linux 

    Tux Linux IconBind Mounting allows us to make one directory accessible from more than one location. It’s like “intercepting” directory1 and making the system redirect requests to directory2.

    Let’s take a look how to set this up and why we may want to use this technique.

    Quick Refresher: Device Mounting

    Perhaps you’re familiar with mounting devices to the filesystem in Linux. Say you had a new USB stick or other volume available and you want to make this accessible to your filesystem. Here’s how you’d do that:

    mount /dev/sda1 /media
    

    Whatever was located in /media is no longer visible – instead you’ll see the contents of the mounted device in its place. The content in /media is not gone though, it’s just “invisible” to the filesystem. To unmount the device we’ll use

    umount /dev/sda1
    

    and all previous content in /media is visible again.

    Enter Bind Mounting

    Bind Mounting behaves in much the same way, but it’s used for existing directories rather than devices. Consider two directories: /media and /pictures. If you’d like to see the content of /pictures inside /media you can bind mount those directories:

    mount --bind /pictures /media
    

    After the –bind switch, enter the directory to be mounted, followed by the mount point where you’d like to see it. Again the original contents of /media is invisible again and is replaced by what’s in /pictures. Notice that /pictures is still accessible as it was before – so you can read and write via two different access points – even though the content will only be written into one place (i.e. /pictures).

    Why do I need this?

    This technique can come in handy if you have a script you cannot change, and it’s insisting on writing data into a place you don’t like. Think of partitions that are too small. With bind mounting you can mount a directory from a larger partition to the directory on the smaller partition, and therefore magically create space where there clearly isn’t any.

    How do we make this permanent?

    The mount command does not survive a server restart and you’ll have to set it up when you reboot your system. You can make add a mount automatically upon start by editing /etc/fstab and add your instruction in this way:

    /pictures /media none bind
    
     
  • Jay Versluis 5:40 pm on February 12, 2014 Permalink | Reply
    Tags: ,   

    Categories: Linux, Podcast ( 40 )

    How to SSH into a Virtual Machine in Parallels Desktop 

    Parallels-DesktopI was pulling my hair out the other day trying to connect to a VM in Parallels Desktop. It’s fairly straightforward, yet rather complex at the same time. Here’s how to do it step by step.

    In this example I’m running Parallels Desktop 8 under Mac OS X 10.9 Mavericks, and my Guest OS CentOS 6.5.

    Configuring your VM

    Before starting the VM, start Parallels Desktop and head over to

    • Virtual Machine
    • Configure
    • Hardware
    • Network

    You’ll get to a screen like this:

    Screen Shot 2014-02-12 at 17.21.16

    Make sure the tick box is “Connected”. Under Type you get a drop-down menu with three options:

    • Shared Network: gives your VM access to your internet/network connection, much like a “split”
    • Bridged Network (with several options): creates a new IP address for your VM and attaches it to whichever device you choose. Stick to the “Default Adapter” here unless you know what you’re doing. This will let other devices on your network connect to the VM.
    • Host-Only Network: also creates a new IP address for your VM, but only lets your Mac connect to the instance. Other devices on the network won’t be able to connect (more secure).

    Pick Bridged (Default Adapter) or Host-Only, then start your VM.

    Read the full article

     
  • Jay Versluis 7:18 am on February 8, 2014 Permalink | Reply
    Tags:   

    Categories: Linux ( 40 )

    How to run PHP from the command line in Linux 

    php-med-transDid you know that you can run PHP directly from the command line? Here’s how:

    Running a command

    Let’s run a one-liner and see the output displayed in our console. We’ll use phpinfo() because we know it should generate a lot of text. We need to start our line with php, use the -r switch and specify our command in single quotes:

    php -r 'phpinfo();'
    

    That’s it. You should see a familiar long list of set variables and info about your PHP environment. Note that you have to specify the semicolon inside your brackets, just like you would at the end of any PHP command.

    You can also run several commands in a row:

    php -r 'echo "Hello "; echo "Jay";'
    

    Notice that when we use single quotes to encapsulate the command, we can use double quotes inside it.

    Running a script

    You can execute an entire php file from the command line too. Unlike other shell commands, when called with the php parameter there’s no need to change the file permissions. The file does not need to end with .php either.

    Consider the following simple script – let’s call it “test.php”. Note the use of the opening and closing php tags, just like we’d use them for web output.

    <?php
    echo "Hello Jay!";
    ?>
    

    We can run this script from our command line like this:

    php -f test.php
    

    You can also run the same script “Linux Shell Command Style” without specifying php first. You need to modify your script in two ways for this to work.

    First, we need to include the path to PHP at the beginning of the script, like we’d do with Python or BASH scripts. We do this so that our shell knows how to run the script. The UNIX shebang will look familiar:

    #!/usr/bin/php
    <?php
    echo "Hello Jay!";
    ?>
    

    We also need to change the permissions to allow execution, again like we’d do with other scripts.

    chmod +x test.php
    

    Now we can run it like this:

    ./test.php
    
     
  • Jay Versluis 1:54 pm on February 5, 2014 Permalink | Reply
    Tags:   

    Categories: Linux ( 40 )

    How to fix yum update failure blaming “qpid-cpp” 

    I’ve just tried to update one of my CentOS 6.2 servers that was built from an older AMI, only to find that the yum update command stopped unexpectedly with several dependency errors. They all pointed to something called “qpid”, and I must admit that I’ve never heard of it – nor did I know that it was installed.

    The long list ended with the following suggestion:

     You could try using --skip-broken to work around the problem
     You could try running: rpm -Va --nofiles --nodigest
    

    And why not indeed, since yum is so nice to give us suggestions. I’ve tired running “yum update –skip-broken” and got the following message:

    Packages skipped because of dependency problems:
        qpid-cpp-client-0.14-22.el6_3.x86_64 from base
        qpid-cpp-client-ssl-0.14-22.el6_3.x86_64 from base
        qpid-cpp-server-0.14-22.el6_3.x86_64 from base
        qpid-cpp-server-ssl-0.14-22.el6_3.x86_64 from base
        qpid-qmf-0.14-14.el6_3.x86_64 from base
    

    Looks like this didn’t quite work either.

    Let’s see how to fix this

     
  • Jay Versluis 1:08 pm on February 5, 2014 Permalink | Reply
    Tags:   

    Categories: Linux ( 40 )

    How to display wired clients on an Apple AirPort Time Capsule 

    Version 6.x of Apple’s AirPort Utility displays all your wireless client’s IP addresses, but it doesn’t show you wired devices. The old version 5.6 did, but without a hack it no longer runs on Mountain Lion.

    There is however a simple way to display all clients using the command line tool arp. Open up a Terminal Window (Applications – Utilities – Terminal) and type the following:

    arp -a
    
    ? (10.0.1.1) at 88:1f:a1:2c:87:37 on en1 ifscope [ethernet]
    ? (10.0.1.2) at bc:52:b7:1a:f2:9 on en1 ifscope [ethernet]
    ? (10.0.1.6) at 74:e1:b6:c0:31:f1 on en1 ifscope [ethernet]
    ? (10.0.1.18) at bc:67:78:55:37:96 on en1 ifscope [ethernet]
    ? (10.0.1.24) at 4:c:ce:dc:94:4e on en1 ifscope [ethernet]
    ? (10.0.1.25) at 88:1f:a1:2c:87:37 on en1 ifscope [ethernet]
    ? (10.0.1.29) at 0:13:77:d4:da:d0 on en1 ifscope [ethernet]
    ? (10.0.1.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
    

    It’s not pretty, but it gives you a good idea of what’s attached, and which IP addresses to try. You can also compare your wireless devices to this list and see which one is the “odd one out”.

    To display your wireless device IPs, open AirPort Utility, then option-double-click onto your AirPort model/icon and a window opens up. Each device has “friendly name” and a small triangle that will display its IP and MAC address:

    Screen Shot 2014-02-05 at 13.06.38

     
  • Jay Versluis 1:33 pm on November 17, 2013 Permalink | Reply
    Tags: ,   

    Categories: Linux ( 40 )

    How to list installed packages on CentOS 

    yum can help us here. This should do the trick:

    #yum list installed
    

    If you’re interested in a particular package, you can pipe a search string into grep and only list filtered results:

    #yum list installed | grep "yum"
    
    keyutils-libs.i686    1.4-4.el6           @anaconda-CentOS-201303020136.i386/6.4
    yum.noarch            3.2.29-40.el6.centos
    yum-metadata-parser.i686
    yum-plugin-fastestmirror.noarch
    

    The search string doesn’t even need to be in quotes, and partials work fine too.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Google+