sudo: cd: command not found

If you try to use sudo to get to a directory (for instance if the user calling sudo don’t have permissions for that folder) then you will see an error message like this

user@server:~$ sudo cd /var/log/
sudo: cd: command not found

While I don’t know how to get sudo to work with cd I can offer some workarounds.
As far as I can tell the problem with sudo cd is that if it would have worked the user would be in a directory that he/she has no permissions to so nothing is gained and having a folder that the user don’t have permissions to as the working directory might cause problems in more way than one.

  • Use “sudo ls” and then continue with the next command as sudo without entering that folder; for instance
      sudo ls /var/log
    sudo cat /var/log/syslog
  • Open a shell using sudo:
     sudo -s

    this will give you a shell with su permissions

  • simulate initial login using sudo:
     sudo -i

    this will also give you su permissions but with the difference that it will be like logging in like root (home dir, profile, variables etc) [this would be equivalent of running “sudo su”]

  • Use sudo to start a new shell and do all you need to do in this shell
     sudo sh -c 'cd /var/spool/exim4/;ls'

    (don’t miss the single quotes in the command)

To give some more details on the difference between -i and -s here is a excerpt from the man pages

-i [command]
The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed. sudo attempts to change to that user’s home directory before running the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, as well as the contents of /etc/environment on Linux and AIX systems. All other environment variables are removed.
-s [command]
The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5). If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.

Linux send a log (or output from a command) as a mail from the linux console

Sometimes it is nice to send a log or some other file as a email to someone (or a part of a file)

cat myfile.txt | mail -s 'SUBJECT' example@mail.com;

Change myfile.txt, SUBJECT and example@mail.com to something useful.

It is also possible to send the output of a command to someone

ps -ef|mail -s 'SUBJET' example@mail.com;

Linux: Finding the serial number of a hard drive

Finding the serial number of a hard drive on a Linux console (as root) is simple with the right tool.
The right tool in this case is hdparm (using the -i option).
-i will query the device directly for the details.

Example:

themachine:/proc# hdparm -i /dev/sda
 
/dev/sda:
 
 Model=ST31500341AS                            , FwRev=CC1H    , SerialNo=            9VS21ZXM
 Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs RotSpdTol>.5% }
 RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=4
 BuffType=unknown, BuffSize=0kB, MaxMultSect=16, MultSect=?16?
 CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=18446744072344861488
 IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
 PIO modes:  pio0 pio1 pio2 pio3 pio4 
 DMA modes:  mdma0 mdma1 mdma2 
 UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6 
 AdvancedPM=no WriteCache=enabled
 Drive conforms to: unknown:  ATA/ATAPI-4,5,6,7
 
 * signifies the current active mode

Using the option -I instead gives even more information:

themachine:/proc# hdparm -I /dev/sda
 
/dev/sda:
 
ATA device, with non-removable media
        Model Number:       ST31500341AS                            
        Serial Number:      9VS21ZXM
        Firmware Revision:  CC1H    
        Transport:          Serial
Standards:
        Used: unknown (minor revision code 0x0029) 
        Supported: 8 7 6 5 
        Likely used: 8
Configuration:
        Logical         max     current
        cylinders       16383   16383
        heads           16      16
        sectors/track   63      63
        --
        CHS current addressable sectors:   16514064
        LBA    user addressable sectors:  268435455
        LBA48  user addressable sectors: 2930277168
        device size with M = 1024*1024:     1430799 MBytes
        device size with M = 1000*1000:     1500301 MBytes (1500 GB)
Capabilities:
        LBA, IORDY(can be disabled)
        Queue depth: 32
        Standby timer values: spec'd by Standard, no device specific minimum
        R/W multiple sector transfer: Max = 16  Current = ?
        Recommended acoustic management value: 254, current value: 0
        DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6 
             Cycle time: min=120ns recommended=120ns
        PIO: pio0 pio1 pio2 pio3 pio4 
             Cycle time: no flow control=120ns  IORDY flow control=120ns
Commands/features:
        Enabled Supported:
           *    SMART feature set
                Security Mode feature set
           *    Power Management feature set
           *    Write cache
           *    Look-ahead
           *    Host Protected Area feature set
           *    WRITE_BUFFER command
           *    READ_BUFFER command
           *    DOWNLOAD_MICROCODE
                SET_MAX security extension
           *    Automatic Acoustic Management feature set
           *    48-bit Address feature set
           *    Device Configuration Overlay feature set
           *    Mandatory FLUSH_CACHE
           *    FLUSH_CACHE_EXT
           *    SMART error logging
           *    SMART self-test
           *    General Purpose Logging feature set
           *    WRITE_{DMA|MULTIPLE}_FUA_EXT
           *    64-bit World wide name
                Write-Read-Verify feature set
           *    WRITE_UNCORRECTABLE_EXT command
           *    {READ,WRITE}_DMA_EXT_GPL commands
           *    Segmented DOWNLOAD_MICROCODE
           *    SATA-I signaling speed (1.5Gb/s)
           *    SATA-II signaling speed (3.0Gb/s)
           *    Native Command Queueing (NCQ)
           *    Phy event counters
                Device-initiated interface power management
           *    Software settings preservation
           *    SMART Command Transport (SCT) feature set
           *    SCT Long Sector Access (AC1)
           *    SCT LBA Segment Access (AC2)
           *    SCT Error Recovery Control (AC3)
           *    SCT Features Control (AC4)
           *    SCT Data Tables (AC5)
                unknown 206[12] (vendor specific)
Security: 
        Master password revision code = 65534
                supported
        not     enabled
        not     locked
        not     frozen
        not     expired: security count
                supported: enhanced erase
        240min for SECURITY ERASE UNIT. 240min for ENHANCED SECURITY ERASE UNIT.
Logical Unit WWN Device Identifier: 5000c50014ec1f60
        NAA             : 5
        IEEE OUI        : c50
        Unique ID       : 014ec1f60
Checksum: correct

Pine: Flags

To mark a message as unread in pine this can be done using flags.
First flags have to be turned on in setup
[M]ain [S]etup [C]onfig
Then find “enable-flag-cmd” and turn it on.
After this marking a message as unread is done by “* N” in the list view or when looking at the message.

All the flags are:

  • [N]New
  • [D]Deleted
  • [*] Important
  • [A] Answered

[^T] Can be used to see all the flags and set/unset them one by one (or all).

apt-get WARNING: The following packages cannot be authenticated!

apt-get is verifying the packages before installing them.
If the keys are not up to date, then apt-get upgrade will issue a warning.

WARNING: The following packages cannot be authenticated!
  ure uno-libs3
Install these packages without verification [y/N]?

The way to solve this is rather simple, just run apt-get update and it should download the keys automatically.

apt-key handles keys, using apt-key list will show you the keys that are on the computer.

Limit the download speed of apt-get

Sometimes it is nice to limit the download speed of apt-get.

The way I do this is to create a file “/etc/apt/apt.conf.d/76download” and enter the following to it

Acquire{Queue-mode "access";http{Dl-Limit "50";};};

This will limit apt-get to at most 50 kb/s for apt-get.
Some other how to’s suggest the use of dl-limit but be adviced, dl-limit is per each connection apt-get does (normally it does 2 at a time); the one used above caps apt-get in total.

OpenSSH and password-less logins

Using SSH you can run commands at remote servers.

By default the remote server will ask for credentials but for instance when writing a script it is not a good idea to store the login credentials in a script.
The solution to this is to create a key pair at the origin machine (where the script is) and then send this key to the remoteserver (where the script needs to login).

  • First create a key pair (only has to be done once for this machine; it can be reused for other machines if you wish to be able to login to several computers)
     jonas@jonas-desktop:~$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/jonas/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/jonas/.ssh/id_rsa.
    Your public key has been saved in /home/jonas/.ssh/id_rsa.pub.
    The key fingerprint is:
    11:f9:5a:8f:7d:74:e4:68:3b:4b:22:1c:78:e6:be:2d jonas@jonas-desktop
    The key's randomart image is:
    +--[ RSA 2048]----+
    |        ..       |
    |        ..      .|
    |        .o     + |
    |        ..*   + o|
    |        SB = o o |
    |        . = + =  |
    |         . . + o |
    |          E.  .  |
    |          .o.    |
    +-----------------+

    Passphrase is needed to “unlock” this key before being able to use it. Leaving passphrase blank means that the key can be used without any inputs.

  • Next send the key to the server that you wish to be able to login to.
     jonas@jonas-desktop:~$ ssh-copy-id -i .ssh/id_rsa.pub user@remoteserver
    Warning: Permanently added 'remoteserver,11.222.333.222' (RSA) to the list of known hosts.
    user@remoteserver's password: 
    Now try logging into the machine, with "ssh 'user@remoteserver'", and check in:
     
      .ssh/authorized_keys
     
    to make sure we haven't added extra keys that you weren't expecting.

    -i is the identity file we created with ssh-keygen

  • This is it. Now you are able to login to remoteserver as user without a password.

tar extracting to a target directory

By default untaring will put the contents into the current directory.
Sometimes it is not desirable to change directory to the target (such as in a scrip).

tar -xvvf /source/tarfile.tar -C /target

This will extract(x), very verbosly(vv) the file(f) /source/tarfile.tar to the directory(-C) /target (it will change the output folder to /target (so make sure it exists).

ubuntu: Upgrade release version

  1. Check out “/etc/update-manager/release-upgrades” to make check out what sort of releases you wish to use (lts/normal/or never upgrade)
    # default behavior for the release upgrader
    #
     
    [DEFAULT]
    # default prompting behavior, valid options:
    #  never  - never prompt for a new distribution version
    #  normal - prompt if a new version of the distribution is available
    #  lts    - prompt only if a LTS version of the distribution is available
    Prompt=normal
  2. Make sure the (current) dist is up to date. Problems are not guaranteed if you don’t – but they are likely
    • apt-get update
    • apt-get upgrade
  3. Make sure the upgrade tool is installed (and up to date) “apt-get install update-manager-core”
  4. do-release-upgrade
  5. Follow the instructions

What program is using port x?

netstat can be used to figure out what program is using a port.

$ netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1096/sshd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1733/apache2
tcp6       0      0 :::22                   :::*                    LISTEN      1096/sshd
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     6338     1071/gdm-simple-sla @/tmp/gdm-greeter-LvjLQPzT
unix  2      [ ACC ]     STREAM     LISTENING     5165     1072/X              /tmp/.X11-unix/X0

If you have a lot open then grep can be used to only find what you are looking for

netstat -nlp|grep :22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1096/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1096/sshd

For windows, simply start with

netstat -n