Change root directory linux

How to change directory in Linux terminal

M y Dell Laptop came preinstalled with Ubuntu Linux, and I am a new Linux desktop user. How do I change directories in the Linux terminal?

Introduction – On Linux the cd command allows you to change directories when using the terminal application. This page shows how to change directory in Linux terminal using the cd command.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Est. reading time 3 minutes

How to change directory in Linux terminal

  1. To return to the home directory immediately, use cd

OR cd

  • To change into the root directory of Linux file system, use cd / .
  • To go into the root user directory, run cd /root/ as root user.
  • To navigate up one directory level up, use cd ..
  • To go back to the previous directory, use cd —
  • Let us see all examples and usage for terminal in details.

    How to use the Linux command line to change directory or folder

    The directory in which the user is currently working is called the current working directory (CDW).

    How to print the current working directory in Linux

    To display the name of the current/working directory, type the following pwd command:
    pwd

    cd command in Linux termianl

    The syntax is:
    cd
    cd ..
    cd /path/to/dir
    When cd command used without stipulating any directory name, cd command returns to the home directory. Let us change the directory to /usr/sbin/, run:
    cd /usr/sbin/
    Verify it:
    pwd
    Want to list the files in the /usb/sbin/ directory? Try the ls command:
    ls
    ls -l
    Let us go back to user’s home directory, run:
    cd
    Again verify it:
    pwd

    • No ads and tracking
    • In-depth guides for developers and sysadmins at Opensourceflare✨
    • Join my Patreon to support independent content creators and start reading latest guides:
      • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
      • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
      • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
      • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
      • How to protect Linux against rogue USB devices using USBGuard

    Join Patreon

    Absolute vs Relative pathname

    The cd command changes the current directory when a directory name provided by the user. The name can be written as an absolute pathname (e.g. cd /etc/httpd/ ) or as local pathname relative to the root directory (e.g. cd conf.d/ ). For example:
    cd /etc/httpd/
    pwd
    ls
    cd conf.d/
    pwd
    ls

    The absolute vs. relative pathname for cd command in Linux

    Understanding . and .. directories

    On Linux the current directory is represented by a single dot ( . ) and two consecutive dots represent its parent directory ( .. ). Thus, to change to the parent of the current directory, run cd . .. For example:
    ls
    pwd
    cd ..
    pwd
    ls

    How can I return directly to my home directory when using the Linux terminal?

    How do I change directories in the Linux terminal and return to the previous directory?

    Simply pass the — option to the cd:
    cd —
    Verify it:
    pwd

    The -P option instructs cd to use the physical directory structure instead of following symbolic links:
    cd -P LinkDir
    pwd
    The -L option forces cd to follow symbolic links:
    cd -L LinkDir
    pwd

    Linux cd command cheat sheet

    Table 1: cd command under Linux
    Command Description
    cd Returns you to your login directory
    cd

    Also returns you to your login directory cd — Returns you to your previous working directory cd / Takes you to the entire system’s root directory. cd /root Takes you to the home directory of the root user. You must be the root user to access this directory. cd /home Takes you to the home directory, where user login directories are usually stored cd .. Takes you to the directory one level up. cd

    tom Takes you to tom’s home directory, if tom user has granted you permission cd /path/to/dir/ Take you to the /path/to/dir/ directory cd dir* Use a wildcard to change the directory name

    Conclusion

    The cd command is used to change the current directory in both Linux and other Unix-like systems. See Cd command wiki page.

    🐧 Get the latest tutorials on Linux, Open Source & DevOps via

    Источник

    chroot (2) — Linux Man Pages

    chroot: change root directory

    Command to display chroot manual in Linux: $ man 2 chroot

    chroot — change root directory

    SYNOPSIS

    int chroot(const char * path );

    Feature Test Macro Requirements for glibc (see feature_test_macros (7)):

    chroot (): Since glibc 2.2.2: Before glibc 2.2.2: none

    DESCRIPTION

    Only a privileged process (Linux: one with the CAP_SYS_CHROOT capability in its user namespace) may call chroot ().

    This call changes an ingredient in the pathname resolution process and does nothing else. In particular, it is not intended to be used for any kind of security purpose, neither to fully sandbox a process nor to restrict filesystem system calls. In the past, chroot () has been used by daemons to restrict themselves prior to passing paths supplied by untrusted users to system calls such as open (2). However, if a folder is moved out of the chroot directory, an attacker can exploit that to get out of the chroot directory as well. The easiest way to do that is to chdir (2) to the to-be-moved directory, wait for it to be moved out, then open a path like ../../../etc/passwd.

    A slightly trickier variation also works under some circumstances if chdir (2) is not permitted. If a daemon allows a «chroot directory» to be specified, that usually means that if you want to prevent remote users from accessing files outside the chroot directory, you must ensure that folders are never moved out of it.

    This call does not change the current working directory, so that after the call ‘ . ‘ can be outside the tree rooted at ‘ / ‘. In particular, the superuser can escape from a «chroot jail» by doing:

    mkdir foo; chroot foo; cd ..

    This call does not close open file descriptors, and such file descriptors may allow access to files outside the chroot tree.

    RETURN VALUE


    ERRORS


    CONFORMING TO


    NOTES

    The magic symbolic link, /proc/[pid]/root , can be used to discover a process’s root directory; see proc (5) for details.

    FreeBSD has a stronger jail () system call.

    Источник

    chroot(2) — Linux man page

    chroot — change root directory

    Synopsis

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)): chroot(): Since glibc 2.2.2: Before glibc 2.2.2: none

    Description

    chroot() changes the root directory of the calling process to that specified in path. This directory will be used for pathnames beginning with /. The root directory is inherited by all children of the calling process.

    Only a privileged process (Linux: one with the CAP_SYS_CHROOT capability) may call chroot().

    This call changes an ingredient in the pathname resolution process and does nothing else.

    This call does not change the current working directory, so that after the call ‘.‘ can be outside the tree rooted at ‘/‘. In particular, the superuser can escape from a «chroot jail» by doing: This call does not close open file descriptors, and such file descriptors may allow access to files outside the chroot tree.

    Return Value

    On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

    Errors

    Depending on the file system, other errors can be returned. The more general errors are listed below: EACCES

    Search permission is denied on a component of the path prefix. (See also path_resolution(7).)

    path points outside your accessible address space.

    An I/O error occurred.

    Too many symbolic links were encountered in resolving path. ENAMETOOLONG path is too long. ENOENT

    The file does not exist.

    Insufficient kernel memory was available. ENOTDIR A component of path is not a directory. EPERM

    The caller has insufficient privilege.

    Conforming To

    SVr4, 4.4BSD, SUSv2 (marked LEGACY). This function is not part of POSIX.1-2001.

    Notes

    A child process created via fork(2) inherits its parent’s root directory. The root directory is left unchanged by execve(2).

    FreeBSD has a stronger jail() system call.

    Источник

    How to change the root directory for a Linux user?

    For a user on a Linux host, I need to make everything inaccessible besides his home directory.

    I have heard that this is usually done by changing the root directory for the user (and setting it to the user’s home directory), however I couldn’t find the way to do it.

    I thought about the chroot command, but it seems it just runs the specified command, considering the specified directory as the root directory. So it seems chroot is not what i need.

    So my question is: what is the command which changes the user’s root directory?

    8 Answers 8

    It sounds like you want to change the HOME dir, not the root dir. Root will always be / or /root.

    To be sure that » everything inaccessible » behind this home directory, i would use chroot, but after you cannot do anything if you don’t prepare the chrooted directory, you don’t have any binaries or libs available.

    It is a full chroot settings set to restrict the ftp users to this chrooted directory.

    I stand by this answer, and I don’t care if I got a -1 over it. This isn’t something Unix can do for you in any reasonable kind of way. You’re asking for something that’s practically impossible.

    But there is sort of a way to get something like what you’re asking for, kind of. Use a virtual machine. Set up an instance of the virtual machine for the user in question and only give them login rights on it, not the host OS.

    Here is my original answer:

    Just use permissions. That’s what they’re for. A user generally doesn’t have access to write to anything other than /tmp and their home directory anyway. And they have to have read access to /bin/ , /usr/bin , /usr/lib , and /lib or they aren’t going to be able to do anything at all.

    If you really want something more strict than permissions give you, then looking into setting up selinux so you can restrict them further.

    Using chroot just isn’t going to give you what you want. It’s really complicated to set up, and there are ways of getting around it.

    see the manual of useradd . You have to use the -d switch.

    I think if you add

    to the end of your /etc/profile you might get what you are asking for, but it may have some side effects I’m not aware of (taking over the shell running /etc/profile might cause problems). In this case you would need to create a file /etc/jailbirds which was a list of the users you wanted to chroot for. You’ll also need to make sure that this file exists and decide what to do in the case of errors accessing it to make sure that other users (especially root) don’t get locked up by chroot

    Additionally, doing this is going to be difficult. The user will be unable to access /proc which will make many shell utilities (such as ps , top , and many others) not work. The user will also not be able to access /dev/null , which will break lots of stuff.

    The user will also not be able to access /dev/tty which will break less and more when used with standard input. The user’s applications will inherit the stdin, stdout, and stderr open file descriptors from before the chroot, but less opens /dev/tty so that it can get keystrokes from the user to deal with having stdin redirected from a pipe.

    You will also not be able to access any programs or shared libraries or many files that live under /etc (such as /etc/protocols ) that many programs use.

    All of these are things you can work around, but you will end up needing to do a lot more work to make the users’ environment usable than I think you have considered.

    If you do try my suggestion I suggest you be very careful. It would be easy to get it wrong and mess things up bad. I have not tested this, by the way.

    Источник

    Change Apache document root folder to secondary hard drive

    I installed the ubuntu 12.04 server edition for my server pc . i had installed lamp server. i need to change the var/www location to my secondary hard disk location. i was configured so many time to at gedit /etc/apache2/sites-available/default here is my code

    still i was not able to connect my /media/myserver and my browser show the following message

    Please tell anyone how to mount myserver at my var/www, thanks advance

    8 Answers 8

    You’ll have to edit apache2.conf and 000-default.conf to change the document root of apache.

    The Apache server is installed on /var/www/html .This is the default root directory of apache.

    Either change the root directory of Apache or move the project to /var/www/html .

    To change Apache’s root directory, run:

    Then open the 000-default.conf file using the command:

    Edit the DocumentRoot option:

    Then restart the apache server:

    If you get Forbidden You don’t have permission to access / on this server after changing the root of apache then do follow these steps

    Find the apache2.conf located in /etc/apache2 and open it using:

    Use Ctrl + W and search for Directory (It should be in line 153)

    It should look like this

    I made a script that changes apache root in a single command. You can find it on my github.

    Maybe a little bit late. But still..

    You should edit your directory permissions in apache.conf under /etc/apache2

    Search for this

    and add this code under of it, which gives the permission to access your directory

    Simply change the document root in your activated configuration. /etc/apache2/sites-enabled/000-default and then Make sure reloading your apache.

    So try with this:

    Then the proper permission should be given like this:

    you modify apache2.conf . Replace /var/www/ with /your/path :

    Modify the 000-default.conf file:

    Change the ownership of the directories to yourself from file manager or terminal (e.g. sudo chown pi: path ). Without this you will get this error message:

    As a quick workaround (safe and quick) you can make the mounting point of your external hard driver to the default root directory ( /var/www by default).

    Assigning the mounting point to a per-existing directory is safe but the old content can’t be reached unless you unmounted the driver.

    To learn more how to create a mounting point refer to this.

    sudo gedit etc/apache2/apache2.conf add this Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all

    in virtual configuration : /etc/apache2/sites-available/site.conf

    sudo a2dissite 000-default.conf

    For those who use VirtualBox guest additions and get you don’t have permission to access /on this server despite of everything mentioned above:

    If you are trying to set the Apache document root folder to a VirtualBox shared folder,and you have tried everything above and that did not help, there is one more step.

    In short, the solution is to add user ‘www-data’ to the group ‘vboxsf’:

    You can not change the owner and/or the group of the VirtualBox shared folder, but the solution above worked well for me.

    Источник

    Читайте также:  Cad системы под linux
    Оцените статью